Fixes for Notion import (#7119)

Signed-off-by: Anna Khismatullina <anna.khismatullina@gmail.com>
This commit is contained in:
Anna Khismatullina 2024-11-08 12:41:18 +07:00 committed by GitHub
parent 54ce770ea3
commit 97412400ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 44 additions and 15 deletions

View File

@ -24,6 +24,7 @@ import {
export interface FileUploader {
uploadFile: (id: Ref<Doc>, name: string, file: File, contentType?: string) => Promise<Response>
uploadCollaborativeDoc: (id: Ref<Doc>, collabId: CollaborativeDoc, data: Buffer) => Promise<Response>
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<Doc>, name: string, file: File, contentType?: string): Promise<Response> {
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<Doc>, collabId: CollaborativeDoc, data: Buffer): Promise<Response> {
const file = new File([data], collabId)
const { documentId } = collaborativeDocParse(collabId)

View File

@ -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) {

View File

@ -409,7 +409,7 @@ async function importAttachment (
}
const file = new File([data], docMeta.name)
await fileUploader.uploadFile(docMeta.id as Ref<Doc>, docMeta.name, file)
await fileUploader.uploadFile(docMeta.id as Ref<Doc>, docMeta.id, file)
const attachedData: AttachedData<Attachment> = {
file: docMeta.id as Ref<Blob>,
@ -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<string, DocumentMetadata>): void {
function preProcessMarkdown (
json: MarkupNode,
documentMetaMap: Map<string, DocumentMetadata>,
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<string, Docu
const notionId = getFileId('', src as string)
const meta = documentMetaMap.get(notionId)
if (meta !== undefined) {
alterImageNode(node, meta)
alterImageNode(node, meta, fileUploader.getFileUrl(meta.id))
}
}
} else {
@ -561,12 +565,21 @@ function alterInternalLinkNode (node: MarkupNode, targetMeta: DocumentMetadata):
}
}
function alterImageNode (node: MarkupNode, meta: DocumentMetadata): void {
function alterImageNode (node: MarkupNode, meta: DocumentMetadata, fileUrl: string): void {
node.type = MarkupNodeType.image
if (node.attrs !== undefined) {
node.attrs['file-id'] = meta.id
if (meta.mimeType !== undefined) {
node.attrs['data-file-type'] = meta.mimeType
node.attrs = {
'file-id': meta.id,
src: fileUrl,
width: node.attrs.width ?? null,
height: node.attrs.height ?? null,
align: node.attrs.align ?? null,
alt: meta.name,
title: meta.name
}
const mimeType = getContentType(meta.name)
if (mimeType !== undefined) {
node.attrs['data-file-type'] = mimeType
}
}
}
@ -580,19 +593,27 @@ function isExternalLink (href: any): boolean {
return URL.canParse(href)
}
function safeDecodeURI (uri: string): string {
try {
return decodeURI(uri)
} catch (e) {
return uri
}
}
function extractNotionId (fileName: string): string | undefined {
const decoded = decodeURI(fileName).trimEnd()
const decoded = safeDecodeURI(fileName).trimEnd()
const matched = decoded.match(/ ([\w\d]{32}(_all)?)(\.|$)/)
return matched !== null && matched.length >= 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)
}