Skip to content

Commit c57e7ee

Browse files
committed
Add form validation and improve response handling
1 parent 21c88bd commit c57e7ee

File tree

3 files changed

+71
-26
lines changed

3 files changed

+71
-26
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ Import your local cached Lenses from your Snap Camera application or upload cust
1111
## ⚠️ Requirements
1212
- [Snap Camera Server](https://github.com/ptrumpis/snap-camera-server)
1313
- Cache Import requires v2.0+
14-
- Custom Upload requires v3.3+
14+
- Custom Upload requires v3.3.3+
1515

1616
## 🚀 Usage
17-
Visit the Web Import Tool and follow the instructions.
18-
- [Web Import Tool](https://ptrumpis.github.io/snap-lens-cache-import/)
17+
Visit the [Web Import Tool](https://ptrumpis.github.io/snap-lens-cache-import/) and follow the instructions.
1918

2019
Thats it!
2120

22-
#### Offline Usage (optional)
21+
### Offline Usage (optional)
2322
If you have Node.js and npm installed you can download the latest release package and run a local copy with
2423
```
2524
npx http-server
@@ -37,7 +36,7 @@ And your *settings.json* is located at
3736
```
3837

3938
## 📺 How To Video Guide
40-
You can watch this step by step video guide on YouTube if you need help with the import process
39+
You can watch this step by step video guide on YouTube if you need help with the import process.
4140

4241
[![Snap Camera Installation Guide](https://img.youtube.com/vi/alo49et3QxY/0.jpg)](https://www.youtube.com/watch?v=alo49et3QxY)
4342

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ <h3>Upload and Import Snap Lenses</h3>
109109
<div class="upload-group row justify-content-sm-center mb-3">
110110
<div class="col-xs-auto col-sm-6 col-md-4 col-lg-3">
111111
<div class="form-group mb-1">
112-
<input type="file" class="form-control" name="file[]" accept=".lns" required>
112+
<input type="file" class="form-control" name="file[]" accept=".lns,.zip" required>
113113
</div>
114114
<div class="form-group mb-1">
115-
<input type="text" class="form-control" name="id[]" placeholder="Lens ID or Lens Share URL" required>
115+
<input type="text" class="form-control" name="id[]" placeholder="Lens ID, Lens UUID or Lens Share URL" required>
116116
</div>
117117
<div class="form-group mb-1">
118118
<button class="btn btn-sm btn-danger" type="button" onclick="this.closest('.upload-group').remove()">Remove</button>

script.js

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,38 @@ function importFiles(apiPath, formData) {
3333
xhr.open('POST', serverAddress.value + apiPath);
3434
xhr.responseType = 'json';
3535
xhr.onload = function () {
36-
if (xhr.status >= 200 && xhr.status < 300) {
36+
if (xhr.status >= 200 && xhr.status <= 500) {
3737
try {
3838
const data = xhr.response;
39-
console.log(data);
40-
if (data && data.error) {
41-
error(`Import Error: ${data.error}`);
42-
} else if (data && (data.import?.length || data.update?.length)) {
43-
if (data.import?.length) {
44-
success("Success!");
45-
success(`Imported IDs: ${data.import.join(', ')}`);
46-
}
47-
if (data.update?.length) {
48-
success("Success!");
49-
success(`Updated IDs: ${data.update.join(', ')}`);
39+
if (data) {
40+
if (data.error !== false) {
41+
error(`Import Error: ${data.error}`);
42+
} else if (data.import?.length || data.update?.length) {
43+
success("Import Success!");
44+
if (data.import?.length) {
45+
success(`Imported new IDs: [ ${data.import.join(', ')} ]`);
46+
}
47+
if (data.update?.length) {
48+
success(`Updated existing IDs: [ ${data.update.join(', ')} ]`);
49+
}
50+
if (data.discard?.length) {
51+
info(`Discarded existing IDs: [ ${data.discard.join(', ')} ]`);
52+
}
53+
if (data.fail?.length) {
54+
error(`Failed IDs: [ ${data.fail.join(', ')} ]`);
55+
}
56+
} else if (data.discard?.length) {
57+
info(`No new lenses were imported because they already exist.`);
58+
info(`Discarded existing IDs: [ ${data.discard.join(', ')} ]`);
59+
60+
if (data.fail?.length) {
61+
error(`Failed IDs: [ ${data.fail.join(', ')} ]`);
62+
}
63+
} else if (data.fail?.length) {
64+
error(`Importing failed without error.`);
65+
error(`Failed IDs: [ ${data.fail.join(', ')} ]`);
66+
} else {
67+
info(`The request was executed successfully, but no changes were made.`);
5068
}
5169
} else {
5270
error(`API Error: no response`);
@@ -60,7 +78,7 @@ function importFiles(apiPath, formData) {
6078
}
6179
};
6280
xhr.onerror = function () {
63-
error("Network Error");
81+
error("Network Error: Make sure your server is reachable.");
6482
};
6583
xhr.send(formData);
6684
}
@@ -69,6 +87,10 @@ function error(message) {
6987
out(message, 'warning');
7088
}
7189

90+
function info(message) {
91+
out(message, 'info');
92+
}
93+
7294
function success(message) {
7395
out(message, 'success');
7496
}
@@ -92,6 +114,14 @@ function parseLensId(path) {
92114
return null;
93115
}
94116

117+
function isUuid(string) {
118+
const uuid = string.match(/[a-f0-9]{32}/gi)
119+
if (uuid && uuid[0]) {
120+
return true;
121+
}
122+
return false;
123+
}
124+
95125
function isValidUrl(string) {
96126
try {
97127
new URL(string);
@@ -160,7 +190,13 @@ startCacheImport.addEventListener("click", function (e) {
160190

161191
response.innerHTML = "";
162192

193+
if (!cacheImportForm.has('file[]')) {
194+
error(`Error: There is no file to upload.`);
195+
return false;
196+
}
197+
163198
importFiles(importCacheApiPath, cacheImportForm);
199+
return true;
164200
});
165201

166202
addLensUpload.addEventListener("click", function (e) {
@@ -182,14 +218,16 @@ resetLensUpload.addEventListener("click", function (e) {
182218

183219
startLensUpload.addEventListener("click", function (e) {
184220
e.preventDefault();
185-
221+
186222
lensUploadForm = new FormData();
187223

188224
response.innerHTML = "";
189225
let isErrorOccurred = false;
190226

191227
const form = this.closest('form');
192228
if (form && !form.checkValidity()) {
229+
error(`Error: You must fill in the input fields correctly.`);
230+
form.reportValidity();
193231
return false;
194232
}
195233

@@ -201,25 +239,33 @@ startLensUpload.addEventListener("click", function (e) {
201239
if (fileInput.files.length > 0) {
202240
lensUploadForm.append('file[]', fileInput.files[0]);
203241
} else {
204-
error(`Error: You have to select a .lns file.`);
242+
error(`Error: You have to select a .lns or .zip file.`);
205243
isErrorOccurred = true;
206244
}
207245

208246
const inputVal = idInput.value.trim();
209247
const lensId = parseLensId(inputVal);
210248
if (lensId) {
211249
lensUploadForm.append('id[]', lensId);
212-
} else if (isValidUrl(inputVal)) {
250+
} else if (isValidUrl(inputVal) || isUuid(inputVal)) {
213251
lensUploadForm.append('id[]', inputVal);
214252
} else {
215-
error(`Error: "${inputVal}" is neither a valid Lens ID nor a share URL.`);
253+
error(`Error: "${inputVal}" is neither a valid Lens ID nor a share URL or UUID.`);
216254
isErrorOccurred = true;
217255
}
218256
});
219257

220-
if (!isErrorOccurred) {
221-
importFiles(importLensApiPath, lensUploadForm);
258+
if (isErrorOccurred) {
259+
return false;
260+
}
261+
262+
if (!lensUploadForm.has('id[]') || !lensUploadForm.has('file[]')) {
263+
error(`Error: There is no file to upload.`);
264+
return false;
222265
}
266+
267+
importFiles(importLensApiPath, lensUploadForm);
268+
return true;
223269
});
224270

225271
document.addEventListener("DOMContentLoaded", function () {

0 commit comments

Comments
 (0)