Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(attachments): recover sizes for legacy notes

tianma-ife53155047df6
diff --git a/apps/web/src/components/pdf/PdfViewer.tsx b/apps/web/src/components/pdf/PdfViewer.tsx
index 231a165e..5dd48c5b 100644
--- a/apps/web/src/components/pdf/PdfViewer.tsx
+++ b/apps/web/src/components/pdf/PdfViewer.tsx
@@ -20,6 +20,7 @@ import { useTranslation } from "react-i18next";
import { AttachmentFileIcon } from "@/components/attachments/AttachmentFileIcon";
import { COMPACT_ATTACHMENT_WIDTH_CLASS } from "@/components/attachments/attachment-layout";
import { ButtonTooltip } from "@/components/ui/button-tooltip";
+import { useAttachmentByteSize } from "@/hooks/useAttachmentByteSize";
import { isDesktopResourceRuntime, toApiResourceUrl } from "@/lib/desktop-resources";
import { cn } from "@/lib/utils";
import { loadPdfJs } from "./pdfjs-loader";
@@ -193,9 +194,10 @@ export const PdfViewer = ({
const [failed, setFailed] = useState(false);
const [internalFullscreen, setInternalFullscreen] = useState(false);
const isFullscreen = fullscreen || internalFullscreen;
- const previewAllowed = canPreviewPdfInline(byteSize);
+ const resolvedByteSize = useAttachmentByteSize(resolvedUrl, byteSize);
+ const previewAllowed = canPreviewPdfInline(resolvedByteSize);
const expanded = previewAllowed && (isFullscreen || (controlledExpanded ?? uncontrolledExpanded));
- const metadata = formatAttachmentMetadata("application/pdf", filename || label, byteSize);
+ const metadata = formatAttachmentMetadata("application/pdf", filename || label, resolvedByteSize);
useEffect(() => {
const root = rootRef.current;
diff --git a/apps/web/src/hooks/useAttachmentByteSize.ts b/apps/web/src/hooks/useAttachmentByteSize.ts
new file mode 100644
index 00000000..67b5188f
--- /dev/null
+++ b/apps/web/src/hooks/useAttachmentByteSize.ts
@@ -0,0 +1,49 @@
+import { normalizeAttachmentByteSize } from "@edgeever/shared";
+import { useEffect, useState } from "react";
+import { fetchAttachmentByteSize } from "@/lib/attachment-byte-size";
+
+const byteSizeCache = new Map<string, number | null>();
+const pendingRequests = new Map<string, Promise<number | null>>();
+
+const loadAttachmentByteSize = (url: string) => {
+ const cached = byteSizeCache.get(url);
+ if (cached !== undefined) return Promise.resolve(cached);
+
+ const pending = pendingRequests.get(url);
+ if (pending) return pending;
+
+ const request = fetchAttachmentByteSize(url)
+ .catch(() => null)
+ .then((byteSize) => {
+ byteSizeCache.set(url, byteSize);
+ pendingRequests.delete(url);
+ return byteSize;
+ });
+ pendingRequests.set(url, request);
+ return request;
+};
+
+export const useAttachmentByteSize = (url: string, storedByteSize: unknown) => {
+ const normalizedStoredByteSize = normalizeAttachmentByteSize(storedByteSize);
+ const [discovered, setDiscovered] = useState<{ url: string; byteSize: number | null }>(() => ({
+ url,
+ byteSize: normalizedStoredByteSize === null ? byteSizeCache.get(url) ?? null : null,
+ }));
+
+ useEffect(() => {
+ if (normalizedStoredByteSize !== null || !url) {
+ setDiscovered({ url, byteSize: null });
+ return;
+ }
+
+ let active = true;
+ void loadAttachmentByteSize(url).then((byteSize) => {
+ if (active) setDiscovered({ url, byteSize });
+ });
+ return () => {
+ active = false;
+ };
+ }, [normalizedStoredByteSize, url]);
+
+ return normalizedStoredByteSize ?? (discovered.url === url ? discovered.byteSize : null);
+};
diff --git a/apps/web/src/lib/attachment-byte-size.test.mjs b/apps/web/src/lib/attachment-byte-size.test.mjs
new file mode 100644
index 00000000..528b67da
--- /dev/null
+++ b/apps/web/src/lib/attachment-byte-size.test.mjs
@@ -0,0 +1,34 @@
+import { describe, expect, test } from "bun:test";
+import { getAttachmentByteSizeFromResponse } from "./attachment-byte-size.ts";
+
+describe("attachment byte size discovery", () => {
+ test("reads the complete size from a one-byte range response", () => {
+ const response = new Response(new Uint8Array([1]), {
+ status: 206,
+ headers: {
+ "Content-Length": "1",
+ "Content-Range": "bytes 0-0/856000",
+ },
+ });
+
+ expect(getAttachmentByteSizeFromResponse(response)).toBe(856000);
+ });
+
+ test("falls back to Content-Length for a full response", () => {
+ const response = new Response(null, {
+ status: 200,
+ headers: { "Content-Length": "2048" },
+ });
+
+ expect(getAttachmentByteSizeFromResponse(response)).toBe(2048);
+ });
+
+ test("does not mistake a partial Content-Length for the complete size", () => {
+ const response = new Response(new Uint8Array([1]), {
+ status: 206,
+ headers: { "Content-Length": "1" },
+ });
+
+ expect(getAttachmentByteSizeFromResponse(response)).toBeNull();
+ });
+});
diff --git a/apps/web/src/lib/attachment-byte-size.ts b/apps/web/src/lib/attachment-byte-size.ts
new file mode 100644
index 00000000..42a113c6
--- /dev/null
+++ b/apps/web/src/lib/attachment-byte-size.ts
@@ -0,0 +1,27 @@
+import { normalizeAttachmentByteSize } from "@edgeever/shared";
+
+const COMPLETE_LENGTH_PATTERN = /\/(\d+)\s*$/;
+
+export const getAttachmentByteSizeFromResponse = (response: Response): number | null => {
+ const contentRange = response.headers.get("Content-Range");
+ const rangeTotal = contentRange?.match(COMPLETE_LENGTH_PATTERN)?.[1];
+ const byteSize = normalizeAttachmentByteSize(rangeTotal);
+ if (byteSize !== null) return byteSize;
+
+ if (response.status !== 200) return null;
+ return normalizeAttachmentByteSize(response.headers.get("Content-Length"));
+};
+
+export const fetchAttachmentByteSize = async (url: string, signal?: AbortSignal): Promise<number | null> => {
+ if (!url) return null;
+
+ const response = await fetch(url, {
+ cache: "no-store",
+ credentials: "same-origin",
+ headers: { Range: "bytes=0-0" },
+ signal,
+ });
+ const byteSize = response.ok ? getAttachmentByteSizeFromResponse(response) : null;
+ await response.body?.cancel().catch(() => undefined);
+ return byteSize;
+};