Skip to content

Build your personal knowledge base with Trilium Notes

fix(export): replace a picture's extension instead of adding to it

Elian Doran0270e50e0248
diff --git a/packages/trilium-core/src/services/export/zip.spec.ts b/packages/trilium-core/src/services/export/zip.spec.ts
index 933a3a7c71..9b5d5de845 100644
--- a/packages/trilium-core/src/services/export/zip.spec.ts
+++ b/packages/trilium-core/src/services/export/zip.spec.ts
@@ -322,14 +322,28 @@ describe.skipIf(isBrowserRuntime)("zip export (real DB)", () => {
const { entries } = await exportSubtree(branch, "markdown");
const rootMeta = parseMeta(entries).files[0];
+ // Named exactly, not merely ending in the right thing: the extension replaces the one
+ // the title carried rather than being appended to it, and "image.png.jpg" would pass
+ // any check that only looked at how the name ends.
const attFileName = (rootMeta.attachments ?? [])[0]?.dataFileName ?? "";
- expect(attFileName).toMatch(/\.jpe?g$/);
- expect(attFileName).not.toMatch(/\.png$/);
+ expect(attFileName).toBe("ConvertedImageHost_image.jpg");
// The renamed file is the one actually written, and its bytes are the JPEG's.
expect(entries[attFileName]).toBeDefined();
expect(Buffer.compare(entries[attFileName], jpegBytes)).toBe(0);
});
+ it("keeps a dotted title whole when the extension is added rather than substituted", async () => {
+ // `extname` reads "v0.48" as ending in ".48", and a release note is called exactly that.
+ // Only a picture's extension is replaced; anywhere else the name is left alone and the
+ // new extension is appended, so this must not export as "v0.md".
+ const { note } = createNote("root", { title: "v0.48", content: "<p>changelog</p>" });
+ const branch = note.getParentBranches()[0];
+
+ const { entries } = await exportSubtree(branch, "markdown");
+
+ expect(parseMeta(entries).files[0].dataFileName).toBe("v0.48.md");
+ });
+
it("round-trips binary attachment content byte-for-byte", async () => {
const { note } = createNote("root", { title: "BinaryAttachHost", content: "<p>host</p>" });
// Bytes that are not valid UTF-8 (0x00, 0xFF, lone 0x80 continuation byte)
diff --git a/packages/trilium-core/src/services/export/zip.ts b/packages/trilium-core/src/services/export/zip.ts
index b99653a7d4..c3d7404cc2 100644
--- a/packages/trilium-core/src/services/export/zip.ts
+++ b/packages/trilium-core/src/services/export/zip.ts
@@ -1,4 +1,4 @@
-import { getImageAttachmentTitle, NoteType } from "@triliumnext/commons";
+import { getImageAttachmentTitle, imageMimeForExtension, isAcceptedImageMime, NoteType } from "@triliumnext/commons";
import sanitize from "sanitize-filename";
import packageInfo from "../../../package.json" with { type: "json" };
@@ -81,7 +81,8 @@ async function exportToZip(taskContext: TaskContext<"export">, branch: BBranch,
// name so the whole entry (extension included) stays within the 255-byte
// filesystem limit without ever chopping the extension off long / multi-byte
// titles and attachment names. This replaces the old arbitrary 30-char cap.
- const base = truncateUtf8Bytes(sanitize(fileName), MAX_FILENAME_BYTES - extension.length);
+ const named = dropReplacedPictureExtension(fileName, existingExtension, newExtension);
+ const base = truncateUtf8Bytes(sanitize(named), MAX_FILENAME_BYTES - extension.length);
return getUniqueFilename(existingFileNames, `${base}${extension}`);
}
@@ -598,6 +599,33 @@ export function shouldStoreUncompressed(mime: string | undefined | null): boolea
return ALREADY_COMPRESSED.has(m) || isZipBasedContainer(m);
}
+/**
+ * The name with the extension the new one is about to replace taken off it, where that is what is
+ * happening — otherwise the name unchanged.
+ *
+ * A picture is the one thing whose extension gets *substituted* rather than added to: an image
+ * converted to JPEG on upload keeps a `.png` title, and the export renames it. Appending there
+ * produces `image.png.jpg`.
+ *
+ * Everywhere else the extension is only ever added to a name that had none of its own, and taking
+ * a trailing dot-suffix off would do real damage: `extname` reads "v0.48" as ending in ".48", so a
+ * release note by that name would export as "v0.md". Restricting this to a picture replacing a
+ * picture is what keeps a suffix that merely looks like an extension out of reach — the check is
+ * against the media types those extensions name, not against the text.
+ */
+function dropReplacedPictureExtension(fileName: string, existingExtension: string, newExtension: string | null): string {
+ if (!existingExtension || !newExtension || !namesPicture(existingExtension) || !namesPicture(newExtension)) {
+ return fileName;
+ }
+
+ return fileName.slice(0, -existingExtension.length);
+}
+
+/** Whether an extension is one a picture is written with, dotted or not. */
+function namesPicture(extension: string): boolean {
+ return !!extension && isAcceptedImageMime(imageMimeForExtension(extension));
+}
+
// Office / e-book / package formats that are ZIP containers under the hood.
function isZipBasedContainer(mime: string): boolean {
return (