Skip to content

Commit

Permalink
Make cropping work consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
rogierslag committed Jun 22, 2016
1 parent 21da2a8 commit ade84e0
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 190 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
"operator-assignment": [1, "never"], // require assignment operator shorthand where possible or prohibit it entirely (off by default)
"padded-blocks": [1, "never"], // enforce padding within blocks (off by default)
"quote-props": [1, "as-needed"], // require quotes around object literal property names (off by default)
"quotes": [1, "single"], // specify whether double or single quotes should be used
"quotes": [0, "single"], // specify whether double or single quotes should be used
"semi": [1, "always"], // require or disallow use of semicolons instead of ASI
"semi-spacing": [1, {
"before": false,
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ADD package.json /opt/live-image-resize/package.json
RUN cd /opt/live-image-resize && npm install

# Add the application
ADD *.js /opt/live-image-resize/src/
ADD src/*.js /opt/live-image-resize/src/
ADD migrations /opt/live-image-resize/migrations/
RUN cd /opt/live-image-resize/src && babel -d ../ *.js

Expand Down
59 changes: 0 additions & 59 deletions Token.js

This file was deleted.

2 changes: 1 addition & 1 deletion create-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ git tag -a $VERSION -m "Public release of version ${VERSION}"
git push > /dev/null
git push --tags > /dev/null

echo "Version ${VERSION} was succesfully released. On Github you can complete the release: https://github.com/inventid/live-image-resize/releases/tag/${VERSION}"
echo "Version ${VERSION} was successfully released. On Github you can complete the release: https://github.com/inventid/live-image-resize/releases/tag/${VERSION}"
git checkout develop > /dev/null
exit 0

Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
"name": "live-image-resize",
"version": "1.1.0",
"description": "Dynamic image resizing service, using AWS S3 as a cache",
"main": "index.js",
"main": "src/index.js",
"dependencies": {
"aws-sdk": "^2.2.26",
"body-parser": "^1.14.2",
"config": "^1.17.1",
"express": "^4.13.3",
"formidable": "^1.0.17",
"fs-extra": "^0.26.0",
"gm": "^1.21.1",
"gm": "^1.22.0",
"node-uuid": "^1.4.7",
"pg": "^4.4.3",
"pg-migration": "^1.0.1",
"pg-native": "^1.10.0",
"response-time": "^2.3.1"
"response-time": "^2.3.1",
"uuid": "^2.0.2"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
11 changes: 11 additions & 0 deletions src/AwsS3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import AWS from "aws-sdk";
import config from "config";

// The AWS config needs to be set before this object is created
AWS.config.update({
accessKeyId: config.get('aws.access_key'),
secretAccessKey: config.get('aws.secret_key'),
region: config.get('aws.region')
});
const S3 = new AWS.S3();
export default S3;
File renamed without changes.
File renamed without changes.
File renamed without changes.
34 changes: 34 additions & 0 deletions src/RequestLogger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import parsing from './UrlParsing';
import log from './Log';

function logRequest(req, res, time) {
const remoteIp = req.headers['x-forwarded-for'] || req.ip;
const obj = {
datetime: Date.now(),
method: req.method,
url: req.url,
client: remoteIp,
response_time: (time / 1e3),
response_status: res.statusCode
};
if (parsing.isGetRequest(req)) {
if (res.statusCode === 200) {
obj.cache_hit = false;
} else if (res.statusCode === 307) {
obj.cache_hit = true;
}
try {
const params = parsing.getImageParams(req);
for (let param in params) {
if (params.hasOwnProperty(param)) {
obj[param] = params[param];
}
}
} catch (e) {
log.log('info', `Could not extract image parameters, might not have been an image request: ${req.url}`);
}
}
log.log('debug', JSON.stringify(obj));
}

export default logRequest;
57 changes: 57 additions & 0 deletions src/Token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import uuid from "node-uuid";
import log from "./Log";

/*eslint-disable quotes*/
const insertToken = `INSERT INTO tokens (id, image_id, valid_until, used) VALUES ($1,$2,now() + interval '15 minute', 0)`;
const consumeToken = `UPDATE tokens SET used=1 WHERE id=$1 AND image_id=$2 AND valid_until >= now() AND used=0`;
const deleteOldTokens = `DELETE FROM tokens WHERE valid_until < datetime('now') AND used=0`;
/*eslint-enable*/

const Token = (db) => {
const module = {
consume(token, id, callback) {
db.query(consumeToken, [token, id], (err, res) => {
callback(err, res);
});
},
create(req, res) {
// Here we create a token which is valid for one single upload
// This way we can directly send the file here and just a small json payload to the app
const newToken = uuid.v4();
if (!req.body.id) {
res.writeHead(400, 'Bad request');
res.end();
return;
}
// Ensure the id wasnt requested or used previously
db.query(insertToken, [newToken, req.body.id], (err) => {
if (!err) {
res.json({token: newToken}).end();
log.log('info', 'Created token successfully');
if (module.shouldRunCleanup()) {
module.cleanup();
}
} else {
res.statusCode = 403;
res.json({error: 'The requested image_id is already requested'}).end();
}
});
},
shouldRunCleanup() {
return Math.floor(Math.random() * 10) === 0;
},
cleanup() {
log.log('info', 'Doing a token cleanup');
db.query(deleteOldTokens, [], (err) => {
if (!err) {
log.log('info', `Cleaned ${this.changes} tokens from the db`);
} else {
log.log('error', `Encountered error ${err} when cleaning up tokens`);
}
});
}
};
return module;
};

export default Token;
File renamed without changes.
Loading

0 comments on commit ade84e0

Please sign in to comment.