-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·135 lines (124 loc) · 3.52 KB
/
index.js
File metadata and controls
executable file
·135 lines (124 loc) · 3.52 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
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
#!/usr/bin/env node
const utils = require("./utils");
const fs = require("fs");
const mime = require("mime");
const { program } = require("commander");
program.version("1.2.0");
program
.requiredOption(
"-f, --file <path-to-file>",
"Expiry time of the object in minutes"
)
.option(
"-e, --expire <expire-minutes>",
"Expiry time of the object in minutes. Minimum 5 minutes",
5
)
.option("-p, --password", "To password protect your file.");
program.on("--help", () => {
console.log("Example call:");
console.log("$ fha -f hello.txt");
console.log("$ fha -f hello.txt -e 30");
console.log("$ fha -f hello.txt -e 30 -p");
});
// If no arguments more arguments sent to application, display help.
if (process.argv.length === 2) {
program.outputHelp();
process.exit(0);
}
program.parse(process.argv);
// Check minimum time
if (program.expire < 5) {
console.error("Expiry of objects should be more than 5 minutes");
}
// Ask for password
if (program.password) {
utils.askPassword().then((password) => {
console.log("\n");
uploadFile(password);
});
} else {
uploadFile();
}
function uploadFile(password = null) {
const axios = require("axios").default;
const baseURL = "https://fha.omkarshelar.dev";
let binaryFile;
try {
binaryFile = fs.readFileSync(program.file);
} catch {
console.error(`Error: File named ${program.file} NOT found.`);
process.exit(1);
}
fileName = program.file.slice(program.file.lastIndexOf("/") + 1);
if (!process.env.FH_API_KEY) {
console.log(
"API KEY not in environment variables. Please provide API KEY."
);
process.exit(1);
}
// API key needed to authenticate
const baseHeaders = {
"x-api-key": process.env.FH_API_KEY,
};
// Make requests to the API and S3
axios
.get(`${baseURL}/signed-url-upload/${fileName}`, {
headers: {
...baseHeaders,
},
})
.then((res) => {
const uploadURL = res.data.UploadURL;
const objectKey = res.data.Key;
// Calculate mime/type of the file
contentType = mime.getType(fileName.slice(fileName.lastIndexOf(".") + 1));
const config = {
headers: {
"Content-Type": contentType,
},
maxContentLength: Infinity,
maxBodyLength: Infinity,
};
axios.put(uploadURL, binaryFile, config).then((res) => {
if (res.status === 200) {
const expiryTime =
Math.floor(new Date().getTime() / 1000) + 60 * program.expire;
let customURIData = {};
if (password) {
customURIData.password = password;
}
axios
.post(
`${baseURL}/custom-uri/${objectKey}/${expiryTime}`,
customURIData,
{
headers: {
...baseHeaders,
},
}
)
.then((res) => {
console.log(`Download URL: ${baseURL}/asset/${res.data.URL}`);
const options = {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
timeZoneName: "long",
hour: "numeric",
minute: "numeric",
second: "numeric",
};
console.log(
`URL Expiry : ${new Date(expiryTime * 1000).toLocaleString(
"en-US",
options
)}`
);
})
.catch((err) => console.error(err));
}
});
});
}