fixes #543 added FILE_DIR environment variable

This commit is contained in:
Danny Coates 2017-09-06 13:25:27 -07:00
parent bbaeb44b26
commit 7a8e9b5de1
No known key found for this signature in database
GPG Key ID: 4C442633C62E00CB
4 changed files with 19 additions and 12 deletions

4
package-lock.json generated
View File

@ -6813,7 +6813,6 @@
"version": "0.5.1", "version": "0.5.1",
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz",
"integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=",
"dev": true,
"requires": { "requires": {
"minimist": "0.0.8" "minimist": "0.0.8"
}, },
@ -6821,8 +6820,7 @@
"minimist": { "minimist": {
"version": "0.0.8", "version": "0.0.8",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz",
"integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0="
"dev": true
} }
} }
}, },

View File

@ -102,6 +102,7 @@
"fluent": "^0.4.1", "fluent": "^0.4.1",
"fluent-langneg": "^0.1.0", "fluent-langneg": "^0.1.0",
"helmet": "^3.8.1", "helmet": "^3.8.1",
"mkdirp": "^0.5.1",
"mozlog": "^2.1.1", "mozlog": "^2.1.1",
"raven": "^2.1.2", "raven": "^2.1.2",
"redis": "^2.8.0" "redis": "^2.8.0"

View File

@ -1,4 +1,7 @@
const convict = require('convict'); const convict = require('convict');
const { tmpdir } = require('os');
const path = require('path');
const { randomBytes } = require('crypto');
const conf = convict({ const conf = convict({
s3_bucket: { s3_bucket: {
@ -56,6 +59,11 @@ const conf = convict({
format: 'url', format: 'url',
default: 'https://send.firefox.com', default: 'https://send.firefox.com',
env: 'BASE_URL' env: 'BASE_URL'
},
file_dir: {
format: 'String',
default: `${tmpdir()}${path.sep}send-${randomBytes(4).toString('hex')}`,
env: 'FILE_DIR'
} }
}); });

View File

@ -1,8 +1,8 @@
const AWS = require('aws-sdk'); const AWS = require('aws-sdk');
const s3 = new AWS.S3(); const s3 = new AWS.S3();
const mkdirp = require('mkdirp');
const config = require('./config'); const config = require('./config');
const { tmpdir } = require('os');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
@ -25,7 +25,7 @@ redis_client.on('error', err => {
log.error('Redis:', err); log.error('Redis:', err);
}); });
let tempDir = null; const fileDir = config.file_dir;
if (config.s3_bucket) { if (config.s3_bucket) {
module.exports = { module.exports = {
@ -44,8 +44,8 @@ if (config.s3_bucket) {
metadata metadata
}; };
} else { } else {
tempDir = fs.mkdtempSync(`${tmpdir()}${path.sep}send-`); mkdirp.sync(config.file_dir);
log.info('tempDir', tempDir); log.info('fileDir', fileDir);
module.exports = { module.exports = {
filename: filename, filename: filename,
exists: exists, exists: exists,
@ -123,7 +123,7 @@ function setField(id, key, value) {
function localLength(id) { function localLength(id) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { try {
resolve(fs.statSync(path.join(tempDir, id)).size); resolve(fs.statSync(path.join(fileDir, id)).size);
} catch (err) { } catch (err) {
reject(); reject();
} }
@ -131,12 +131,12 @@ function localLength(id) {
} }
function localGet(id) { function localGet(id) {
return fs.createReadStream(path.join(tempDir, id)); return fs.createReadStream(path.join(fileDir, id));
} }
function localSet(newId, file, filename, meta) { function localSet(newId, file, filename, meta) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const filepath = path.join(tempDir, newId); const filepath = path.join(fileDir, newId);
const fstream = fs.createWriteStream(filepath); const fstream = fs.createWriteStream(filepath);
file.pipe(fstream); file.pipe(fstream);
file.on('limit', () => { file.on('limit', () => {
@ -166,7 +166,7 @@ function localDelete(id, delete_token) {
} else { } else {
redis_client.del(id); redis_client.del(id);
log.info('Deleted:', id); log.info('Deleted:', id);
resolve(fs.unlinkSync(path.join(tempDir, id))); resolve(fs.unlinkSync(path.join(fileDir, id)));
} }
}); });
}); });
@ -175,7 +175,7 @@ function localDelete(id, delete_token) {
function localForceDelete(id) { function localForceDelete(id) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
redis_client.del(id); redis_client.del(id);
resolve(fs.unlinkSync(path.join(tempDir, id))); resolve(fs.unlinkSync(path.join(fileDir, id)));
}); });
} }