AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.
HTML ai-agentcloudflarecloudflare-d1cloudflare-r2cloudflare-workersdockerelectronevernoteevernote-alternativeknowledge-basemcpnote-takingobsidian-alternativeoffline-firstpersonal-knowledge-basepwaself-hostedsqlitetiptapweb-clipper
fix(web): make multi-image uploads resilient
tianma-if9e32d317da9b
diff --git a/apps/web/src/components/EditorPane.tsx b/apps/web/src/components/EditorPane.tsxindex b0c9bfee..88a4eeb0 100644--- a/apps/web/src/components/EditorPane.tsx+++ b/apps/web/src/components/EditorPane.tsx@@ -112,6 +112,7 @@ import { downloadMarkdownFile } from "@/lib/note-markdown-export";import { openNotePrintPreview, serializeNoteDocumentForPrint } from "@/lib/note-print";import { isBrowserOffline } from "@/lib/network-status";import { shouldOpenEditorLink } from "@/lib/editor-link-click";+import { processFilesSequentially } from "@/lib/file-batch";const SUPPORTED_PASTE_IMAGE_TYPES = new Set(["image/png", "image/jpeg", "image/gif", "image/webp", "image/avif"]);const MOBILE_EDITOR_QUERY = "(max-width: 639px)";@@ -1343,70 +1344,74 @@ const RichEditorPane = ({void (async () => {setImageUploadState("uploading");- try {- for (const file of files) {- const isImage = SUPPORTED_PASTE_IMAGE_TYPES.has(file.type);- const shouldCompress = isImage && imageCompressionEnabledRef.current;- setImageUploadState(shouldCompress ? "compressing" : "uploading");- const uploadFile = shouldCompress ? (await compressImageForUpload(file)).file : file;-- setImageUploadState("uploading");- let resource: { kind: "image" | "attachment"; filename: string | null; url: string };- try {- const uploadedResource = (await repository.uploadMemoResource(targetMemoId, uploadFile)).resource;- resource = { ...uploadedResource, url: toDesktopResourceUrl(uploadedResource.url) };- } catch (error) {- if (!isDesktopResourceRuntime()) throw error;- const staged = await stageDesktopResource(targetMemoId, uploadFile);- if (!staged) throw error;- resource = {- kind: isImage ? "image" : "attachment",- filename: uploadFile.name,- url: `edgeever-staged://${staged.id}`,- };- }- void queryClient.invalidateQueries({ queryKey: ["resources"] });+ const results = await processFilesSequentially(files, async (file) => {+ const isImage = SUPPORTED_PASTE_IMAGE_TYPES.has(file.type);+ const shouldCompress = isImage && imageCompressionEnabledRef.current;+ setImageUploadState(shouldCompress ? "compressing" : "uploading");+ const uploadFile = shouldCompress ? (await compressImageForUpload(file)).file : file;- const activeEditor = editorRef.current;- if (memoRef.current?.id !== targetMemoId || !isEditorReady(activeEditor)) {- setImageUploadState("idle");- return;- }+ setImageUploadState("uploading");+ let resource: { kind: "image" | "attachment"; filename: string | null; url: string };+ try {+ const uploadedResource = (await repository.uploadMemoResource(targetMemoId, uploadFile)).resource;+ resource = { ...uploadedResource, url: toDesktopResourceUrl(uploadedResource.url) };+ } catch (error) {+ if (!isDesktopResourceRuntime()) throw error;+ const staged = await stageDesktopResource(targetMemoId, uploadFile);+ if (!staged) throw error;+ resource = {+ kind: isImage ? "image" : "attachment",+ filename: uploadFile.name,+ url: `edgeever-staged://${staged.id}`,+ };+ }+ return resource;+ });++ const successfulResults = results.filter((result) => result.status === "fulfilled");+ if (successfulResults.length > 0) {+ void queryClient.invalidateQueries({ queryKey: ["resources"] });+ }++ const activeEditor = editorRef.current;+ if (memoRef.current?.id !== targetMemoId || !isEditorReady(activeEditor)) {+ setImageUploadState("idle");+ return;+ }- if (resource.kind === "image") {- activeEditor- .chain()- .focus()- .setImage({+ const content = successfulResults.map(({ file, value: resource }) =>+ resource.kind === "image"+ ? {+ type: "image",+ attrs: {src: resource.url,alt: file.name,title: file.name,width: DEFAULT_IMAGE_WIDTH_PERCENT,- })- .run();- } else {- activeEditor- .chain()- .focus()- .insertContent({- type: "paragraph",- content: [{- type: "text",- text: t("editor.attachmentLabel", { filename: resource.filename || file.name }),- marks: [{- type: "link",- attrs: { href: resource.url, target: "_blank", class: "edgeever-attachment-link" },- }],+ },+ }+ : {+ type: "paragraph",+ content: [{+ type: "text",+ text: t("editor.attachmentLabel", { filename: resource.filename || file.name }),+ marks: [{+ type: "link",+ attrs: { href: resource.url, target: "_blank", class: "edgeever-attachment-link" },}],- })- .run();- }- }+ }],+ }+ );- setImageUploadState("idle");- } catch {+ if (content.length > 0) {+ activeEditor.chain().focus().insertContent(content).run();+ }++ if (results.some((result) => result.status === "rejected")) {setImageUploadState("error");window.setTimeout(() => setImageUploadState("idle"), 2200);+ } else {+ setImageUploadState("idle");}})();}, [queryClient, repository, t]);diff --git a/apps/web/src/lib/file-batch.test.mjs b/apps/web/src/lib/file-batch.test.mjsnew file mode 100644index 00000000..5f6170c1--- /dev/null+++ b/apps/web/src/lib/file-batch.test.mjs@@ -0,0 +1,29 @@+import { describe, expect, test } from "bun:test";+import { processFilesSequentially } from "./file-batch.ts";++describe("processFilesSequentially", () => {+ test("continues after a failed file and preserves selection order", async () => {+ const files = [+ new File(["a"], "first.jpg", { type: "image/jpeg" }),+ new File(["b"], "broken.jpg", { type: "image/jpeg" }),+ new File(["c"], "last.jpg", { type: "image/jpeg" }),+ ];+ const processed = [];++ const results = await processFilesSequentially(files, async (file) => {+ processed.push(file.name);+ if (file.name === "broken.jpg") {+ throw new Error("upload failed");+ }+ return `${file.name}:uploaded`;+ });++ expect(processed).toEqual(["first.jpg", "broken.jpg", "last.jpg"]);+ expect(results.map(({ file, status }) => [file.name, status])).toEqual([+ ["first.jpg", "fulfilled"],+ ["broken.jpg", "rejected"],+ ["last.jpg", "fulfilled"],+ ]);+ expect(results[2]).toMatchObject({ status: "fulfilled", value: "last.jpg:uploaded" });+ });+});diff --git a/apps/web/src/lib/file-batch.ts b/apps/web/src/lib/file-batch.tsnew file mode 100644index 00000000..b10329d3--- /dev/null+++ b/apps/web/src/lib/file-batch.ts@@ -0,0 +1,20 @@+export type FileBatchResult<T> =+ | { file: File; status: "fulfilled"; value: T }+ | { file: File; status: "rejected"; reason: unknown };++export const processFilesSequentially = async <T>(+ files: File[],+ processFile: (file: File) => Promise<T>+): Promise<FileBatchResult<T>[]> => {+ const results: FileBatchResult<T>[] = [];++ for (const file of files) {+ try {+ results.push({ file, status: "fulfilled", value: await processFile(file) });+ } catch (reason) {+ results.push({ file, status: "rejected", reason });+ }+ }++ return results;+};