@zwehtetpaing55/uploader 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/config.js +9 -0
- package/index.js +8 -0
- package/package.json +18 -0
- package/uploader.js +52 -0
package/config.js
ADDED
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zwehtetpaing55/uploader",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [],
|
|
10
|
+
"author": "",
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"axios": "^1.15.0",
|
|
14
|
+
"express": "^5.2.1",
|
|
15
|
+
"form-data": "^4.0.5"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {}
|
|
18
|
+
}
|
package/uploader.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
const axios = require("axios");
|
|
2
|
+
const FormData = require("form-data");
|
|
3
|
+
const { getConfig } = require("./config");
|
|
4
|
+
|
|
5
|
+
exports.upload = async (file, folder = "") => {
|
|
6
|
+
const { baseURL } = getConfig();
|
|
7
|
+
|
|
8
|
+
if (!baseURL) {
|
|
9
|
+
throw new Error("baseURL is not set");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const form = new FormData();
|
|
13
|
+
|
|
14
|
+
form.append("file", file.buffer, file.originalname);
|
|
15
|
+
form.append("folder", folder);
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
const res = await axios.post(
|
|
19
|
+
`${baseURL}/upload`,
|
|
20
|
+
form,
|
|
21
|
+
{
|
|
22
|
+
headers: form.getHeaders(),
|
|
23
|
+
maxContentLength: Infinity,
|
|
24
|
+
maxBodyLength: Infinity
|
|
25
|
+
}
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
return res.data;
|
|
29
|
+
|
|
30
|
+
} catch (err) {
|
|
31
|
+
throw new Error("Upload failed: " + err.message);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
exports.deleteFile = async (public_id) => {
|
|
36
|
+
const { baseURL } = getConfig();
|
|
37
|
+
|
|
38
|
+
if (!baseURL) {
|
|
39
|
+
throw new Error("baseURL is not set");
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const parts = public_id.split('/');
|
|
43
|
+
const folder = parts.length > 1 ? parts[0] : '';
|
|
44
|
+
const id = parts.length > 1 ? parts[1] : parts[0];
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
await axios.delete(`${baseURL}/delete/${folder}/${id}`);
|
|
48
|
+
return { message: "Deleted" };
|
|
49
|
+
} catch (err) {
|
|
50
|
+
throw new Error("Delete failed: " + err.message);
|
|
51
|
+
}
|
|
52
|
+
};
|