From 97412400ff50a8c1227733bb919f81607da365de Mon Sep 17 00:00:00 2001 From: Anna Khismatullina Date: Fri, 8 Nov 2024 12:41:18 +0700 Subject: [PATCH] Fixes for Notion import (#7119) Signed-off-by: Anna Khismatullina --- dev/import-tool/src/importer/uploader.ts | 10 ++++- dev/import-tool/src/index.ts | 2 +- dev/import-tool/src/notion/notion.ts | 47 +++++++++++++++++------- 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/dev/import-tool/src/importer/uploader.ts b/dev/import-tool/src/importer/uploader.ts index 2edf7b2f25..64269f1176 100644 --- a/dev/import-tool/src/importer/uploader.ts +++ b/dev/import-tool/src/importer/uploader.ts @@ -24,6 +24,7 @@ import { export interface FileUploader { uploadFile: (id: Ref, name: string, file: File, contentType?: string) => Promise uploadCollaborativeDoc: (id: Ref, collabId: CollaborativeDoc, data: Buffer) => Promise + getFileUrl: (id: string) => string } export interface UploadResult { @@ -34,8 +35,11 @@ export interface UploadResult { export class FrontFileUploader implements FileUploader { constructor ( private readonly frontUrl: string, + private readonly workspaceId: string, private readonly token: string - ) {} + ) { + this.getFileUrl = this.getFileUrl.bind(this) + } public async uploadFile (id: Ref, name: string, file: File, contentType?: string): Promise { const form = new FormData() @@ -55,6 +59,10 @@ export class FrontFileUploader implements FileUploader { }) } + public getFileUrl (id: string): string { + return concatLink(this.frontUrl, `/files/${this.workspaceId}/${id}?file=${id}&workspace=${this.workspaceId}`) + } + public async uploadCollaborativeDoc (id: Ref, collabId: CollaborativeDoc, data: Buffer): Promise { const file = new File([data], collabId) const { documentId } = collaborativeDocParse(collabId) diff --git a/dev/import-tool/src/index.ts b/dev/import-tool/src/index.ts index cf8ab5f456..df3759fa5d 100644 --- a/dev/import-tool/src/index.ts +++ b/dev/import-tool/src/index.ts @@ -78,7 +78,7 @@ export function importTool (): void { return } const client = new TxOperations(connection, acc._id) - const fileUploader = new FrontFileUploader(getFrontUrl(), selectedWs.token) + const fileUploader = new FrontFileUploader(getFrontUrl(), selectedWs.workspaceId, selectedWs.token) try { await f(client, fileUploader) } catch (err: any) { diff --git a/dev/import-tool/src/notion/notion.ts b/dev/import-tool/src/notion/notion.ts index 1ac2aedb0f..7af6e37082 100644 --- a/dev/import-tool/src/notion/notion.ts +++ b/dev/import-tool/src/notion/notion.ts @@ -409,7 +409,7 @@ async function importAttachment ( } const file = new File([data], docMeta.name) - await fileUploader.uploadFile(docMeta.id as Ref, docMeta.name, file) + await fileUploader.uploadFile(docMeta.id as Ref, docMeta.id, file) const attachedData: AttachedData = { file: docMeta.id as Ref, @@ -442,7 +442,7 @@ async function importPageDocument ( const md = data.toString() ?? '' const json = parseMessageMarkdown(md ?? '', 'image://') if (documentMetaMap !== undefined) { - preProcessMarkdown(json, documentMetaMap) + preProcessMarkdown(json, documentMetaMap, fileUploader) } const yDoc = jsonToYDocNoSchema(json, 'content') const buffer = yDocToBuffer(yDoc) @@ -472,7 +472,11 @@ async function importPageDocument ( await client.createDoc(document.class.Document, space, attachedData, id) } -function preProcessMarkdown (json: MarkupNode, documentMetaMap: Map): void { +function preProcessMarkdown ( + json: MarkupNode, + documentMetaMap: Map, + fileUploader: FileUploader +): void { traverseNode(json, (node) => { if (node.type === MarkupNodeType.image) { const src = node.attrs?.src @@ -480,7 +484,7 @@ function preProcessMarkdown (json: MarkupNode, documentMetaMap: Map= 2 ? matched[1] : undefined } function extractExtension (fileName: string): string { - const decoded = decodeURI(fileName) + const decoded = safeDecodeURI(fileName) return parse(decoded).ext.toLowerCase() } function extractNameWoExtension (fileName: string): string { - const decoded = decodeURI(fileName) + const decoded = safeDecodeURI(fileName) return parse(decoded).name } @@ -608,8 +629,8 @@ function getFileId (filePath: string, fileName: string): string { if (notionId !== '' && notionId !== undefined) { return notionId } - const decodedPath = decodeURI(filePath) - const decodedName = decodeURI(fileName) + const decodedPath = safeDecodeURI(filePath) + const decodedName = safeDecodeURI(fileName) return join(basename(decodedPath), decodedName) }