refactor: importer service (#2674)

This commit is contained in:
Aman Harwara
2023-12-05 02:55:32 +05:30
committed by GitHub
parent 9265e7afe9
commit 85ecb10924
17 changed files with 509 additions and 518 deletions

View File

@@ -1,22 +1,23 @@
import { ContentType } from '@standardnotes/domain-core'
import { NativeFeatureIdentifier, NoteType } from '@standardnotes/features'
import { NoteType } from '@standardnotes/features'
import { parseFileName } from '@standardnotes/filepicker'
import { SuperConverterServiceInterface } from '@standardnotes/files'
import { DecryptedTransferPayload, NoteContent } from '@standardnotes/models'
import { GenerateUuid } from '@standardnotes/services'
import { readFileAsText } from '../Utils'
import { Converter } from '../Converter'
export class HTMLConverter {
constructor(
private superConverterService: SuperConverterServiceInterface,
private _generateUuid: GenerateUuid,
) {}
export class HTMLConverter implements Converter {
constructor() {}
static isHTMLFile(file: File): boolean {
return file.type === 'text/html'
getImportType(): string {
return 'html'
}
async convertHTMLFileToNote(file: File, isEntitledToSuper: boolean): Promise<DecryptedTransferPayload<NoteContent>> {
getSupportedFileTypes(): string[] {
return ['text/html']
}
isContentValid(_content: string): boolean {
return true
}
convert: Converter['convert'] = async (file, { createNote, convertHTMLToSuper, readFileAsText }) => {
const content = await readFileAsText(file)
const { name } = parseFileName(file.name)
@@ -24,28 +25,16 @@ export class HTMLConverter {
const createdAtDate = file.lastModified ? new Date(file.lastModified) : new Date()
const updatedAtDate = file.lastModified ? new Date(file.lastModified) : new Date()
const text = isEntitledToSuper
? this.superConverterService.convertOtherFormatToSuperString(content, 'html')
: content
const text = convertHTMLToSuper(content)
return {
created_at: createdAtDate,
created_at_timestamp: createdAtDate.getTime(),
updated_at: updatedAtDate,
updated_at_timestamp: updatedAtDate.getTime(),
uuid: this._generateUuid.execute().getValue(),
content_type: ContentType.TYPES.Note,
content: {
return [
createNote({
createdAt: createdAtDate,
updatedAt: updatedAtDate,
title: name,
text,
references: [],
...(isEntitledToSuper
? {
noteType: NoteType.Super,
editorIdentifier: NativeFeatureIdentifier.TYPES.SuperEditor,
}
: {}),
},
}
noteType: NoteType.Super,
}),
]
}
}