Skip to content

Commit fa417f9

Browse files
authored
Enhance file import with validation and error handling
Updated the file import process to handle legacy entries and validate the imported data format. Added error handling for invalid formats and improved user feedback.
1 parent b2bb455 commit fa417f9

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

app.js

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -222,19 +222,42 @@ exportBtn.onclick = () => {
222222
// Import Vault
223223
importBtn.onclick = () => importFile.click();
224224

225-
importFile.onchange = (e) => {
225+
importFile.onchange = async (e) => {
226226
const file = e.target.files[0];
227227
if (!file) return;
228228

229229
const reader = new FileReader();
230-
reader.onload = (event) => {
230+
reader.onload = async (event) => {
231231
try {
232-
vault = JSON.parse(event.target.result);
233-
saveVault();
234-
addMessage('Vault imported successfully.', 'bot');
235-
showToast('Vault imported', 'success');
236-
} catch {
237-
showToast('Invalid file format', 'error');
232+
const data = JSON.parse(event.target.result);
233+
234+
// Validate it’s an array
235+
if (!Array.isArray(data)) {
236+
alert("Invalid file format. Expected a list of documents.");
237+
return;
238+
}
239+
240+
// Convert legacy entries (from old localStorage system)
241+
const converted = data.map(d => {
242+
return {
243+
name: d.name || "Unnamed",
244+
value: d.value || "",
245+
info: d.info || "",
246+
file: d.file || null, // may already be a DataURL or null
247+
};
248+
});
249+
250+
// Save each document to IndexedDB
251+
for (const doc of converted) {
252+
await saveDocToDB(doc);
253+
}
254+
255+
vault = await getAllDocs();
256+
addMessage("Vault imported successfully.", "bot");
257+
showToast("Vault imported successfully!", "success");
258+
} catch (err) {
259+
console.error("Import error:", err);
260+
alert("Invalid file format or corrupted JSON.");
238261
}
239262
};
240263
reader.readAsText(file);

0 commit comments

Comments
 (0)