Skip to content

Commit

Permalink
fix(Resize API): Error when source images not in originals folder
Browse files Browse the repository at this point in the history
  • Loading branch information
danactive committed Jan 19, 2021
1 parent 57762ef commit 4bebbcf
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
3 changes: 1 addition & 2 deletions api/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
server/plugins/*/public/assets/**/*.js
server/plugins/*/public/lib/**/*.js
server/plugins/*/public/**/*.js
8 changes: 4 additions & 4 deletions api/server/plugins/resize/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ async function handler(request, h) {
const sourcePath = request.payload.source_path;

try {
const response = await resizeMod.resize(sourcePath);
if (dotProp.get(response, 'meta.error.count', 0) > 0) {
return h.response(response.meta.error).code(500);
const result = await resizeMod.resize(sourcePath);
if (dotProp.get(result, 'meta.error.count', 0) > 0) {
return h.response(result.meta.error).code(500);
}

// TODO danactive add property of photo and thumb output directories
console.log('Resize photo images to ', result.payload.paths.photos);
return { resize: true };
} catch (error) {
return error;
Expand Down
8 changes: 8 additions & 0 deletions api/server/plugins/resize/lib/resize.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,15 @@ function resize(sourcePath) {
safePath = utils.file.safePublicPath(sourcePath);
} catch (error) {
reject(boom.boomify(error));
return;
}

if (!safePath.includes('originals')) {
const error = boom.badRequest('Source folder must be in the "originals" folder with "photos" and "thumbs" as sibling folders');
reject(error);
return;
}

existsMod.pathExists(safePath)
.then(transformImages)
.catch((error) => reject(boom.boomify(error)));
Expand Down
24 changes: 24 additions & 0 deletions api/server/plugins/resize/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,30 @@ tape('Verify /resize route', { skip: false }, (describe) => {
},
};

try {
await server.register(plugins);
const response = await server.inject(request);

assert.equal(response.statusCode, 400, 'Expected status code');
} catch (error) {
assert.fail(error);
}

assert.end();
});

describe.test('* Caught fake source', { skip: false }, async (assert) => {
const server = hapi.Server({ port });
server.validator(joi);

const request = {
method: 'POST',
url: '/resize',
payload: {
source_path: 'originals/FAKE',
},
};

try {
await server.register(plugins);
const response = await server.inject(request);
Expand Down
12 changes: 11 additions & 1 deletion api/server/plugins/resize/test/resize.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,17 @@ tape('Verify resize library', { skip: false }, (describe) => {
plugin.resize('FAKE')
.then((response) => assert.fail(JSON.stringify(response)))
.catch((error) => {
assert.ok(error, 'Caught expected error');
const expected = 'Source folder must be in the "originals" folder with "photos" and "thumbs" as sibling folders';
assert.equal(error.message, expected, 'Caught expected error');
assert.end();
});
});

describe.test('* Catch fake source in originals folder', { skip: false }, (assert) => {
plugin.resize('originals/FAKE')
.then((response) => assert.fail(JSON.stringify(response)))
.catch((error) => {
assert.ok(error.message.includes('File system path is absolute and not found due to error'), 'Caught expected error');
assert.end();
});
});
Expand Down

0 comments on commit 4bebbcf

Please sign in to comment.