From fe1df904eaefb7abc611b64a219b536a06ec6389 Mon Sep 17 00:00:00 2001 From: briandennis Date: Thu, 8 Mar 2018 15:50:06 -0700 Subject: [PATCH] pass encode state to api instead of url --- README.md | 8 ++++---- handlers/image.js | 8 +++++--- lib/api.js | 4 ++-- lib/routing.js | 5 ++--- pages/editor.js | 10 ++++++---- 5 files changed, 19 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index a32eec4..e25fc20 100644 --- a/README.md +++ b/README.md @@ -80,9 +80,9 @@ Thanks goes out to all these wonderful people ([emoji key](https://github.com/ke -| [
briandennis](https://github.com/briandennis)
[πŸ’»](https://github.com/dawnlabs/carbon/commits?author=briandennis "Code") [πŸ“–](https://github.com/dawnlabs/carbon/commits?author=briandennis "Documentation") [πŸš‡](#infra-briandennis "Infrastructure (Hosting, Build-Tools, etc)") [πŸ‘€](#review-briandennis "Reviewed Pull Requests") | [
mfix22](https://github.com/mfix22)
[πŸ’¬](#question-mfix22 "Answering Questions") [πŸ’»](https://github.com/dawnlabs/carbon/commits?author=mfix22 "Code") [πŸ€”](#ideas-mfix22 "Ideas, Planning, & Feedback") | [
jakedex](https://github.com/jakedex)
[πŸ’¬](#question-jakedex "Answering Questions") [πŸ’»](https://github.com/dawnlabs/carbon/commits?author=jakedex "Code") [🎨](#design-jakedex "Design") [πŸ“Ή](#video-jakedex "Videos") | [
andrewda](https://github.com/andrewda)
[πŸ’¬](#question-andrewda "Answering Questions") [πŸ’»](https://github.com/dawnlabs/carbon/commits?author=andrewda "Code") [πŸ›](https://github.com/dawnlabs/carbon/issues?q=author%3Aandrewda "Bug reports") [πŸ‘€](#review-andrewda "Reviewed Pull Requests") | [
yeskunall](https://github.com/yeskunall)
[πŸ’»](https://github.com/dawnlabs/carbon/commits?author=yeskunall "Code") [πŸ“–](https://github.com/dawnlabs/carbon/commits?author=yeskunall "Documentation") [πŸ”§](#tool-yeskunall "Tools") [πŸ›](https://github.com/dawnlabs/carbon/issues?q=author%3Ayeskunall "Bug reports") | [
stoshfabricius](https://github.com/stoshfabricius)
[πŸ’»](https://github.com/dawnlabs/carbon/commits?author=stoshfabricius "Code") | [
jkling38](https://github.com/jkling38)
[πŸ“–](https://github.com/dawnlabs/carbon/commits?author=jkling38 "Documentation") | +| [
briandennis](https://github.com/briandennis)
[πŸ’»](/dawnlabs/carbon/commits?author=briandennis "Code") [πŸ“–](/dawnlabs/carbon/commits?author=briandennis "Documentation") [πŸš‡](#infra-briandennis "Infrastructure (Hosting, Build-Tools, etc)") [πŸ‘€](#review-briandennis "Reviewed Pull Requests") | [
mfix22](https://github.com/mfix22)
[πŸ’¬](#question-mfix22 "Answering Questions") [πŸ’»](/dawnlabs/carbon/commits?author=mfix22 "Code") [πŸ€”](#ideas-mfix22 "Ideas, Planning, & Feedback") | [
jakedex](https://github.com/jakedex)
[πŸ’¬](#question-jakedex "Answering Questions") [πŸ’»](/dawnlabs/carbon/commits?author=jakedex "Code") [🎨](#design-jakedex "Design") [πŸ“Ή](#video-jakedex "Videos") | [
andrewda](https://github.com/andrewda)
[πŸ’¬](#question-andrewda "Answering Questions") [πŸ’»](/dawnlabs/carbon/commits?author=andrewda "Code") [πŸ›](/dawnlabs/carbon/issues?q=author%3Aandrewda "Bug reports") [πŸ‘€](#review-andrewda "Reviewed Pull Requests") | [
yeskunall](https://github.com/yeskunall)
[πŸ’»](/dawnlabs/carbon/commits?author=yeskunall "Code") [πŸ“–](/dawnlabs/carbon/commits?author=yeskunall "Documentation") [πŸ”§](#tool-yeskunall "Tools") [πŸ›](/dawnlabs/carbon/issues?q=author%3Ayeskunall "Bug reports") | [
stoshfabricius](https://github.com/stoshfabricius)
[πŸ’»](/dawnlabs/carbon/commits?author=stoshfabricius "Code") | [
jkling38](https://github.com/jkling38)
[πŸ“–](/dawnlabs/carbon/commits?author=jkling38 "Documentation") | | :---: | :---: | :---: | :---: | :---: | :---: | :---: | -| [
otobrglez](https://github.com/otobrglez)
[πŸ’»](https://github.com/dawnlabs/carbon/commits?author=otobrglez "Code") | [
darahak](https://github.com/darahak)
[πŸ“–](https://github.com/dawnlabs/carbon/commits?author=darahak "Documentation") | [
dom96](https://github.com/dom96)
[πŸ’»](https://github.com/dawnlabs/carbon/commits?author=dom96 "Code") | [
elrumordelaluz](https://github.com/elrumordelaluz)
[πŸ’»](https://github.com/dawnlabs/carbon/commits?author=elrumordelaluz "Code") | [
cjb](https://github.com/cjb)
[πŸ’»](https://github.com/dawnlabs/carbon/commits?author=cjb "Code") | [
Krzysztof-Cieslak](https://github.com/Krzysztof-Cieslak)
[πŸ’»](https://github.com/dawnlabs/carbon/commits?author=Krzysztof-Cieslak "Code") | [
fernahh](https://github.com/fernahh)
[πŸ“–](https://github.com/dawnlabs/carbon/commits?author=fernahh "Documentation") | -| [
g3r4n](https://github.com/g3r4n)
[πŸ’»](https://github.com/dawnlabs/carbon/commits?author=g3r4n "Code") | [
Mat Gadd](http://drarok.com/)
[πŸ›](https://github.com/dawnlabs/carbon/issues?q=author%3ADrarok "Bug reports") [πŸ’»](https://github.com/dawnlabs/carbon/commits?author=Drarok "Code") | [
Brad Davies](https://bradlab.co.uk)
[πŸ›](https://github.com/dawnlabs/carbon/issues?q=author%3Avarbrad "Bug reports") [πŸ’»](https://github.com/dawnlabs/carbon/commits?author=varbrad "Code") | [
Rafael CΓ’mara](http://www.rafaelcamaram.com/)
[πŸ’»](https://github.com/dawnlabs/carbon/commits?author=rafaelcamaram "Code") | [
Gleb Bahmutov](https://glebbahmutov.com/)
[⚠️](https://github.com/dawnlabs/carbon/commits?author=bahmutov "Tests") [πŸ”§](#tool-bahmutov "Tools") | [
IvΓ‘n MunguΓ­a](https://ivan-munguia.netlify.com)
[πŸ’»](https://github.com/dawnlabs/carbon/commits?author=warborn "Code") | [
Dillon Mulroy](https://dillonmulroy.com)
[πŸ’»](https://github.com/dawnlabs/carbon/commits?author=dmmulroy "Code") | -| [
Nihad Abbasov](https://github.com/NARKOZ)
[πŸ’»](https://github.com/dawnlabs/carbon/commits?author=NARKOZ "Code") | [
Hugo Torzuoli](https://torzuolih.github.io)
[πŸ“–](https://github.com/dawnlabs/carbon/commits?author=TorzuoliH "Documentation") | [
Bruno C. Couto](https://github.com/bruno02221)
[πŸ’»](https://github.com/dawnlabs/carbon/commits?author=bruno02221 "Code") | [
Mark Molnar](https://github.com/molnarmark)
[πŸ’»](https://github.com/dawnlabs/carbon/commits?author=molnarmark "Code") | [
Takahiko Inayama](https://www.behance.net/tetra2000)
[πŸ’»](https://github.com/dawnlabs/carbon/commits?author=TETRA2000 "Code") | [
François Martin](https://github.com/martinfrancois)
[πŸ’»](https://github.com/dawnlabs/carbon/commits?author=martinfrancois "Code") | +| [
otobrglez](https://github.com/otobrglez)
[πŸ’»](/dawnlabs/carbon/commits?author=otobrglez "Code") | [
darahak](https://github.com/darahak)
[πŸ“–](/dawnlabs/carbon/commits?author=darahak "Documentation") | [
dom96](https://github.com/dom96)
[πŸ’»](/dawnlabs/carbon/commits?author=dom96 "Code") | [
elrumordelaluz](https://github.com/elrumordelaluz)
[πŸ’»](/dawnlabs/carbon/commits?author=elrumordelaluz "Code") | [
cjb](https://github.com/cjb)
[πŸ’»](/dawnlabs/carbon/commits?author=cjb "Code") | [
Krzysztof-Cieslak](https://github.com/Krzysztof-Cieslak)
[πŸ’»](/dawnlabs/carbon/commits?author=Krzysztof-Cieslak "Code") | [
fernahh](https://github.com/fernahh)
[πŸ“–](/dawnlabs/carbon/commits?author=fernahh "Documentation") | +| [
g3r4n](https://github.com/g3r4n)
[πŸ’»](/dawnlabs/carbon/commits?author=g3r4n "Code") | [
Mat Gadd](http://drarok.com/)
[πŸ›](/dawnlabs/carbon/issues?q=author%3ADrarok "Bug reports") [πŸ’»](/dawnlabs/carbon/commits?author=Drarok "Code") | [
Brad Davies](https://bradlab.co.uk)
[πŸ›](/dawnlabs/carbon/issues?q=author%3Avarbrad "Bug reports") [πŸ’»](/dawnlabs/carbon/commits?author=varbrad "Code") | [
Rafael CΓ’mara](http://www.rafaelcamaram.com/)
[πŸ’»](/dawnlabs/carbon/commits?author=rafaelcamaram "Code") | [
Gleb Bahmutov](https://glebbahmutov.com/)
[⚠️](/dawnlabs/carbon/commits?author=bahmutov "Tests") [πŸ”§](#tool-bahmutov "Tools") | [
IvΓ‘n MunguΓ­a](https://ivan-munguia.netlify.com)
[πŸ’»](/dawnlabs/carbon/commits?author=warborn "Code") | [
Dillon Mulroy](https://dillonmulroy.com)
[πŸ’»](/dawnlabs/carbon/commits?author=dmmulroy "Code") | +| [
Nihad Abbasov](https://github.com/NARKOZ)
[πŸ’»](/dawnlabs/carbon/commits?author=NARKOZ "Code") | [
Hugo Torzuoli](https://torzuolih.github.io)
[πŸ“–](/dawnlabs/carbon/commits?author=TorzuoliH "Documentation") | [
Bruno C. Couto](https://github.com/bruno02221)
[πŸ’»](/dawnlabs/carbon/commits?author=bruno02221 "Code") | [
Mark Molnar](https://github.com/molnarmark)
[πŸ’»](/dawnlabs/carbon/commits?author=molnarmark "Code") | [
Takahiko Inayama](https://www.behance.net/tetra2000)
[πŸ’»](/dawnlabs/carbon/commits?author=TETRA2000 "Code") | [
François Martin](https://github.com/martinfrancois)
[πŸ’»](/dawnlabs/carbon/commits?author=martinfrancois "Code") | diff --git a/handlers/image.js b/handlers/image.js index 90ffe9a..14d914e 100644 --- a/handlers/image.js +++ b/handlers/image.js @@ -1,11 +1,13 @@ +const port = parseInt(process.env.PORT, 10) || 3000 + module.exports = browser => async (req, res) => { let page = await browser.newPage() - let url = req.body.url + let state = req.body.state - if (!url) res.status(400).send() + if (!state) res.status(400).send() try { - await page.goto(url) + await page.goto(`http://localhost:${port}?state=${state}`) await page.addScriptTag({ path: `./lib/customDomToImage.js` }) const targetElement = await page.$('#export-container') diff --git a/lib/api.js b/lib/api.js index 55b57f0..7a37b8a 100644 --- a/lib/api.js +++ b/lib/api.js @@ -26,8 +26,8 @@ async function tweet(encodedImage) { .catch(checkIfRateLimited) } -async function image(url) { - return axios.post(`${DOMAIN}/image`, { url }).then(res => res.data.dataUrl) +async function image(state) { + return axios.post(`${DOMAIN}/image`, { state }).then(res => res.data.dataUrl) } const getGist = uid => { diff --git a/lib/routing.js b/lib/routing.js index 369212f..306a79f 100644 --- a/lib/routing.js +++ b/lib/routing.js @@ -46,8 +46,7 @@ const reverseMappings = mappings.map(mapping => }) ) - -const serializeState = state => { +export const serializeState = state => { const stateString = encodeURIComponent(JSON.stringify(state)) return typeof window !== 'undefined' @@ -55,7 +54,7 @@ const serializeState = state => { : Buffer.from(stateString).toString('base64') } -const deserializeState = serializedState => { +export const deserializeState = serializedState => { let stateString if (typeof window !== 'undefined') { stateString = atob(serializedState) diff --git a/pages/editor.js b/pages/editor.js index 09350b1..eb9314f 100644 --- a/pages/editor.js +++ b/pages/editor.js @@ -33,7 +33,7 @@ import { DEFAULT_BG_COLOR, DEFAULT_SETTINGS } from '../lib/constants' -import { getQueryStringState, updateQueryString } from '../lib/routing' +import { getQueryStringState, updateQueryString, serializeState } from '../lib/routing' import { getState, saveState } from '../lib/util' const removeQueryString = str => { @@ -79,6 +79,7 @@ class Editor extends React.Component { this.updateTitleBar = this.updateTitleBar.bind(this) this.updateAspectRatio = this.updateAspectRatio.bind(this) this.resetDefaultSettings = this.resetDefaultSettings.bind(this) + this.getCarbonImage = this.getCarbonImage.bind(this) } componentDidMount() { @@ -104,12 +105,13 @@ class Editor extends React.Component { //if safari, get image from api if ( navigator.userAgent.indexOf('Safari') != -1 && - navigator.userAgent.indexOf('Chrome') == -1 + navigator.userAgent.indexOf('Chrome') == -1 && + format === 'png' ) { - return api.image(window.location.href) + const encodedState = serializeState(this.state) + return api.image(encodedState) } - const node = document.getElementById('export-container') const exportSize = (EXPORT_SIZES_HASH[this.state.exportSize] || DEFAULT_EXPORT_SIZE).value