-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
57 lines (48 loc) · 1.39 KB
/
index.ts
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
import { Client } from "minio";
import { createReadStream } from "fs";
import { extname } from "path";
let config = {
endpoint: "s3.myinfra.lol",
publicURL: "https://s3.tritan.gg",
bucketName: "uploads",
};
async function main(filePath: string) {
if (!filePath) {
console.error("No file path provided.\nUsage: ./executable <file-path>");
process.exit(1);
}
let file;
try {
file = createReadStream(filePath);
} catch (err) {
console.error(`Could not open file: ${filePath}`);
process.exit(1);
}
const fileExtension = extname(filePath);
const fileName = generateRandomString(10);
const newFileName = fileName + fileExtension;
const minioClient = new Client({
endPoint: config.endpoint,
accessKey: "",
secretKey: "",
useSSL: false,
});
try {
await minioClient.putObject(config.bucketName, newFileName, file);
const url = `${config.publicURL}/${config.bucketName}/${newFileName}`;
console.log(`File uploaded successfully.\nURL: ${url}`);
} catch (err) {
console.error(`Failed to upload file: ${err}`);
process.exit(1);
}
}
function generateRandomString(length: number) {
const charset =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let result = "";
for (let i = 0; i < length; i++) {
result += charset[Math.floor(Math.random() * charset.length)];
}
return result;
}
main(process.argv[2]);