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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
require('dotenv').config()
var knox = require('knox')
var uuid = require('uuid/v1')
var s3;
var acceptableuploadTypes = {
'image/gif': 'gif',
'image/jpeg': 'jpg',
'image/jpg': 'jpg',
'image/png': 'png',
}
var typesByExtension = {
// audio
'aiff': 'audio/aiff',
'aif': 'audio/aiff',
'mp3': 'audio/mp3',
'wav': 'audio/wav',
'flac': 'audio/flac',
'ogg': 'audio/ogg',
'opus': 'audio/ogg',
// video
'mp4': 'video/mp4',
'webm': 'video/webm',
'ogv': 'video/ogg',
'mov': 'video/quicktime',
'avi': 'video/x-msvideo',
'wmv': 'video/x-ms-wmv',
// image
'gif': 'image/gif',
'jpeg': 'image/jpg',
'jpg': 'image/jpg',
'png': 'image/png',
'bmp': 'image/bmp',
'tiff': 'image/tif',
'tif': 'image/tif',
// misc
'zip': 'application/zip',
}
module.exports = {}
module.exports.init = function(){
s3 = knox.createClient({
key: process.env.S3_KEY,
secret: process.env.S3_SECRET,
bucket: process.env.S3_BUCKET,
})
}
module.exports.client = function(){
return s3
}
module.exports.put = function (opt) {
var filename
var err
var now = new Date()
var file = opt.file
var mimetype = file.mimetype
var types = opt.types
var extension = types && types[file.mimetype]
if (opt.filename) {
filename = opt.filename
} else if (opt.preserveFilename) {
filename = file.originalname
if (mimetype.indexOf('application') !== -1) {
var fpartz = file.originalname.split('.')
extension = fpartz.pop()
if (extension in typesByExtension) {
mimetype = typesByExtension[extension]
console.error('storing', extension, 'as', mimetype)
} else {
console.error('unknown extension', extension, 'storing as', mimetype)
}
}
} else {
filename = uuid() + "." + extension;
}
if (opt.sanitizeFilename) {
filename = filename.replace(/-/g,'_').replace(/\s/g,'_').replace(/_+/g,'_')
}
var remote_path = opt.dirname + filename
if (types && ! extension) {
err = "Unacceptable filetype."
}
else if (opt.maxSize && file.size > opt.maxSize) {
err = "File too large. Uploads can be a maximum of " + opt.maxSize + " bytes."
}
if (err) {
console.error(">>>", err)
opt.unacceptable && opt.unacceptable(err)
return
}
opt.acceptable && opt.acceptable(err)
console.log("upload >", remote_path)
s3.putBuffer(file.buffer, remote_path, {
'Content-Length': file.size,
'Content-Type': mimetype,
'x-amz-acl': 'public-read'
}, function(err, s3res) {
if (err || s3res.statusCode !== 200) {
console.error(err);
if (s3res && s3res.resume) {
s3res.resume()
}
return;
}
var file_url = s3res.url || s3res.req.url
opt.success && opt.success(file_url, filename)
}).on('error', function(err, s3res){
console.error(err)
s3res && s3res.resume && s3res.resume()
})
}
module.exports.destroyFile = function(file){
const file_url = file.get('url')
const s3_path = process.env.S3_PATH
const s3_path_index = file_url.indexOf(s3_path)
if (file_url.match(/^https?:\/\/s3.amazonaws.com\//) && s3_path_index === -1) return
const file_path = file_url.substr(s3_path_index)
console.log('delete', file_path)
s3.deleteFile(file_path, function(err, res){
if (err) {
console.error('error deleting file', err)
} else {
console.log('file deleted!')
}
// check `err`, then do `res.pipe(..)` or `res.resume()` or whatever.
})
}
|