Skip to content

Commit

Permalink
refactor: cleanup uploaded files if parsing fails
Browse files Browse the repository at this point in the history
  • Loading branch information
amanharwara committed Dec 9, 2023
1 parent c5d0cdf commit 98d6e2f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/ui-services/src/Import/Converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface Converter {
item: DecryptedItemInterface<ItemContent>,
itemToLink: DecryptedItemInterface<ItemContent>,
): Promise<void>
cleanupItems(items: DecryptedItemInterface<ItemContent>[]): Promise<void>
},
): Promise<void>
}
Expand Down Expand Up @@ -54,3 +55,5 @@ export type LinkItemsFn = (
item: DecryptedItemInterface<ItemContent>,
itemToLink: DecryptedItemInterface<ItemContent>,
) => Promise<void>

export type CleanupItemsFn = (items: DecryptedItemInterface<ItemContent>[]) => Promise<void>
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,17 @@ export class EvernoteConverter implements Converter {

convert: Converter['convert'] = async (
file,
{ insertNote, insertTag, linkItems, canUploadFiles, canUseSuper, convertHTMLToSuper, readFileAsText, uploadFile },
{
insertNote,
insertTag,
linkItems,
canUploadFiles,
canUseSuper,
convertHTMLToSuper,
readFileAsText,
uploadFile,
cleanupItems,
},
) => {
const content = await readFileAsText(file)

Expand All @@ -50,6 +60,7 @@ export class EvernoteConverter implements Converter {
}

for (const [index, xmlNote] of Array.from(xmlNotes).entries()) {
const filesToPotentiallyCleanup: FileItem[] = []
try {
const title = xmlNote.getElementsByTagName('title')[0].textContent
const created = xmlNote.getElementsByTagName('created')[0]?.textContent
Expand Down Expand Up @@ -86,6 +97,7 @@ export class EvernoteConverter implements Converter {
canUploadFiles,
uploadFile,
)
filesToPotentiallyCleanup.push(...uploadedFiles)

// Some notes have <font> tags that contain separate <span> tags with text
// which causes broken paragraphs in the note.
Expand Down Expand Up @@ -140,6 +152,7 @@ export class EvernoteConverter implements Converter {
}
} catch (error) {
console.error(error)
cleanupItems(filesToPotentiallyCleanup).catch(console.error)
continue
}
}
Expand Down
16 changes: 14 additions & 2 deletions packages/ui-services/src/Import/Importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ import {
SNNote,
SNTag,
TagContent,
isFile,
isNote,
} from '@standardnotes/models'
import { HTMLConverter } from './HTMLConverter/HTMLConverter'
import { SuperConverter } from './SuperConverter/SuperConverter'
import { Converter, InsertNoteFn, InsertTagFn, LinkItemsFn, UploadFileFn } from './Converter'
import { SuperConverterServiceInterface } from '@standardnotes/files'
import { CleanupItemsFn, Converter, InsertNoteFn, InsertTagFn, LinkItemsFn, UploadFileFn } from './Converter'
import { FilesClientInterface, SuperConverterServiceInterface } from '@standardnotes/files'
import { ContentType } from '@standardnotes/domain-core'

export const BYTES_IN_ONE_MEGABYTE = 1_000_000
Expand Down Expand Up @@ -59,6 +60,7 @@ export class Importer {
): Promise<void>
},
private _generateUuid: GenerateUuid,
private files: FilesClientInterface,
) {
this.registerNativeConverters()
}
Expand Down Expand Up @@ -190,6 +192,15 @@ export class Importer {
await this.linkingController.linkItems(item, itemToLink, false)
}

cleanupItems: CleanupItemsFn = async (items) => {
for (const item of items) {
if (isFile(item)) {
await this.files.deleteFile(item)
}
await this.mutator.deleteItems([item])
}
}

canUseSuper = (): boolean => {
return (
this.features.getFeatureStatus(
Expand Down Expand Up @@ -244,6 +255,7 @@ export class Importer {
convertMarkdownToSuper: this.convertMarkdownToSuper,
readFileAsText,
linkItems: this.linkItems,
cleanupItems: this.cleanupItems,
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class WebDependencies extends DependencyContainer {
this.get<FilesController>(Web_TYPES.FilesController),
this.get<LinkingController>(Web_TYPES.LinkingController),
application.generateUuid,
application.files,
)
})

Expand Down

0 comments on commit 98d6e2f

Please sign in to comment.