Skip to content

Commit 5a1421f

Browse files
try to harden image seeding
1 parent 950ccf9 commit 5a1421f

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"homepage": "https://human-connection.org",
66
"license": "CC-BY-NC-SA-4.0",
77
"repository": {
8-
"type" : "git",
9-
"url" : "https://github.com/Human-Connection/API.git"
8+
"type": "git",
9+
"url": "https://github.com/Human-Connection/API.git"
1010
},
1111
"main": "server",
1212
"keywords": [
@@ -21,8 +21,8 @@
2121
},
2222
"contributors": [],
2323
"bugs": {
24-
"url" : "https://github.com/Human-Connection/API/issues",
25-
"email" : "[email protected]"
24+
"url": "https://github.com/Human-Connection/API/issues",
25+
"email": "[email protected]"
2626
},
2727
"directories": {
2828
"lib": "server",
@@ -78,6 +78,7 @@
7878
"handlebars-layouts": "~3.1.4",
7979
"helmet": "~3.10.0",
8080
"html-excerpt": "~0.1.0",
81+
"mime": "^2.2.0",
8182
"mongoose": "~4.13.2",
8283
"multer": "~1.3.0",
8384
"node-sass": "~4.7.2",

server/hooks/save-remote-images.js

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ const fs = require('fs');
66
const path = require('path');
77
const request = require('request');
88
const faker = require('faker');
9+
const mime = require('mime/lite');
910

1011
module.exports = function (options = []) { // eslint-disable-line no-unused-vars
1112
return function async (hook) {
1213

13-
return new Promise((resolve) => {
14+
return new Promise((resolve, reject) => {
1415

1516
let urls = [];
1617

@@ -35,25 +36,46 @@ module.exports = function (options = []) { // eslint-disable-line no-unused-vars
3536
imgCount++;
3637
// TODO: fix that to use the data _id or somethink similar
3738
let uuid = faker.fake('{{random.uuid}}');
38-
const imgName = `${field}_${uuid}.jpg`;
39-
const imgPath = path.resolve('public', 'uploads/' + imgName);
40-
let stream = fs.createWriteStream(imgPath);
41-
urls.push(imgPath);
42-
stream.on('close', () => {
43-
if (--loading <= 0) {
44-
hook.app.debug('Download(s) finished', imgName);
45-
resolve(hook);
46-
} else {
47-
hook.app.debug('Download finished', imgName);
39+
const imgName = `${field}_${uuid}`;
40+
let imgPath = path.resolve('public', 'uploads/' + imgName);
41+
42+
request({
43+
url: hook.data[field],
44+
encoding: null
45+
}, (err, res, body) => {
46+
if (err) {
47+
hook.app.error(err);
48+
reject(err);
49+
}
50+
try {
51+
const mimeType = res.headers['content-type'];
52+
if (mimeType.indexOf('image') !== 0) {
53+
hook.app.error('its not an image');
54+
reject('its not an image');
55+
}
56+
57+
const ext = mime.getExtension(mimeType);
58+
59+
imgPath += `.${ext}`;
60+
61+
fs.writeFileSync(imgPath, body, {
62+
encoding: 'binary'
63+
});
64+
65+
loading--;
66+
67+
hook.data[field] = uploadsUrl + imgName + `.${ext}`;
68+
69+
if (imgCount > 0 && loading <= 0) {
70+
hook.app.debug('Download(s) finished', urls);
71+
resolve(hook);
72+
}
73+
} catch (err) {
74+
hook.app.error(err);
4875
}
4976
});
50-
stream.on('error', (err) => {
51-
// reject(err);
52-
throw new errors.Unprocessable('Thumbnail download failed', { errors: err, urls: urls });
53-
});
77+
5478
hook.app.debug('Downloading', hook.data[field]);
55-
request(hook.data[field]).pipe(stream);
56-
hook.data[field] = uploadsUrl + imgName;
5779
});
5880

5981
if (imgCount > 0 && loading <= 0) {

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3143,6 +3143,10 @@ [email protected]:
31433143
version "1.4.1"
31443144
resolved "https://registry.yarnpkg.com/mime/-/mime-1.4.1.tgz#121f9ebc49e3766f311a76e1fa1c8003c4b03aa6"
31453145

3146+
mime@^2.2.0:
3147+
version "2.2.0"
3148+
resolved "https://registry.yarnpkg.com/mime/-/mime-2.2.0.tgz#161e541965551d3b549fa1114391e3a3d55b923b"
3149+
31463150
mimer@^0.2.1:
31473151
version "0.2.3"
31483152
resolved "https://registry.yarnpkg.com/mimer/-/mimer-0.2.3.tgz#8808c0d03fb2b1273b81ae25a6e52e04ce18dce4"

0 commit comments

Comments
 (0)