-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (45 loc) · 1.35 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
const request = require('request')
const concat = require('concat-stream')
function UploadcareStorage (opts) {
this.options = {
public_key: null,
private_key: null,
store: 'auto'
}
Object.assign(this.options, opts)
}
UploadcareStorage.prototype._handleFile = function _handleFile (req, file, cb) {
file.stream.pipe(concat((fileBuffer) => {
request.post({
url: 'https://upload.uploadcare.com/base/',
json: true,
formData: {
UPLOADCARE_PUB_KEY: this.options.public_key,
UPLOADCARE_STORE: this.options.store,
file: {
value: fileBuffer,
options: {
filename: file.originalname,
contentType: file.mimetype
}
}
}
}, function _createCallback (err, httpResponse, body) {
if (err) return cb(err)
return cb(null, { uploadcare_file_id: body.file })
})
}))
}
UploadcareStorage.prototype._removeFile = function _removeFile (req, file, cb) {
request.delete({
url: `https://api.uploadcare.com/files/${file.uploadcare_file_id}`,
Authorization: `Uploadcare.Simple ${this.options.public_key}:${this.options.private_key}`,
json: true
}, function _deleteCallback (err, httpResponse, body) {
if (err) return cb(err)
return cb(null, body)
})
}
module.exports = function (opts) {
return new UploadcareStorage(opts)
}