drop.chapril.org-firefoxsend/server/storage.js

234 lines
5.1 KiB
JavaScript
Raw Normal View History

const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const conf = require('./config.js');
const fs = require('fs');
const path = require('path');
const fetch = require('node-fetch');
const crypto = require('crypto');
2017-06-09 19:44:12 +02:00
const notLocalHost = conf.notLocalHost;
2017-06-08 22:45:28 +02:00
const mozlog = require('./log.js');
2017-06-09 19:44:12 +02:00
const log = mozlog('portal.storage');
2017-06-08 22:45:28 +02:00
const redis = require('redis');
2017-06-08 01:46:48 +02:00
const redis_client = redis.createClient({
host: conf.redis_host
});
redis_client.on('error', err => {
log.info('Redis:', err);
});
2017-06-08 22:45:28 +02:00
if (notLocalHost) {
module.exports = {
filename: filename,
exists: exists,
length: awsLength,
get: awsGet,
set: awsSet,
delete: awsDelete,
forceDelete: awsForceDelete
};
} else {
module.exports = {
filename: filename,
exists: exists,
length: localLength,
get: localGet,
set: localSet,
delete: localDelete,
forceDelete: localForceDelete
};
}
function filename(id) {
return new Promise((resolve, reject) => {
redis_client.hget(id, 'filename', (err, reply) => {
if (!err) {
resolve(reply);
} else {
reject();
}
});
});
}
function exists(id) {
2017-06-08 22:45:28 +02:00
return new Promise((resolve, reject) => {
redis_client.exists(id, (rediserr, reply) => {
2017-06-09 00:02:00 +02:00
resolve(reply === 1);
2017-06-08 22:45:28 +02:00
});
});
}
function localLength(id) {
return new Promise((resolve, reject) => {
try {
2017-06-09 19:44:12 +02:00
resolve(fs.statSync(path.join(__dirname, '../static', id)).size);
} catch (err) {
reject();
}
});
}
function localGet(id) {
2017-06-09 19:44:12 +02:00
return fs.createReadStream(path.join(__dirname, '../static', id));
}
function localSet(id, file, filename, url) {
return new Promise((resolve, reject) => {
2017-06-09 19:44:12 +02:00
const fstream = fs.createWriteStream(path.join(__dirname, '../static', id));
file.pipe(fstream);
fstream.on('close', () => {
2017-06-09 19:44:12 +02:00
const uuid = crypto.randomBytes(10).toString('hex');
redis_client.hmset([id, 'filename', filename, 'delete', uuid]);
redis_client.expire(id, 86400000);
2017-06-08 22:45:28 +02:00
log.info('localSet:', 'Upload Finished of ' + id);
resolve({
uuid: uuid,
url: url
});
});
fstream.on('error', () => {
log.error('localSet:', 'Failed upload of ' + id);
reject();
});
});
}
function localDelete(id, delete_token) {
return new Promise((resolve, reject) => {
redis_client.hget(id, 'delete', (err, reply) => {
if (!reply || delete_token !== reply) {
reject();
} else {
redis_client.del(id);
2017-06-09 19:44:12 +02:00
resolve(fs.unlinkSync(path.join(__dirname, '../static', id)));
}
});
});
}
function localForceDelete(id) {
return new Promise((resolve, reject) => {
redis_client.del(id);
2017-06-09 19:44:12 +02:00
resolve(fs.unlinkSync(path.join(__dirname, '../static', id)));
});
}
function awsLength(id) {
2017-06-09 19:44:12 +02:00
const params = {
Bucket: conf.s3_bucket,
Key: id
};
return new Promise((resolve, reject) => {
s3.headObject(params, function(err, data) {
if (!err) {
resolve(data.ContentLength);
} else {
reject();
}
});
});
}
function awsGet(id) {
2017-06-09 19:44:12 +02:00
const params = {
Bucket: conf.s3_bucket,
Key: id
};
2017-06-20 19:21:11 +02:00
try {
return s3.getObject(params).createReadStream();
} catch(err) {
log.info('GetFailed', 'Get Object from s3 failed.');
return null;
}
}
function awsSet(id, file, filename, url) {
2017-06-09 19:44:12 +02:00
const params = {
Bucket: conf.s3_bucket,
Key: id,
Body: file
};
return new Promise((resolve, reject) => {
2017-06-09 19:44:12 +02:00
s3.upload(params, function(err, _data) {
if (err) {
2017-06-08 22:45:28 +02:00
log.info('awsUploadError:', err.stack); // an error occurred
reject();
} else {
2017-06-09 19:44:12 +02:00
const uuid = crypto.randomBytes(10).toString('hex');
redis_client.hmset([id, 'filename', filename, 'delete', uuid]);
redis_client.expire(id, 86400000);
log.info('awsUploadFinish', 'Upload Finished of ' + filename);
if (conf.bitly_key) {
fetch(
'https://api-ssl.bitly.com/v3/shorten?access_token=' +
conf.bitly_key +
'&longUrl=' +
encodeURIComponent(url) +
'&format=txt'
)
.then(res => {
return res.text();
})
.then(body => {
resolve({
uuid: uuid,
url: body
});
});
} else {
resolve({
uuid: uuid,
url: url
});
}
}
});
});
}
function awsDelete(id, delete_token) {
return new Promise((resolve, reject) => {
redis_client.hget(id, 'delete', (err, reply) => {
if (!reply || delete_token !== reply) {
reject();
} else {
redis_client.del(id);
2017-06-09 19:44:12 +02:00
const params = {
Bucket: conf.s3_bucket,
Key: id
};
2017-06-09 19:44:12 +02:00
s3.deleteObject(params, function(err, _data) {
2017-06-19 22:37:56 +02:00
err ? reject(err) : resolve(err);
});
}
});
});
}
function awsForceDelete(id) {
return new Promise((resolve, reject) => {
redis_client.del(id);
2017-06-09 19:44:12 +02:00
const params = {
Bucket: conf.s3_bucket,
Key: id
};
2017-06-09 19:44:12 +02:00
s3.deleteObject(params, function(err, _data) {
2017-06-19 22:37:56 +02:00
err ? reject(err) : resolve(err);
});
});
}