-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscript.js
273 lines (224 loc) · 8.45 KB
/
script.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import uppie from "https://cdn.skypack.dev/[email protected]";
const serverAddress = document.querySelector('#serverAddress');
const overwriteLenses = document.querySelector('#overwriteLenses');
const uploadMode = document.querySelector('#uploadMode');
const cacheImport = document.querySelector('#cacheImport');
const lensUpload = document.querySelector('#lensUpload');
const cacheImportDropZone = document.querySelector('#cacheImportDropZone');
const cacheImportFileList = document.querySelector('#cacheImportFileList');
const resetCacheImport = document.querySelector('#resetCacheImport');
const startCacheImport = document.querySelector('#startCacheImport');
const lensUploadGroup = document.querySelector('#lensUploadGroup');
const lensUploadGroups = document.querySelector('#lensUploadGroups');
const addLensUpload = document.querySelector('#addLensUpload');
const resetLensUpload = document.querySelector('#resetLensUpload');
const startLensUpload = document.querySelector('#startLensUpload');
const response = document.querySelector('#response');
const importCacheApiPath = "/vc/v1/import/cache";
const importLensApiPath = "/vc/v1/import/lens";
let cacheImportForm = new FormData();
let lensUploadForm = new FormData();
function importFiles(apiPath, formData) {
formData.set('allow_overwrite', overwriteLenses.checked ? 'true' : 'false');
const xhr = new XMLHttpRequest();
xhr.open('POST', serverAddress.value + apiPath);
xhr.responseType = 'json';
xhr.onload = function () {
if (xhr.status >= 200 && xhr.status <= 500) {
try {
const data = xhr.response;
if (data) {
if (data.error !== false) {
error(`Import Error: ${data.error}`);
} else if (data.import?.length || data.update?.length) {
success("Import Success!");
if (data.import?.length) {
success(`Imported new IDs: [ ${data.import.join(', ')} ]`);
}
if (data.update?.length) {
success(`Updated existing IDs: [ ${data.update.join(', ')} ]`);
}
if (data.discard?.length) {
info(`Discarded existing IDs: [ ${data.discard.join(', ')} ]`);
}
if (data.fail?.length) {
error(`Failed IDs: [ ${data.fail.join(', ')} ]`);
}
} else if (data.discard?.length) {
info(`No new lenses were imported because they already exist.`);
info(`Discarded existing IDs: [ ${data.discard.join(', ')} ]`);
if (data.fail?.length) {
error(`Failed IDs: [ ${data.fail.join(', ')} ]`);
}
} else if (data.fail?.length) {
error(`Importing failed without error.`);
error(`Failed IDs: [ ${data.fail.join(', ')} ]`);
} else {
info(`The request was executed successfully, but no changes were made.`);
}
} else {
error(`API Error: no response`);
}
} catch (e) {
console.error(e);
error(`JS Error: ${e.message}`);
}
} else {
error(`Request Error: ${xhr.status} - ${xhr.statusText || '(No status text available)'}`);
}
};
xhr.onerror = function () {
error("Network Error: Make sure your server is reachable.");
};
xhr.send(formData);
}
function error(message) {
out(message, 'warning');
}
function info(message) {
out(message, 'info');
}
function success(message) {
out(message, 'success');
}
function out(message, type) {
const p = document.createElement('p');
p.classList.add(`text-${type}`);
p.textContent = message;
response.appendChild(p);
}
function parseLensId(path) {
const regex = /(?:^|\/)(\d{11,16})(?:_|\.|$)/;
const match = path.match(regex);
if (match) {
const numericValue = parseInt(match[1], 10);
if (!isNaN(numericValue)) {
return numericValue;
}
}
return null;
}
function isUuid(string) {
const uuid = string.match(/[a-f0-9]{32}/gi)
if (uuid && uuid[0]) {
return true;
}
return false;
}
function isValidUrl(string) {
try {
new URL(string);
return true;
} catch (_) {
return false;
}
}
function addNewLensUploadGroup() {
const clone = document.importNode(lensUploadGroup.content, true);
lensUploadGroups.appendChild(clone);
const fileInput = lensUploadGroups.lastElementChild.querySelector('input[type=file]');
const lensIdInput = fileInput.closest('.upload-group').querySelector('input[name="id[]"]');
uppie(fileInput, (event, formData, files) => {
files.forEach(path => {
const id = parseLensId(path);
if (!lensIdInput.value && id) {
lensIdInput.value = id;
}
});
});
}
uppie(cacheImportDropZone, (event, formData, files) => {
files.forEach(path => {
cacheImportFileList.innerHTML += path + '\r\n';
});
for (const value of formData.values()) {
cacheImportForm.append('file[]', value, value.name);
}
});
uploadMode.addEventListener('change', function (e) {
e.preventDefault();
var selectedOption = this.value;
if (selectedOption === 'cacheImport') {
cacheImport.classList.add('show');
cacheImport.classList.remove('hide');
lensUpload.classList.remove('show');
lensUpload.classList.add('hide');
} else if (selectedOption === 'lensUpload') {
cacheImport.classList.remove('show');
cacheImport.classList.add('hide');
lensUpload.classList.add('show');
lensUpload.classList.remove('hide');
}
});
resetCacheImport.addEventListener("click", function (e) {
e.preventDefault();
cacheImportForm = new FormData();
cacheImportFileList.innerHTML = "";
response.innerHTML = "";
});
startCacheImport.addEventListener("click", function (e) {
e.preventDefault();
response.innerHTML = "";
if (!cacheImportForm.has('file[]')) {
error(`Error: There is no file to upload.`);
return false;
}
importFiles(importCacheApiPath, cacheImportForm);
return true;
});
addLensUpload.addEventListener("click", function (e) {
e.preventDefault();
addNewLensUploadGroup();
});
resetLensUpload.addEventListener("click", function (e) {
e.preventDefault();
lensUploadForm = new FormData();
lensUploadGroups.innerHTML = "";
response.innerHTML = "";
addNewLensUploadGroup();
});
startLensUpload.addEventListener("click", function (e) {
e.preventDefault();
lensUploadForm = new FormData();
response.innerHTML = "";
let isErrorOccurred = false;
const form = this.closest('form');
if (form && !form.checkValidity()) {
error(`Error: You must fill in the input fields correctly.`);
form.reportValidity();
return false;
}
const uploadGroups = document.querySelectorAll('.upload-group');
uploadGroups.forEach(group => {
const fileInput = group.querySelector('input[type="file"]');
const idInput = group.querySelector('input[name="id[]"]');
if (fileInput.files.length > 0) {
lensUploadForm.append('file[]', fileInput.files[0]);
} else {
error(`Error: You have to select a .lns or .zip file.`);
isErrorOccurred = true;
}
const inputVal = idInput.value.trim();
const lensId = parseLensId(inputVal);
if (lensId) {
lensUploadForm.append('id[]', lensId);
} else if (isValidUrl(inputVal) || isUuid(inputVal)) {
lensUploadForm.append('id[]', inputVal);
} else {
error(`Error: "${inputVal}" is neither a valid Lens ID nor a share URL or UUID.`);
isErrorOccurred = true;
}
});
if (isErrorOccurred) {
return false;
}
if (!lensUploadForm.has('id[]') || !lensUploadForm.has('file[]')) {
error(`Error: There is no file to upload.`);
return false;
}
importFiles(importLensApiPath, lensUploadForm);
return true;
});
document.addEventListener("DOMContentLoaded", function () {
addNewLensUploadGroup();
});