-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
47 lines (38 loc) · 1.08 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
function PkgcloudStorage (opts) {
this.client = opts.client
this.getDestination = opts.destination || getDestination
function getDestination (req, file, cb) {
cb(null, {
container: opts.container || 'uploads',
remote: file.originalname
})
}
}
PkgcloudStorage.prototype._handleFile = function _handleFile (req, file, cb) {
var that = this
// Change destination if needed
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)
}