forked from dustin-H/multer-storage-pkgcloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (42 loc) · 1.3 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
function PkgcloudStorage (opts) {
this.client = opts.client
this.getDestination = opts.destination || getDestination
this.transformFile = opts.transform || function (originalFile, cb) {cb(originalFile)}
function getDestination (req, file, cb) {
cb(null, {
container: opts.container || 'uploads',
remote: file.originalname
})
}
}
PkgcloudStorage.prototype._handleFile = function _handleFile (req, originalFile, cb) {
var that = this
// Apply an image transform and
// Change destination if needed
this.transformFile(originalFile, function (file) {
that.getDestination(req, file, function (err, dest) {
if (err) return cb(err)
var params = {
container: dest.container,
remote: dest.remote,
contentType: file.mimetype
}
var outStream = that.client.upload(params)
file.stream.pipe(outStream)
outStream.on('error', cb)
outStream.on('success', function (info) {
cb(null, {
size: info.size,
container: params.container,
remote: params.remote
})
})
})
})
}
PkgcloudStorage.prototype._removeFile = function _removeFile (req, file, cb) {
this.client.removeFile(file.container, file.remote, cb)
}
module.exports = function (opts) {
return new PkgcloudStorage(opts)
}