-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
86 lines (82 loc) · 2.83 KB
/
index.js
File metadata and controls
86 lines (82 loc) · 2.83 KB
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
let uurl = "https://files.co.pl/api/v1/upload"
let durl = "https://files.co.pl/api/v1/delete"
const fetch = require('node-fetch');
const fs = require('fs');
const FormData = require('form-data');
var argv = require('minimist')(process.argv.slice(2));
var token = argv.t
var file = argv.f
var del = argv.d
var upload = argv.u
if (token) {
if (upload) {
fs.stat(`./${file}`, function (err, stats) {
if (stats) {
const filePath = `./${file}`
const form = new FormData();
const stats = fs.statSync(filePath);
const fileSizeInBytes = stats.size;
const fileStream = fs.createReadStream(filePath);
form.append('field-name', fileStream, { knownLength: fileSizeInBytes });
fetch(uurl, {
method: 'POST',
body: form,
headers: {
"auth": token
},
}).then(response => {
return response.json();
}).then(data => {
if (data.status == "403") {
console.error(`Token "${token}" is invalid`)
}
else {
console.log(`File has been successfully uploaded, and it available at ${data.url}`)
}
});
}
else {
console.log("Specify the file that you want to upload using -f <filename>")
}
})
}
else if (del){
if (file) {
fetch(durl, {
method: 'DELETE',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
"auth": token
},
body: JSON.stringify({
delete: file
})
}).then(response => {
return response.json();
}).then(data => {
if (data.status == "403") {
console.error(`Token "${token}" is invalid`)
}
else if (data.status == "422") {
console.log(`There is no file named ${file}`)
}
else if (data.status == "404") {
console.log(`There is no file named ${file}`)
}
else {
console.log(`File has been successfully deleted`)
}
});
}
else {
console.log("Specify the file that you want to delete using -f <filename>")
}
}
else {
console.log("Specify what you want to do. Use -u to upload file or -d to delete file")
}
}
else {
console.error("You must supply your service token using -t <token>!")
}