-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (56 loc) · 1.55 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
'use strict'
let popsicle = require('popsicle');
var S3Client = module.exports = function(config) {
this.host = config.host;
this.signerUrl = config.signerUrl;
this.bucket = config.bucket;
}
S3Client.prototype._upload = function(signed_url, public_url, id, config) {
const type = config.contentType || "application/octet-stream"
, self = this
, req = popsicle.request({
url: signed_url,
method: 'PUT',
headers: {
'content-type': type,
'x-amz-acl': 'public-read',
'content-length': config.filesize
},
body: config.file,
})
;
req.then(res => {
if(res.status == 200) config.complete(id, public_url);
else config.error(res);
});
req.catch(err => config.error(err));
req.progress(() => {
if(config.progress) config.progress(Math.round(req.uploaded));
});
}
// adds a file. Only important function for the end user
S3Client.prototype.add = function(config) {
const self = this
;
let object_name = config.name
, send = {
endpoint: "files/",
method: 'post',
headers: {
"Content-Type": 'application/json',
},
};
send.url = this.signerUrl;
send.payload = {
bucket: this.bucket,
path: object_name,
mime_type: config.contentType
};
popsicle.request(send).then(function(res) {
if (res.code >= 400) { config.error(res); return; }
const result = res.body.s3
, public_url = result.url.split("?")[0]
;
self._upload(result.url, public_url, res.body.id, config);
})
}