Skip to content

Build your personal knowledge base with Trilium Notes

feat(import): show progress bar with total count for ZIP import

Elian Dorancc3ba4b3bc9b
diff --git a/packages/trilium-core/src/services/import/zip.spec.ts b/packages/trilium-core/src/services/import/zip.spec.ts
index dbe2848690..e794c223b9 100644
--- a/packages/trilium-core/src/services/import/zip.spec.ts
+++ b/packages/trilium-core/src/services/import/zip.spec.ts
@@ -212,6 +212,40 @@ describe("processNoteContent", () => {
expect(sheet.cellData[0][1].v).toBe(42);
});
+ it("reports a progress total that the running count lands exactly on (bar reaches 100%)", async () => {
+ // fresh task id -> a new TaskContext whose constructor increment happens before we spy
+ const taskContext = TaskContext.getInstance("import-progress-total", "importNotes", { textImportedAsText: true });
+ const setTotalSpy = vi.spyOn(taskContext, "setTotalCount");
+ const increaseSpy = vi.spyOn(taskContext, "increaseProgressCount");
+
+ const zipBuffer = await createZipBuffer({
+ "a.txt": "first",
+ "b.txt": "second",
+ "sub/c.txt": "third"
+ });
+
+ await new Promise<void>((resolve, reject) => {
+ getContext().init(async () => {
+ const rootNote = becca.getNote("root");
+ if (!rootNote) {
+ reject(new Error("missing root note"));
+ return;
+ }
+ await zip.importZip(taskContext, zipBuffer, rootNote as BNote);
+ resolve();
+ });
+ });
+
+ // the total is set twice: an initial estimate after the scan pass, then widened to the exact
+ // value once the created-note count is known
+ expect(setTotalSpy).toHaveBeenCalledTimes(2);
+
+ const finalTotal = setTotalSpy.mock.calls.at(-1)?.[0];
+ // the constructor's first increment isn't captured by the spy, but it brought progressCount from
+ // -1 to 0, so the final progressCount equals the number of captured increments
+ expect(finalTotal).toBe(increaseSpy.mock.calls.length);
+ });
+
it("imports a CSV entry as a plain file note when the spreadsheet option is off", async () => {
const zipBuffer = await createZipBuffer({ "csv_as_file_sample.csv": "a,b\r\n1,2" });
const { rootNote } = await testImportBuffer(zipBuffer, "import-csv-off", { spreadsheetImportedAsSpreadsheet: false });
diff --git a/packages/trilium-core/src/services/import/zip.ts b/packages/trilium-core/src/services/import/zip.ts
index 0ad2670940..43b064d04c 100644
--- a/packages/trilium-core/src/services/import/zip.ts
+++ b/packages/trilium-core/src/services/import/zip.ts
@@ -653,6 +653,9 @@ async function importZip(taskContext: TaskContext<"importNotes">, fileBuffer: Ui
// we're running two passes in order to obtain critical information first (meta file and root)
const topLevelItems = new Set<string>();
+ // count of entries the processing pass will handle, used as the progress denominator so the
+ // client can show a progress bar ("X of N") instead of a bare running count
+ let entriesToProcess = 0;
await zipProvider.readZipFile(fileBuffer, async (entry, readContent) => {
const filePath = normalizeFilePath(entry.fileName);
@@ -661,6 +664,8 @@ async function importZip(taskContext: TaskContext<"importNotes">, fileBuffer: Ui
return;
}
+ entriesToProcess++;
+
// make sure that the meta file is loaded before the rest of the files is processed.
if (filePath === "!!!meta.json") {
const content = await readContent();
@@ -675,6 +680,9 @@ async function importZip(taskContext: TaskContext<"importNotes">, fileBuffer: Ui
topLevelPath = (topLevelItems.size > 1 ? "" : topLevelItems.values().next().value ?? "");
+ // the processing pass increments progress once per entry; expose the total so the client shows a bar
+ taskContext.setTotalCount(entriesToProcess);
+
await zipProvider.readZipFile(fileBuffer, async (entry, readContent) => {
const filePath = normalizeFilePath(entry.fileName);
@@ -691,6 +699,10 @@ async function importZip(taskContext: TaskContext<"importNotes">, fileBuffer: Ui
taskContext.increaseProgressCount();
}, filenameEncoding);
+ // post-processing increments progress once per created note (a subset of the entries, now known
+ // exactly), so widen the total accordingly to keep the count accurate and let the bar reach 100%
+ taskContext.setTotalCount(entriesToProcess + createdNoteIds.size);
+
for (const noteId of createdNoteIds) {
const note = becca.getNote(noteId);
if (!note) continue;