-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
94 lines (75 loc) · 2.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
'use strict';
// # S3 storage module for Ghost blog http://ghost.org/
var fs = require('fs');
var path = require('path');
var nodefn = require('when/node/function');
var when = require('when');
var readFile = nodefn.lift(fs.readFile);
var unlink = nodefn.lift(fs.unlink);
var AWS = require('aws-sdk');
var config;
module.exports = function(options) {
options = options || {};
if (options.config) {
config = options.config;
AWS.config.update(config);
}
if (options.errors) errors = options.errors;
return module.exports;
};
// ### Save
// Saves the image to S3
// - image is the express image object
// - returns a promise which ultimately returns the full url to the uploaded image
module.exports.save = function(image) {
if (!config) return when.reject('ghost-s3 is not configured');
var targetDir = getTargetDir();
var targetFilename = getTargetName(image, targetDir);
var awsPath = 'https://' + config.bucket + '.s3.amazonaws.com/';
return readFile(image.path)
.then(function(buffer) {
var s3 = new AWS.S3();
return nodefn.call(s3.putObject.bind(s3), {
Bucket: config.bucket,
Key: targetFilename,
Body: buffer,
ContentType: image.type,
CacheControl: 'maxage=' + (30 * 24 * 60 * 60) // 30 days
});
})
.then(function(result) {
return unlink(image.path);
})
.then(function() {
return when.resolve(awsPath + targetFilename);
})
.catch(function(err) {
unlink(image.path);
errors.logError(err);
throw err;
});
};
// middleware for serving the files
module.exports.serve = function() {
// a no-op, these are absolute URLs
return function(){};
};
var MONTHS = [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
];
var getTargetDir = function() {
var now = new Date();
return path.join(now.getFullYear() + '', MONTHS[now.getMonth()]) + '/';
};
var getTargetName = function(image, targetDir) {
var ext = path.extname(image.name),
name = path.basename(image.name, ext).replace(/\W/g, '_');
return targetDir + name + '-' + Date.now() + ext;
};
// default error handler
var errors = {
logError: function(error) {
console.log('error in ghost-s3', error);
}
};