summaryrefslogtreecommitdiff
path: root/app.js
diff options
context:
space:
mode:
authorMax Wu2016-11-27 10:54:00 +0800
committerGitHub2016-11-27 10:54:00 +0800
commitbd3d4958e43a8437e45113354059cf41dacda995 (patch)
tree8b1b08557a7caa516609dbb1f0b1a82d0b47d92a /app.js
parent76a6190dacae691968dc3baa7dd86696ac7bbc1e (diff)
parent1a4f3950e6d03797beb79c63a3e6f071d8298c17 (diff)
Merge pull request #248 from hackmdio/file-upload-options
Support other options for image uploading
Diffstat (limited to 'app.js')
-rw-r--r--app.js105
1 files changed, 87 insertions, 18 deletions
diff --git a/app.js b/app.js
index ea745dfb..da4278a1 100644
--- a/app.js
+++ b/app.js
@@ -406,33 +406,102 @@ app.get('/me', function (req, res) {
});
}
});
-//upload to imgur
+
+//upload image
app.post('/uploadimage', function (req, res) {
var form = new formidable.IncomingForm();
+
+ form.keepExtensions = true;
+
+ if (config.imageUploadType === 'filesystem') {
+ form.uploadDir = "public/uploads";
+ }
+
+ function preprocessImage(path) {
+ return new Promise((resolve) => {
+ var oldFile = `${path}-old`;
+ fs.rename(path, oldFile, function() {
+ var sharp = require('sharp');
+ sharp(oldFile).toFile(path).then(() => {
+ fs.unlink(oldFile, function() {
+ resolve(path);
+ })
+ });
+ });
+ });
+ }
+
form.parse(req, function (err, fields, files) {
if (err || !files.image || !files.image.path) {
response.errorForbidden(res);
} else {
- if (config.debug)
- logger.info('SERVER received uploadimage: ' + JSON.stringify(files.image));
- imgur.setClientId(config.imgur.clientID);
- try {
- imgur.uploadFile(files.image.path)
- .then(function (json) {
- if (config.debug)
- logger.info('SERVER uploadimage success: ' + JSON.stringify(json));
+ preprocessImage(files.image.path).then(() => {
+ if (config.debug)
+ logger.info('SERVER received uploadimage: ' + JSON.stringify(files.image));
+
+ var path = require('path');
+ try {
+ switch (config.imageUploadType) {
+ case 'filesystem':
res.send({
- link: json.data.link.replace(/^http:\/\//i, 'https://')
+ link: path.join(config.serverurl, files.image.path.match(/^public(.+$)/)[1])
});
- })
- .catch(function (err) {
- logger.error(err);
- return res.status(500).end('upload image error');
- });
- } catch (err) {
+
+ break;
+
+ case 's3':
+ var AWS = require('aws-sdk');
+ var awsConfig = new AWS.Config(config.s3);
+ var s3 = new AWS.S3(awsConfig);
+
+ fs.readFile(files.image.path, function (err, buffer) {
+ var params = {
+ Bucket: 'hackmd',
+ Key: path.join('uploads', path.basename(files.image.path)),
+ Body: buffer
+ };
+
+ s3.putObject(params, function (err, data) {
+ if (err) {
+ logger.error(err);
+ res.status(500).end('upload image error');
+ } else {
+ res.send({
+ link: `https://s3-${config.s3.region}.amazonaws.com/${config.s3bucket}/${params.Key}`
+ });
+ }
+ });
+
+ });
+
+ break;
+
+ case 'imgur':
+ default:
+ imgur.setClientId(config.imgur.clientID);
+ imgur.uploadFile(files.image.path)
+ .then(function (json) {
+ if (config.debug)
+ logger.info('SERVER uploadimage success: ' + JSON.stringify(json));
+ res.send({
+ link: json.data.link.replace(/^http:\/\//i, 'https://')
+ });
+ })
+ .catch(function (err) {
+ logger.error(err);
+ return res.status(500).end('upload image error');
+ });
+ break;
+ }
+ } catch (err) {
+ logger.error(err);
+ return res.status(500).end('upload image error');
+ }
+
+ }).catch((err) => {
logger.error(err);
- return res.status(500).end('upload image error');
- }
+ return res.status(500).end('process image error');
+ });
}
});
});