-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
159 lines (158 loc) 路 4.76 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const rock = require("rock-req");
/**
* @description Constructor used to init the SDK
* @param {String} API_STORAGE_KEY Storage API KEY
* @param {String} API_STORAGE_ZONE Storage Zone
* @returns Return an object of functions to interact with the storage API
*/
module.exports = function BunnyCDNStorage(API_STORAGE_KEY, API_STORAGE_ZONE) {
if (!API_STORAGE_KEY || !API_STORAGE_ZONE) {
throw new Error(
"BunnyCDNStorage SDK: constructor error: storage key or storage zone missing.",
);
}
const _config = {
API_BASE_URL: "https://storage.bunnycdn.com",
API_STORAGE_ZONE,
};
const _httpHeader = {
AccessKey: API_STORAGE_KEY,
};
return {
/**
* @description Return a file as a buffer
* @param {String} path Path to the file
* @param {String} fileName File name
* @param {Function} cb [OPTIONAL] callback function with 3 arguments: (err, resp, data)
*/
getFile: function (path, fileName, cb = null) {
path = this.checkPathLastChar(path);
const _req = {
url: `${_config.API_BASE_URL}/${_config.API_STORAGE_ZONE}${path}/${fileName}`,
method: "GET",
headers: _httpHeader,
};
return this.request(_req, cb);
},
/**
* @description Return a list of file
* @param {String} path Path to a directory
* @param {Function} cb [OPTIONAL] callback function with 3 arguments: (err, resp, data)
*/
getFiles: function (path = "/", cb = null) {
const _req = {
url: `${_config.API_BASE_URL}/${_config.API_STORAGE_ZONE}${path}`,
method: "GET",
headers: {
AccessKey: API_STORAGE_KEY,
},
json: true,
};
return this.request(_req, cb);
},
/**
* @description Add or update a file
* @param {String} path path to access to the file
* @param {String} fileName file name
* @param {Buffer} buff Buffer of the file
* @param {Function} cb [OPTIONAL] callback function with 3 arguments: (err, resp, data)
*/
putFile: function (path, fileName, buff, cb = null) {
path = this.checkPathLastChar(path);
const _req = {
url: `${_config.API_BASE_URL}/${_config.API_STORAGE_ZONE}${path}/${fileName}`,
method: "PUT",
headers: {
AccessKey: API_STORAGE_KEY,
},
body: buff,
};
return this.request(_req, cb);
},
/**
* @description delete a file
* @param {String} path path to access to the file
* @param {String} fileName file name
* @param {Function} cb [OPTIONAL] callback function with 3 arguments: (err, resp, data)
*/
deleteFile: function (path, fileName = "", cb = null) {
path = this.checkPathLastChar(path);
const _req = {
url: `${_config.API_BASE_URL}/${_config.API_STORAGE_ZONE}${path}/${fileName}`,
method: "DELETE",
headers: {
AccessKey: API_STORAGE_KEY,
},
json: true,
};
return this.request(_req, cb);
},
/**
* @description Remove the last slash on a path
* @param {String} path path
*/
checkPathLastChar: function (path) {
if (path && path[path.length - 1] === "/") {
path = path.slice(0, -1);
}
return path;
},
/**
* @description Set the storage zone used to request the API.
* @param {String} API_STORAGE_ZONE
*/
setStorageZone: function (API_STORAGE_ZONE) {
_config.API_STORAGE_ZONE = API_STORAGE_ZONE;
},
/**
* @description Return the storage zone used to request the API.
*/
getStorageZone: function () {
return _config.API_STORAGE_ZONE;
},
/**
* @description Set the BASE URL used to request the API
* @param {String} API_BASE_URL
*/
setBaseURL: function (API_BASE_URL) {
_config.API_BASE_URL = API_BASE_URL;
},
/**
* @description Return the BASE URL used to request the API
*/
getBaseURL: function () {
return _config.API_BASE_URL;
},
/**
* @description Set the storage API key
* @param {String} API_STORAGE_KEY
*/
setStorageKey: function (API_STORAGE_KEY) {
_httpHeader.AccessKey = API_STORAGE_KEY;
},
/**
* @description Return the storage API KEY
*/
getStorageKey: function () {
return _httpHeader.AccessKey;
},
/**
* @description Function to make HTTP request
* @param {Object} opts
* @param {Function} cb
* @returns a callback function or a promise
*/
request: function (opts, cb) {
return cb
? rock(opts, cb)
: new Promise((resolve, reject) => {
rock(opts, function (err, res, data) {
if (err) {
return reject(err);
}
return resolve(data);
});
});
},
};
};