This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (52 loc) · 1.48 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
"use strict";
const AWS = require("aws-sdk");
module.exports = {
init(config) {
const S3 = new AWS.S3({
apiVersion: "2006-03-01",
...config,
});
const prefix = config.prefix ? `${config.prefix}/` : ""; // Custom
return {
upload(file, customParams = {}) {
return new Promise((resolve, reject) => {
const path = file.path ? `${file.path}/` : ""; // Coming from file obj from admin client
let params = {
Key: `${prefix}${path}${file.hash}${file.ext}`,
Body: Buffer.from(file.buffer, "binary"),
ContentType: file.mime,
ACL: "public-read",
...customParams,
};
S3.upload(params, (err, data) => {
if (err) {
return reject(err);
}
file.url = config.cdnUrl
? `${config.cdnUrl}/${data.Key}`
: data.Location;
resolve();
});
});
},
delete(file, customParams = {}) {
return new Promise((resolve, reject) => {
// delete file on S3 bucket
const path = file.path ? `${file.path}/` : "";
S3.deleteObject(
{
Key: `${prefix}${path}${file.hash}${file.ext}`,
...customParams,
},
(err, data) => {
if (err) {
return reject(err);
}
resolve();
}
);
});
},
};
},
};