Skip to content

Commit

Permalink
Final stuff before moving to IAAS
Browse files Browse the repository at this point in the history
  • Loading branch information
rogierslag committed Jul 3, 2016
1 parent 0a6b236 commit 2fe48f8
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ engines:

ratings:
paths:
- "**.js"
- "src/**.js"

# ratings:
# paths:
Expand Down
26 changes: 13 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ RUN apt-get update && \
apt-get autoremove -y && \
apt-get clean

RUN npm install -g pm2 babel-cli babel-preset-es2015
RUN npm install -g pm2 babel-cli babel-preset-es2015 babel-preset-stage-3

# Export the database, originals dir and the config dir
RUN mkdir /opt/live-image-resize
RUN mkdir /opt/live-image-resize/migrations
RUN mkdir /opt/live-image-resize/config
RUN mkdir /opt/iaas
RUN mkdir /opt/iaas/migrations
RUN mkdir /opt/iaas/config
RUN mkdir /opt/images
VOLUME ["/opt/images", "/opt/live-image-resize/config"]
VOLUME ["/opt/images", "/opt/iaas/config"]

EXPOSE 1337

# Add the dependencies
ADD .babelrc /opt/live-image-resize/
ADD package.json /opt/live-image-resize/package.json
RUN cd /opt/live-image-resize && npm install
ADD .babelrc /opt/iaas/
ADD package.json /opt/iaas/package.json
RUN cd /opt/iaas && npm install

# Add the application
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
ADD src/*.js /opt/iaas/src/
ADD migrations /opt/iaas/migrations/
RUN cd /opt/iaas/src && babel -d ../ *.js

# Run the entire thing!
WORKDIR /opt/live-image-resize
CMD ["/usr/local/bin/pm2", "start", "index.js", "--no-daemon", "--instances=2"]
WORKDIR /opt/iaas
CMD ["/usr/local/bin/pm2", "start", "index.js", "--no-daemon", "--instances=1"]
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ On OSX the Docker Toolbox suffices.
Quick way to send images (ensure you have `jq` installed)
```bash
IMAGE=test1234567
RES=`curl -vvv -XPOST http://192.168.99.100:1337/token -d "{\"id\": \"${IMAGE}\"}" -H "Content-Type: application/json"`
PORT=1337
RES=`curl -vvv -XPOST http://localhost:$PORT/token -d "{\"id\": \"${IMAGE}\"}" -H "Content-Type: application/json"`
TOKEN=`echo $RES | jq -r .token`
curl -vvv -XPOST http://192.168.99.100:1337/${IMAGE} -H "X-Token: ${TOKEN}" -F "image=@/Users/Rogier/Downloads/878261728-5f264338.jpg"
curl -vvv -XPOST http://localhost:$PORT/${IMAGE} -H "X-Token: ${TOKEN}" -F "image=@/Users/Rogier/Downloads/IMG_7419.PNG"
```

## Contributing
Expand Down
4 changes: 4 additions & 0 deletions migrations/20160627164931.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE images ADD COLUMN blur boolean DEFAULT 'f';
DROP INDEX unique_image;

CREATE UNIQUE INDEX unique_image ON images(id,x,y,fit,file_type,blur);
4 changes: 2 additions & 2 deletions src/aws.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ AWS.config.update({
});
const S3 = new AWS.S3();

const uploadPromise = (client, params) => {
const upload = (client, params) => {
return new Promise((resolve, reject) => client.putObject(params, err => err ? reject(err) : resolve()));
};

Expand All @@ -27,7 +27,7 @@ export default (cache) => async(name, params, data) => {
CacheControl: 'public'
};
try {
await uploadPromise(S3, uploadParams);
await upload(S3, uploadParams);
} catch (e) {
log('error', `AWS upload error: ${JSON.stringify(e)}`);
return;
Expand Down
8 changes: 4 additions & 4 deletions src/dbCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default (db) => {
return new Promise((resolve, reject) => db.query(query, vars, (err, data) => err ? reject(err) : resolve(data)));
};
return {
getFromCache: async(params) => {
async getFromCache (params) {
const vars = [params.name,
params.width,
params.height,
Expand All @@ -24,11 +24,11 @@ export default (db) => {
// Cache miss
return null;
} catch (e) {
console.log(e.stack);
console.error(e.stack);
return null;
}
},
addToCache: async(params, url) => {
async addToCache (params, url) {
const vars = [params.name,
params.width,
params.height,
Expand All @@ -42,7 +42,7 @@ export default (db) => {
const result = await promiseQuery(insertImage, vars);
return result.rowCount === 1;
} catch (e) {
console.log(e.stack);
console.error(e.stack);
return false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export function futureDate() {
return cacheDate;
}

export function isNotUndefined(values) {
export function areAllDefined(values) {
const reducer = (val, e) => val && e !== undefined;
return values.reduce(reducer, true);
}
}
26 changes: 15 additions & 11 deletions src/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import uuid from "uuid";

const im = gm.subClass({imageMagick: true});

const clearTempfilesTimeout = 60000; // 1 minute

// Wrap these calls in promises so we can use async/await
const promiseWrite = (client, file) => {
const write = (client, file) => {
return new Promise((resolve, reject) => client.write(file, err => err ? reject(err) : resolve()));
};
const promiseSize = (client) => {
const size = (client) => {
return new Promise((resolve, reject) => client.size((err, data) => err ? reject(err) : resolve(data)));
};

Expand All @@ -31,11 +33,12 @@ const crop = async(client, params) => {
client = client.resize(params.width, params.height, '^');
const tmpFile = `/tmp/${uuid.v4()}`;
try {
await promiseWrite(client, tmpFile);
await write(client, tmpFile);
client = im(tmpFile);
const size = await promiseSize(client);
const imgSize = await size(client);
setTimeout(() => fs.unlink(tmpFile), clearTempfilesTimeout);
return client
.repage(size.width, size.height, 0, 0)
.repage(imgSize.width, imgSize.height, 0, 0)
.gravity('Center')
// Crop the image to the exact size (the ! indicates a force)
// This is ok since we first resized appropriately
Expand All @@ -44,6 +47,7 @@ const crop = async(client, params) => {
catch (e) {
log('error', 'could not write tempfile');
console.error(e.stack);
throw e;
}
};

Expand Down Expand Up @@ -92,23 +96,23 @@ export default {
// if possible, crop first (since the UA had that orientation), then orient
if (cropParameters) {
const cropped = im(source).crop(cropParameters.width, cropParameters.height, cropParameters.xOffset, cropParameters.yOffset);
await promiseWrite(cropped, source);
await write(cropped, source);
}

const oriented = im(source).autoOrient();

try {
await promiseWrite(oriented, destination);
await write(oriented, destination);
} catch (e) {
console.log(e.stack);
return;
throw e;
}

try {
const size = await promiseSize(im(destination));
const imgSize = await size(im(destination));
return {
originalHeight: size.height || null,
originalWidth: size.width || null
originalHeight: imgSize.height || null,
originalWidth: imgSize.width || null
};
} catch (e) {
console.log(e.stack);
Expand Down
11 changes: 6 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import log from "./log";
import urlParameters from "./urlParameters";
import imageResponse from "./imageResponse";
import token from "./token";
import {isNotUndefined} from "./helper";
import {areAllDefined} from "./helper";

let db;
const connectionString = `postgres://${config.get('postgresql.user')}:${config.get('postgresql.password')}@${config.get('postgresql.host')}/${config.get('postgresql.database')}`; //eslint-disable-line max-len

process.on('uncaughtException', function (err) {
console.log(err);
console.error(err);
process.exit(1);
});

const promiseUpload = (form, request) => {
Expand All @@ -26,7 +27,7 @@ const cropParametersOnUpload = (req) => {
const yOffset = Number(req.query.y) || undefined;
const width = Number(req.query.width) || undefined;
const height = Number(req.query.height) || undefined;
if (isNotUndefined([xOffset, yOffset, width, height])) {
if (areAllDefined([xOffset, yOffset, width, height])) {
return {
xOffset, yOffset, width, height
};
Expand Down Expand Up @@ -57,7 +58,7 @@ const uploadImage = async(req, res) => {
status: 'OK',
id: name,
original_height: result.originalHeight,
original_width: result.originalWidth,
original_width: result.originalWidth
});
};

Expand Down Expand Up @@ -101,7 +102,7 @@ server.get('/(:name)_(:width)_(:height).(:format)', (req, res) => {
});
server.get('/(:name).(:format)', (req, res) => {
// Serve the original
const params = urlParameters(req);
const params = urlParameters(req, false);
imageResponse.original(db, params, req.method, res);
});

Expand Down
14 changes: 7 additions & 7 deletions src/urlParameters.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {isNotUndefined} from "./helper";
import {areAllDefined} from "./helper";

const BLUR_RADIUS = 15;
const BLUR_SIGMA = 7;
const BLUR_RADIUS = Number(process.env.BLUR_RADIUS) || 15;
const BLUR_SIGMA = Number(process.env.BLUR_SIGMA) || 7;

const ALLOWED_TYPES = ['jpg', 'jpeg', 'jfif', 'jpe', 'png'];
const ALLOWED_FITS = ['clip', 'crop', 'canvas'];
Expand All @@ -20,7 +20,7 @@ const getMimeFromExtension = (extension) => {
}
};

export default (req) => {
export default (req, requireDimensions = true) => {
// The default settings
const result = {
name: null,
Expand All @@ -35,8 +35,8 @@ export default (req) => {
// Extract data
result.name = req.params.name;
const scale = Number(req.params.scale) || 1;
result.width = Number(req.params.width) * scale;
result.height = Number(req.params.height) * scale;
result.width = Number(req.params.width) * scale || undefined;
result.height = Number(req.params.height) * scale || undefined;

if (ALLOWED_TYPES.includes(req.params.format.toLowerCase())) {
result.type = req.params.format.toLowerCase();
Expand All @@ -53,7 +53,7 @@ export default (req) => {
}

// Check if the minimum is set
if (!result.name || isNotUndefined([result.width, result.height])) {
if (!result.name || (!requireDimensions && areAllDefined([result.width, result.height]))) {
console.warn(result, req.params, req.query);
return null;
}
Expand Down

0 comments on commit 2fe48f8

Please sign in to comment.