|
| 1 | +/** |
| 2 | + * Module dependencies. |
| 3 | + */ |
| 4 | +import { renderToString } from 'react-dom/server' |
| 5 | +import { Provider } from 'react-redux' |
| 6 | +import undoable from 'redux-undo' |
| 7 | +import reducer from './src/reducer' |
| 8 | +import App from './src/components/App' |
| 9 | +import gm from 'gm' |
| 10 | + |
| 11 | +var express = require('express') |
| 12 | + , routes = require('./routes') |
| 13 | + , bodyParser = require('body-parser') |
| 14 | + , cookieParser = require('cookie-parser') |
| 15 | + , temp = require('temp') |
| 16 | + , fs = require('fs') |
| 17 | + , path = require('path') |
| 18 | + , Twitter = require('twitter') |
| 19 | + , OAuth= require('oauth').OAuth |
| 20 | + , session = require('express-session') |
| 21 | + , React = require('react') |
| 22 | + , redux = require('redux') |
| 23 | + ; |
| 24 | + |
| 25 | +var app = module.exports = express(), |
| 26 | + configData = JSON.parse(fs.readFileSync('config.json', 'utf8')); |
| 27 | + |
| 28 | +/** |
| 29 | + * Configuration |
| 30 | + */ |
| 31 | +var env = process.env.NODE_ENV || 'development'; |
| 32 | +if ('development' == env) { |
| 33 | + configData = configData.dev; |
| 34 | +} else { |
| 35 | + configData = configData.prod; |
| 36 | +} |
| 37 | + |
| 38 | +var oa = new OAuth( |
| 39 | + "https://api.twitter.com/oauth/request_token", |
| 40 | + "https://api.twitter.com/oauth/access_token", |
| 41 | + configData.twitter.consumer_key, |
| 42 | + configData.twitter.consumer_secret, |
| 43 | + "1.0A", |
| 44 | + "http://127.0.0.1:3000/auth/twitter/callback", |
| 45 | + "HMAC-SHA1" |
| 46 | + ); |
| 47 | + |
| 48 | +app.set('views', __dirname + '/views'); |
| 49 | +app.set('view engine', 'jade'); |
| 50 | +app.use(express.static(__dirname + '/deploy')); |
| 51 | +app.use(bodyParser.json()); |
| 52 | +app.use(bodyParser.urlencoded({ extended: true })); |
| 53 | +app.use(cookieParser()); |
| 54 | +app.use(session({ secret: configData.express.session_secret, resave: true, saveUninitialized: true })); |
| 55 | + |
| 56 | +/** |
| 57 | + * Routes |
| 58 | + */ |
| 59 | +app.get('/', handleRender); |
| 60 | + |
| 61 | +app.post('/auth/twitter', function(req, res) { |
| 62 | + oa.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, results){ |
| 63 | + if (error) { |
| 64 | + console.log(error); |
| 65 | + res.send("auth twitter: error") |
| 66 | + } |
| 67 | + else { |
| 68 | + // console.log(oauth_token); |
| 69 | + // console.log(oauth_token_secret); |
| 70 | + req.session.oauthRequestToken = oauth_token; |
| 71 | + req.session.oauthRequestTokenSecret = oauth_token_secret; |
| 72 | + req.session.cssData = req.body; |
| 73 | + |
| 74 | + res.contentType('application/json'); |
| 75 | + var data = JSON.stringify('https://twitter.com/oauth/authenticate?oauth_token='+oauth_token); |
| 76 | + res.header('Content-Length', data.length); |
| 77 | + res.end(data); |
| 78 | + } |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +app.get('/auth/twitter/callback', function(req, res, next){ |
| 83 | + // console.log(">>" + req.session.oauthRequestToken); |
| 84 | + // console.log(">>" + req.session.oauthRequestTokenSecret); |
| 85 | + // console.log(">>" + req.query.oauth_verifier); |
| 86 | + |
| 87 | + if (req.query) { |
| 88 | + oa.getOAuthAccessToken(req.session.oauthRequestToken, req.session.oauthRequestTokenSecret, req.query.oauth_verifier, |
| 89 | + function(error, oauthAccessToken, oauthAccessTokenSecret, results){ |
| 90 | + if (error){ |
| 91 | + console.log(error); |
| 92 | + res.send("auth twitter callback: error"); |
| 93 | + } else { |
| 94 | + req.session.oauthAccessToken = oauthAccessToken; |
| 95 | + req.session.oauthAccessTokenSecret = oauthAccessTokenSecret; |
| 96 | + |
| 97 | + var client = new Twitter({ |
| 98 | + consumer_key: configData.twitter.consumer_key, |
| 99 | + consumer_secret: configData.twitter.consumer_secret, |
| 100 | + access_token_key: oauthAccessToken, |
| 101 | + access_token_secret: oauthAccessTokenSecret |
| 102 | + }); |
| 103 | + |
| 104 | + var randomName = temp.path({suffix: '.png'}), |
| 105 | + imgPath = 'images' + randomName; |
| 106 | + |
| 107 | + drawFromCss(req.session.cssData, imgPath, function() { |
| 108 | + // Tweet message and drawing |
| 109 | + var data = require('fs').readFileSync(imgPath); |
| 110 | + client.post('media/upload', {media: data}, function(error, media, response){ |
| 111 | + if (!error) { |
| 112 | + // If successful, a media object will be returned. |
| 113 | + var status = { |
| 114 | + status: req.session.cssData.text, |
| 115 | + media_ids: media.media_id_string // Pass the media id string |
| 116 | + } |
| 117 | + client.post('statuses/update', status, function(error, tweet, response){ |
| 118 | + if (!error) { |
| 119 | + // Success |
| 120 | + console.log(tweet); |
| 121 | + res.redirect('/') |
| 122 | + } |
| 123 | + fs.unlinkSync(imgPath); |
| 124 | + }); |
| 125 | + } else { |
| 126 | + console.log(error); |
| 127 | + fs.unlinkSync(imgPath); |
| 128 | + } |
| 129 | + }); |
| 130 | + }); |
| 131 | + } |
| 132 | + } |
| 133 | + ); |
| 134 | + } else |
| 135 | + next(new Error("auth twitter callback: error")) |
| 136 | +}); |
| 137 | + |
| 138 | +app.listen(3000, '127.0.0.1', function(){ |
| 139 | + console.log("Express server listening on port %d in %s mode", 3000, app.settings.env); |
| 140 | +}); |
| 141 | + |
| 142 | +/** |
| 143 | + * Redux helper functions |
| 144 | + */ |
| 145 | +function handleRender(req, res) { |
| 146 | + // Create a new Redux store instance |
| 147 | + const store = redux.createStore(undoable(reducer, { |
| 148 | + initTypes: ['@@redux/SET_INITIAL_STATE', '@@SET_INITIAL_STATE'], // history will be (re)set upon init action type |
| 149 | + debug: true |
| 150 | + })); |
| 151 | + |
| 152 | + //Dispatch initial state |
| 153 | + store.dispatch({ |
| 154 | + type: 'SET_INITIAL_STATE', |
| 155 | + state: {} |
| 156 | + }); |
| 157 | + |
| 158 | + // Render the component to a string |
| 159 | + const html = renderToString( |
| 160 | + <Provider store={store}> |
| 161 | + <App /> |
| 162 | + </Provider> |
| 163 | + ); |
| 164 | + |
| 165 | + // Grab the initial state from our Redux store |
| 166 | + const initialState = store.getState(); |
| 167 | + |
| 168 | + // Send the rendered page back to the client |
| 169 | + res.send(renderFullPage(html, initialState)); |
| 170 | +} |
| 171 | + |
| 172 | +function renderFullPage(html, initialState) { |
| 173 | + return ` |
| 174 | + <!DOCTYPE html> |
| 175 | + <html> |
| 176 | + <head> |
| 177 | + <title>Pixel Art to CSS</title> |
| 178 | + <link rel="stylesheet" type="text/css" href="main.css"> |
| 179 | + </head> |
| 180 | + <body> |
| 181 | + <header class="grid"> |
| 182 | + <div class="col-2-3"> |
| 183 | + <h1>Pixel Art to CSS</h1> |
| 184 | + </div> |
| 185 | + <div class="credits-wrapper col-1-3"> |
| 186 | + <div> |
| 187 | + <h2>by <a target="_blank" href="http://www.jvrpath.com/">jvalen</a></h2> |
| 188 | + <iframe src="https://ghbtns.com/github-btn.html?user=jvalen&repo=pixel-art-react&type=star&count=true&size=large" frameborder="0" scrolling="0" width="120px" height="30px"></iframe> |
| 189 | + </div> |
| 190 | + </div> |
| 191 | + </header> |
| 192 | + <div id="app">${html}</div> |
| 193 | + <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script> |
| 194 | + <script src="bundle.js"></script> |
| 195 | + </body> |
| 196 | + </html> |
| 197 | + ` |
| 198 | +} |
| 199 | + |
| 200 | +/** |
| 201 | + * Draw image with CSS data |
| 202 | + */ |
| 203 | +function drawFromCss(data, path, callback){ |
| 204 | + var width = data.cols * data.pixelSize, |
| 205 | + height = data.rows * data.pixelSize, |
| 206 | + opacity = 0; |
| 207 | + |
| 208 | + data.boxShadow = data.boxShadow.map(function(elem){ |
| 209 | + return ( |
| 210 | + { |
| 211 | + x: elem[0], |
| 212 | + y:elem[1], |
| 213 | + color:elem[3]} |
| 214 | + ) |
| 215 | + } |
| 216 | + ); |
| 217 | + |
| 218 | + let gmImg = gm(width, height, '#000000' + ('0' + Math.round( (1 - opacity) * 255 ).toString(16)).slice(-2)); |
| 219 | + |
| 220 | + for (var i = 0; i < data.boxShadow.length; i++) { |
| 221 | + var aux = data.boxShadow[i]; |
| 222 | + gmImg.fill(aux.color).drawRectangle( |
| 223 | + aux.x - data.pixelSize, aux.y - data.pixelSize, aux.x, aux.y |
| 224 | + ); |
| 225 | + } |
| 226 | + |
| 227 | + gmImg.write( |
| 228 | + path, |
| 229 | + function (err) { |
| 230 | + if (err) console.log(err); |
| 231 | + callback(); |
| 232 | + } |
| 233 | + ); |
| 234 | +} |
0 commit comments