-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
216 lines (178 loc) · 6.49 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
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.onload = function () {
if (xhr.status >= 200 && xhr.status < 300) {
try {
const data = JSON.parse(xhr.responseText);
if (data.error) {
error(`Import Error: ${data.error}`);
} else if (data.length) {
if (data.import && data.import.length) {
success("Success!");
success(`Imported IDs: ${data.import.join(', ')}`);
}
if (data.update && data.update.length) {
success("Success!");
success(`Updated IDs: ${data.update.join(', ')}`);
}
} else {
error(`API Error: no response`);
}
} catch (e) {
console.error(e);
error(`JS Error: ${e.message}`);
}
} else {
error(`Request Error: ${xhr.statusText}`);
}
};
xhr.onerror = function () {
error("Network Error");
};
xhr.send(formData);
}
function error(message) {
out(message, 'warning');
}
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 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 () {
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) {
cacheImportForm = new FormData();
cacheImportFileList.innerHTML = "";
response.innerHTML = "";
});
startCacheImport.addEventListener("click", function (e) {
e.preventDefault();
response.innerHTML = "";
importFiles(importCacheApiPath, cacheImportForm);
});
addLensUpload.addEventListener("click", function (e) {
addNewLensUploadGroup();
});
resetLensUpload.addEventListener("click", function (e) {
lensUploadForm = new FormData();
lensUploadGroups.innerHTML = "";
response.innerHTML = "";
addNewLensUploadGroup();
});
startLensUpload.addEventListener("click", function (e) {
lensUploadForm = new FormData();
response.innerHTML = "";
let isErrorOccurred = false;
if (this.closest('form').checkValidity()) {
e.preventDefault();
} else {
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 file.`);
isErrorOccurred = true;
}
const inputVal = idInput.value.trim();
const lensId = parseLensId(inputVal);
if (lensId) {
lensUploadForm.append('id[]', lensId);
} else if (isValidUrl(inputVal)) {
lensUploadForm.append('id[]', inputVal);
} else {
error(`Error: "${inputVal}" is neither a valid Lens ID nor a share URL.`);
isErrorOccurred = true;
}
});
if (!isErrorOccurred) {
importFiles(importLensApiPath, lensUploadForm);
}
});
document.addEventListener("DOMContentLoaded", function () {
addNewLensUploadGroup();
});