Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/media player: fix an issue
Adorian Doran24f03ef82e57
diff --git a/apps/client/src/widgets/type_widgets/Attachment.tsx b/apps/client/src/widgets/type_widgets/Attachment.tsxindex 2d64ee2400..b13e2b6f5a 100644--- a/apps/client/src/widgets/type_widgets/Attachment.tsx+++ b/apps/client/src/widgets/type_widgets/Attachment.tsx@@ -151,6 +151,9 @@ function AttachmentInfo({ attachment, isFullDetail, ownerNote, noteContext, view// in place by froca, so reading it directly during render wouldn't re-render an image attachment// (whose title/content don't change when only the erasure schedule does).const [ scheduledForErasureSince, setScheduledForErasureSince ] = useState(attachment.utcDateScheduledForErasureSince);+ // Same reason, for the content itself: replacing an attachment changes neither its id nor its title, so+ // without this nothing here re-renders and the viewer/player would keep showing what it first loaded.+ const [ modified, setModified ] = useState(attachment.utcDateModified);// "importSource" attachments (e.g. OneNote debug source) behave like ordinary files for// preview, OCR and link-copying purposes.const isFileLike = attachment.role === "file" || attachment.role === "importSource";@@ -163,7 +166,7 @@ function AttachmentInfo({ attachment, isFullDetail, ownerNote, noteContext, viewconst isZoomableImage = !!isFullDetail && attachment.role === "image";const isPlayableMedia = !!isFullDetail && (attachment.mime.startsWith("audio/") || attachment.mime.startsWith("video/"));const rendersItself = isZoomableImage || isPlayableMedia;- const imageSrc = `api/attachments/${attachment.attachmentId}/image/${encodeURIComponent(attachment.title)}?${attachment.utcDateModified}`;+ const imageSrc = `api/attachments/${attachment.attachmentId}/image/${encodeURIComponent(attachment.title)}?${modified}`;/** Unmounts whatever the content renderer previously mounted here (a media player), so that replacing* or discarding the content doesn't leak its Preact root — or leave its audio playing. */@@ -189,6 +192,7 @@ function AttachmentInfo({ attachment, isFullDetail, ownerNote, noteContext, viewsetTitle(attachment.title);setScheduledForErasureSince(attachment.utcDateScheduledForErasureSince);+ setModified(attachment.utcDateModified);}useEffect(() => {@@ -267,7 +271,7 @@ function AttachmentInfo({ attachment, isFullDetail, ownerNote, noteContext, view{textContent && <TextPreview content={textContent} />}{isZoomableImage ? (<div key="image-viewer" ref={imageViewerWrapper} className="attachment-content-wrapper attachment-image-viewer">- <ImageViewer key={`${attachment.attachmentId}-${attachment.utcDateModified}`} src={imageSrc} alt={attachment.title} />+ <ImageViewer key={`${attachment.attachmentId}-${modified}`} src={imageSrc} alt={attachment.title} /><SiblingNavigatornote={ownerNote}noteContext={noteContext}diff --git a/apps/client/src/widgets/type_widgets/file/MediaPreview.spec.tsx b/apps/client/src/widgets/type_widgets/file/MediaPreview.spec.tsxindex 4e0e8dc064..a5fa24203e 100644--- a/apps/client/src/widgets/type_widgets/file/MediaPreview.spec.tsx+++ b/apps/client/src/widgets/type_widgets/file/MediaPreview.spec.tsx@@ -14,9 +14,9 @@ import type FAttachment from "../../../entities/fattachment";import type FNote from "../../../entities/fnote";import MediaPreview from "./MediaPreview";-const videoNote = { noteId: "vid1", title: "Holiday", mime: "video/mp4" } as FNote;-const audioNote = { noteId: "snd1", title: "Podcast", mime: "audio/mpeg" } as FNote;-const audioAttachment = { attachmentId: "att1", title: "Recording", mime: "audio/mpeg" } as FAttachment;+const videoNote = { noteId: "vid1", title: "Holiday", mime: "video/mp4", blobId: "blobV" } as FNote;+const audioNote = { noteId: "snd1", title: "Podcast", mime: "audio/mpeg", blobId: "blobA" } as FNote;+const audioAttachment = { attachmentId: "att1", title: "Recording", mime: "audio/mpeg", utcDateModified: "att1mod" } as FAttachment;describe("MediaPreview", () => {let container: HTMLElement;@@ -56,7 +56,7 @@ describe("MediaPreview", () => {expect(container.querySelector(".media-proxy")).toBeNull();const media = mediaElement();expect(media?.tagName).toBe("VIDEO");- expect(media?.getAttribute("src")).toBe("api/notes/vid1/open-partial");+ expect(media?.getAttribute("src")).toBe("api/notes/vid1/open-partial?v=blobV");// The media is only just being fetched, so playback waits for it to become playable.expect(play).not.toHaveBeenCalled();@@ -72,7 +72,7 @@ describe("MediaPreview", () => {const media = mediaElement();expect(media?.tagName).toBe("AUDIO");- expect(media?.getAttribute("src")).toBe("api/attachments/att1/open-partial");+ expect(media?.getAttribute("src")).toBe("api/attachments/att1/open-partial?v=att1mod");});});@@ -82,7 +82,7 @@ describe("MediaPreview", () => {expect(container.querySelector(".media-proxy")).toBeNull();const media = mediaElement();- expect(media?.getAttribute("src")).toBe("api/notes/vid1/open-partial");+ expect(media?.getAttribute("src")).toBe("api/notes/vid1/open-partial?v=blobV");// Embedded is "ready to play", not "playing".expect(media?.getAttribute("preload")).toBe("metadata");expect(play).not.toHaveBeenCalled();diff --git a/apps/client/src/widgets/type_widgets/file/media_source.spec.ts b/apps/client/src/widgets/type_widgets/file/media_source.spec.tsindex f4207b2aba..e0c32c6957 100644--- a/apps/client/src/widgets/type_widgets/file/media_source.spec.ts+++ b/apps/client/src/widgets/type_widgets/file/media_source.spec.ts@@ -4,27 +4,33 @@ import type FAttachment from "../../../entities/fattachment";import type FNote from "../../../entities/fnote";import { getMediaSource } from "./media_source";-const note = { noteId: "vid1", title: "Holiday", mime: "video/mp4" } as FNote;-const attachment = { attachmentId: "att1", title: "Recording", mime: "audio/mpeg" } as FAttachment;+const note = { noteId: "vid1", title: "Holiday", mime: "video/mp4", blobId: "blob1" } as FNote;+const attachment = { attachmentId: "att1", title: "Recording", mime: "audio/mpeg", utcDateModified: "2026-07-23 10:00:00.000Z" } as FAttachment;describe("getMediaSource", () => {- it("resolves a note to its streaming and whole-file endpoints", () => {+ it("resolves a note to its streaming and whole-file endpoints, versioned by its content hash", () => {expect(getMediaSource(note)).toEqual({id: "vid1",title: "Holiday",mime: "video/mp4",- streamUrl: "api/notes/vid1/open-partial",- fullUrl: "api/notes/vid1/open"+ streamUrl: "api/notes/vid1/open-partial?v=blob1",+ fullUrl: "api/notes/vid1/open?v=blob1"});});- it("resolves an attachment to the attachment endpoints", () => {+ it("resolves an attachment to the attachment endpoints, versioned by its modification stamp", () => {expect(getMediaSource(attachment)).toEqual({id: "att1",title: "Recording",mime: "audio/mpeg",- streamUrl: "api/attachments/att1/open-partial",- fullUrl: "api/attachments/att1/open"+ streamUrl: "api/attachments/att1/open-partial?v=2026-07-23%2010%3A00%3A00.000Z",+ fullUrl: "api/attachments/att1/open?v=2026-07-23%2010%3A00%3A00.000Z"});});++ it("re-versions the urls when the content is replaced under the same id", () => {+ // The id is unchanged, so an open player would otherwise keep streaming what it already had.+ const replaced = { ...attachment, utcDateModified: "2026-07-23 11:00:00.000Z" } as FAttachment;+ expect(getMediaSource(replaced).streamUrl).not.toBe(getMediaSource(attachment).streamUrl);+ });});diff --git a/apps/client/src/widgets/type_widgets/file/media_source.ts b/apps/client/src/widgets/type_widgets/file/media_source.tsindex 293bc9c483..5ea9c0218f 100644--- a/apps/client/src/widgets/type_widgets/file/media_source.ts+++ b/apps/client/src/widgets/type_widgets/file/media_source.ts@@ -17,12 +17,18 @@ export interface MediaSource {export function getMediaSource(entity: FNote | FAttachment): MediaSource {const isNote = "noteId" in entity;const path = isNote ? `api/notes/${entity.noteId}` : `api/attachments/${entity.attachmentId}`;+ // Replacing the media leaves its id — and so its endpoints — untouched, which would leave an open player+ // streaming what it already had. Versioning the URLs gives the element (and the waveform fetch) one they+ // haven't loaded, so the new content is picked up in place rather than by rebuilding the player around+ // them. A note carries its content hash; an attachment only exposes a modification stamp, so that one also+ // re-versions on a rename.+ const version = encodeURIComponent(isNote ? entity.blobId : entity.utcDateModified);return {id: isNote ? entity.noteId : entity.attachmentId,title: entity.title,mime: entity.mime,- streamUrl: getUrlForDownload(`${path}/open-partial`),- fullUrl: getUrlForDownload(`${path}/open`)+ streamUrl: getUrlForDownload(`${path}/open-partial?v=${version}`),+ fullUrl: getUrlForDownload(`${path}/open?v=${version}`)};}