Skip to content

Instantly share code, notes, and snippets.

@NoiseEee
Last active December 13, 2016 21:13
Show Gist options
  • Save NoiseEee/88ec728cd929db5c69d2187b6cf4abf1 to your computer and use it in GitHub Desktop.
Save NoiseEee/88ec728cd929db5c69d2187b6cf4abf1 to your computer and use it in GitHub Desktop.
var url = require('url'),
fs = require('fs'),
io = require('socket.io'),
sys = require(process.binding('natives').util ? 'util' : 'sys'),
express = require('express'),
Caman = require('caman').Caman,
bodyParser = require('body-parser'),
app,
server;
setupServer();
function setupServer() {
"use strict";
app = express();
server = require('http').createServer(app);
//redirect any get requests to our website - we don't serve those
app.get('/*',function(req,res) {
res.redirect('https://website.com/signIn.php');
});
//this server is running on port 2015
server.listen(2015);
//prepare our server for accepting POST requests and doin shit with them (ie: adjusting images, etc)
setupPostListening();
}
/**
* Receving information from POSTs to the server
*/
function setupPostListening() {
"use strict";
app.use(bodyParser.urlencoded({ extended: false }));
/**
* CamanJS Photo Server Setup
*/
//app.use(bodyParser.json());
app.post('/photoServer', function (req, res) {
var photoKey, camanSettings, isCropped;
if(typeof req.body.photoKey==='undefined') {
res.writeHead(400, "Bad Request (No key)", {'Content-Type': 'text/html'});
res.end();
return;
}
photoKey = req.body.photoKey;
camanSettings = JSON.parse(req.body.camanSettings);
isCropped = req.body.isCropped;
if(!adjustImage(res, '/path/to/some.jpg', camanSettings)) {
sendBad(res,666,'wtf');
}
});
}
function adjustImage(res, filePath, camanSettings) {
//is the filePath actually accessable / and read/writeable?
var regEx = /\/([^\/]+$)/;
var match = regEx.exec(filePath);
var fileOnly = match[1];
//let's write to tmp
var tmpDestination = '/tmp/'+fileOnly;
try {
fs.accessSync(filePath, fs.R_OK);
} catch (err) {
console.log('tried to read '+filePath);
res.writeHead(403, "No filesystem access? (cannot read image at "+filePath+")");
res.end();
}
//console.log(camanSettings);
var adjustedImage = tmpDestination.replace('.jpg','_CAMAN.png');
try {
Caman(filePath, function () {
this.exposure(camanSettings.exposure)
.brightness(camanSettings.brightness)
.contrast(camanSettings.contrast)
.hue(camanSettings.hue)
.saturation(camanSettings.saturation)
.rotate(camanSettings.rotation);
this.render(function () {
this.nodeSave(adjustedImage);
//caman has no 'close' or anything in their documentation; presumably I should be freeing memory here???
//can now send the response since the image has been saved
res.json({
'success': true,
'adjustedImage': adjustedImage
});
});
});
} catch (err) {
sendBad(res, 500, 'Fucked up Caman');
return false;
}
return true;
}
/**
* Making our Express server respond with an error code
* @param res
* @param code
* @param msg
*/
function sendBad(res, code, msg) {
res.writeHead(code, msg, {'Content-Type': 'text/html'});
res.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment