-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.ts
More file actions
209 lines (177 loc) · 5.07 KB
/
file.ts
File metadata and controls
209 lines (177 loc) · 5.07 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
interface UploadManager {
new (payload: { apiKey: string }): UploadManager;
upload: (payload: {
data: File;
}) => Promise<{ fileUrl: string; filePath: string }>;
}
interface Bytescale {
UploadManager: UploadManager;
}
declare var Bytescale: Bytescale;
/**
* entract loads up the intro modal
*/
const entrance = document.getElementById('entrance');
entrance.click();
/**
* builder is a list of strings that are used to spice up the loading message
*/
const builders = [
'Staging...',
'Building...',
'Deploying...',
'Testing...',
'Cooking...',
'Serving...',
'Frying...',
'Baking...',
];
/**
* collectorLoad is the main function that is called when the user clicks the
* "Continue" button after uploading the pdf and stamp files
*/
function collectorLoad(e: Event) {
if (!validate()) {
return;
}
if (!(e.target as HTMLElement).innerHTML.includes('fa-spinner')) {
(
e.target as HTMLElement
).innerHTML = `<i class="fa fa-spinner fa-spin collectorload"></i> Loading...`;
(e.target as HTMLElement).setAttribute('disabled', 'disabled');
spice();
}
upload();
}
document
.querySelector('#collectorload')
.addEventListener('click', collectorLoad, false);
/**
* spice is a function that is called every 2 seconds to spice up the loading
*/
function spice() {
Context.CollectorLoad = window.setInterval(() => {
document.querySelector(
'#collectorload'
).innerHTML = `<i class="fa fa-spinner fa-spin collectorload"></i> ${
builders[Math.floor(Math.random() * builders.length)]
}`;
}, 2000);
store('context', Context);
}
/**
* salty stops the loader and reverts the button back to its original state
*/
function salty() {
document.querySelector('#collectorload').innerHTML = 'Continue';
document.querySelector('#collectorload').removeAttribute('disabled');
window.clearInterval(Context.CollectorLoad);
}
/**
* validate checks if the user has selected the pdf and stamp files
*/
function validate(e?: Event): boolean {
const pdffile = ((e && e.target) ||
document.getElementById('pdffile')) as HTMLInputElement;
const stampfile = ((e && e.target) ||
document.getElementById('stampfile')) as HTMLInputElement;
if (pdffile.files.length == 0) {
toast('Please select a PDF file');
pdffile.classList.add('is-invalid');
return false;
} else {
pdffile.classList.remove('is-invalid');
}
if (stampfile.files.length == 0) {
toast('Please select a stamp file');
stampfile.classList.add('is-invalid');
return false;
} else {
stampfile.classList.remove('is-invalid');
}
return true;
}
/**
* upload pushes the pdf and stamp files to filebin and sets the context
* with the filebin urls
*/
async function upload() {
const pdffile = document.getElementById('pdffile') as HTMLInputElement;
const stampfile = document.getElementById('stampfile') as HTMLInputElement;
const pdf = pdffile.files[0];
const stamp = stampfile.files[0];
// set the context with the file names
Context.PDFName = pdf.name;
Context.StampName = stamp.name;
const pdfUrl = await byteScale(pdf);
if (pdfUrl instanceof Error) {
return;
}
const stampUrl = await byteScale(stamp);
if (stampUrl instanceof Error) {
return;
}
Context.PDF = pdfUrl;
Context.Stamp = stampUrl;
store('context', Context);
salty();
navigate('/stamp');
}
/**
* filebin uploads a file to filebin and returns the url
*/
async function filebin(file: File): Promise<string | Error> {
try {
const formData = new FormData();
formData.append('file', file);
const req = await fetch(
URLs.FileBin + Misc.Bin + '/' + Date.now().toString() + '_' + file.name,
{
method: 'POST',
body: formData,
headers: {
'Content-Type': file.type,
accept: 'application/json',
cid: Misc.Bin,
'content-length': file.size.toString(),
'user-agent':
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
// ':authority': 'filebin.net',
},
referrer: URLs.FileBin,
}
);
const res: {
file: {
filename: string;
};
} = await req.json();
return URLs.FileBin + Misc.Bin + '/' + res.file.filename;
} catch (error) {
salty();
toast('Error uploading file' + file.name + '! Please try again.');
return error;
}
}
/**
* byteScale uploads a file to bytescale and returns the url
*/
async function byteScale(file: File): Promise<string | Error> {
try {
const manager = new Bytescale.UploadManager({
apiKey: 'public_kW15c1y9o12yjyNinKCTrKKEFfPG', // Get API key: https://www.bytescale.com/get-started
});
const response = await manager.upload({
data: file,
});
return response.fileUrl;
} catch (error) {
salty();
toast('Error uploading file' + file.name + '! Please try again.');
return error;
}
}
document.querySelector('#pdffile').addEventListener('change', validate, false);
document
.querySelector('#stampfile')
.addEventListener('change', validate, false);