-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (74 loc) · 2.3 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
#!/usr/bin/env node
import fetch, { FormData, fileFromSync } from "node-fetch";
import { program } from "commander";
import fs from "fs";
import pc from "picocolors";
const { red, green, blue, bold } = pc;
const packageJson = JSON.parse(fs.readFileSync("./package.json"));
program.name(packageJson.name);
program.description(packageJson.description);
program.version(packageJson.version, "-v, --version");
program.arguments("<file-path/url>");
program.option("-t, --token <token>", "token to use for upload");
program.option("-d, --delete", "delete the file immediately");
program.option("-s, --secret", "generate a URL which is not easily guessable");
program.option(
"-e, --expires <hours/epoch milliseconds>",
"set an expiration date for the file"
);
program.parse(process.argv);
const options = program.opts();
const args = program.args;
const filePath = args[0];
const isUrl = filePath.startsWith("http://") || filePath.startsWith("https://");
const form = new FormData();
const regex = /^http(?:s)?:\/\/0x0\.st\/(?!$).*$/; // regex to check if it is valid 0x0.st URL
const is0x0Url = regex.test(filePath);
let fetchUrl = "https://0x0.st/";
if (is0x0Url) {
fetchUrl = filePath;
} else {
if (isUrl) {
form.append("url", filePath);
} else {
form.append("file", fileFromSync(filePath));
}
if (options.secret) {
form.append("secret", "");
}
}
if (options.token) {
form.append("token", options.token);
}
if (options.delete) {
form.append("delete", "");
if (!options.token) {
console.log(red("You need to provide a token to delete the file"));
process.exit(1);
}
}
if (options.expires) {
form.append("expires", options.expires);
}
(async () => {
try {
const options = {
method: "POST",
body: form,
};
const response = await fetch(fetchUrl, options);
const token = response.headers.get("x-token");
const resultUrl = await response.text();
if (regex.test(resultUrl.trim())) {
console.log(green("File uploaded successfully!"));
if (token) {
console.log(bold("Token:"), blue(token));
}
console.log(bold("URL:"), blue(resultUrl));
} else {
console.log(red(resultUrl));
}
} catch (e) {
console.log(red("Something went wrong!"));
}
})();