From 2b2cfe9d33b4e2641e368ef247ce3439b248e23e Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Tue, 24 Sep 2024 13:25:00 +0500 Subject: [PATCH] web: do not zip note without attachments on export --- apps/web/src/common/export.ts | 41 +++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/apps/web/src/common/export.ts b/apps/web/src/common/export.ts index e750ca66a..890c825a1 100644 --- a/apps/web/src/common/export.ts +++ b/apps/web/src/common/export.ts @@ -33,6 +33,9 @@ import Vault from "./vault"; import { ExportStream } from "../utils/streams/export-stream"; import { showToast } from "../utils/toast"; import { ConfirmDialog } from "../dialogs/confirm"; +import { db } from "./db"; +import { toAsyncIterator } from "@notesnook-importer/core/dist/src/utils/stream"; +import { saveAs } from "file-saver"; export async function exportToPDF( title: string, @@ -151,21 +154,37 @@ export async function exportNote( title: `Exporting "${note.title}"`, subtitle: "Please wait while your note is exported.", action: async (report) => { - await fromAsyncIterator( + const hasAttachments = + (await db.relations.from(note, "attachment").count()) > 0; + const stream = fromAsyncIterator( _exportNote(note, { format: options.format, unlockVault: Vault.unlockVault }) - ) - .pipeThrough( - new ExportStream(report, (e) => showToast("error", e.message)) - ) - .pipeThrough(createZipStream()) - .pipeTo( - await createWriteStream( - `${sanitizeFilename(note.title, { replacement: "-" })}.zip` - ) - ); + ).pipeThrough( + new ExportStream(report, (e) => { + console.error(e); + showToast("error", e.message); + }) + ); + + if (hasAttachments) { + await stream + .pipeThrough(createZipStream()) + .pipeTo( + await createWriteStream( + `${sanitizeFilename(note.title, { replacement: "-" })}.zip` + ) + ); + } else { + for await (const file of toAsyncIterator(stream)) { + if (typeof file.data === "string") + saveAs(new Blob([Buffer.from(file.data, "utf-8")]), file.path); + else if (file.data instanceof Uint8Array) + saveAs(new Blob([file.data]), file.path); + else await file.data.pipeTo(await createWriteStream(file.path)); + } + } return true; } });