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
fix(import): derive Anytype file MIME from extension, not its metadata
Elian Doran95d76ff7098f
diff --git a/packages/trilium-core/src/services/import/anytype/collection.ts b/packages/trilium-core/src/services/import/anytype/collection.tsindex 953a61833f..56306c12d8 100644--- a/packages/trilium-core/src/services/import/anytype/collection.ts+++ b/packages/trilium-core/src/services/import/anytype/collection.ts@@ -19,6 +19,7 @@ import type BNote from "../../../becca/entities/bnote.js";import noteService from "../../notes.js";import { basename } from "../../utils/path.js";import { applyUrlScheme, attachmentReferenceLink, buildPromotedDefinition, saveFileAttachment, toAttributeName } from "../collection_utils.js";+import mimeService from "../mime.js";import type { AnytypeBlock, AnytypeDetails, AnytypeSnapshot, CollectionViewType, FileObjectInfo, Multiplicity, ParsedCollection, ParsedColumn, ParsedObject, ParsedProperty, PropertyLabelType, RelationInfo } from "./model.js";/** Normalizes a zip entry / file path to forward slashes and lower case (Windows exports use backslashes),@@ -62,7 +63,10 @@ export function buildFileObjectMap(fileObjects: AnytypeSnapshot[]): Map<string,const source = details.source ?? "";// The attachment title is the file's name: the source's base name, or the name + extension.const title = basename(normalizePath(source)) || `${details.name ?? "file"}${details.fileExt ? `.${details.fileExt}` : ""}`;- map.set(details.id, { title, mime: details.fileMimeType ?? "", source });+ // Derive the MIME from the file name first (Anytype's own `fileMimeType` is unreliable — e.g. it tags+ // a PDF as text/plain); fall back to Anytype's value only when the extension is unknown.+ const mime = (mimeService.getMime(title) || details.fileMimeType) ?? "";+ map.set(details.id, { title, mime, source });}return map;}diff --git a/packages/trilium-core/src/services/import/anytype/importer.integration.spec.ts b/packages/trilium-core/src/services/import/anytype/importer.integration.spec.tsindex 35c1f40225..162f5bd482 100644--- a/packages/trilium-core/src/services/import/anytype/importer.integration.spec.ts+++ b/packages/trilium-core/src/services/import/anytype/importer.integration.spec.ts@@ -742,18 +742,20 @@ describe("Anytype importer — integration", () => {"objects/coll.pb.json": collectionObject("coll", "Files", ["m1"], [fileKey]),"objects/m1.pb.json": memberObject("m1", { [fileKey]: [fileCid] }),"relations/file.pb.json": relationObject(fileKey, "File", 5),- "filesObjects/f1.pb.json": fileObjectJson(fileCid, "log", "txt", "text/csv", "files\\log.txt"),- "files/log.txt": "hello,world\n1,2\n"+ // Anytype mis-tags the MIME (text/plain for a .csv); the importer derives it from the extension.+ "filesObjects/f1.pb.json": fileObjectJson(fileCid, "log", "csv", "text/plain", "files\\log.csv"),+ "files/log.csv": "hello,world\n1,2\n"});// A file property is never a column.const collection = importRoot.getChildNotes()[0];expect(collection.getOwnedAttributes().filter((a) => a.name.startsWith("label:"))).toHaveLength(0);- // The file is a role:"file" attachment, titled from the file's name, with its bytes preserved.+ // The file is a role:"file" attachment, titled from the file's name, with its bytes preserved and its+ // MIME derived from the extension (not Anytype's unreliable text/plain).const row = collection.getChildNotes()[0];const attachments = row.getAttachmentsByRole("file");- expect(attachments.map((a) => a.title)).toEqual(["log.txt"]);+ expect(attachments.map((a) => a.title)).toEqual(["log.csv"]);expect(attachments[0].mime).toBe("text/csv");expect(decodeUtf8(attachments[0].getContent() ?? "")).toBe("hello,world\n1,2\n");@@ -761,7 +763,7 @@ describe("Anytype importer — integration", () => {const content = decodeUtf8(row.getContent() ?? "");expect(content).toContain(`class="reference-link"`);expect(content).toContain(`href="#root/${row.noteId}?viewMode=attachments&attachmentId=${attachments[0].attachmentId}"`);- expect(content).toContain(">log.txt</a>");+ expect(content).toContain(">log.csv</a>");});it("imports a file object that's a collection member as a file note under the collection", async () => {@@ -770,7 +772,9 @@ describe("Anytype importer — integration", () => {const fileCid = "bafyreifilemember";const importRoot = await importAnytype({"objects/coll.pb.json": collectionObject("coll", "Docs", [fileCid], []),- "filesObjects/f1.pb.json": fileObjectJson(fileCid, "report", "pdf", "application/pdf", "files\\report.pdf"),+ // Anytype records a wrong MIME for the PDF (verbatim from the real export: text/plain); the importer+ // must derive application/pdf from the extension instead.+ "filesObjects/f1.pb.json": fileObjectJson(fileCid, "report", "pdf", "text/plain", "files\\report.pdf"),"files/report.pdf": Buffer.from("%PDF-1.4 hi")});