diff --git a/src/logic/import-logic.ts b/src/logic/import-logic.ts index a8b56e5..58ac8b4 100644 --- a/src/logic/import-logic.ts +++ b/src/logic/import-logic.ts @@ -122,7 +122,7 @@ export class FileImporter { const settings = this.plugin.settings; this.activeImport = true; - let noteFolder, assetFolder; + let noteFolder, assetFolder, unsupportedFolder; this.totalImports = files.length; this.successCount = 0; this.failCount = 0; @@ -133,37 +133,106 @@ export class FileImporter { let result: ImportResult; if(!this.activeImport) return; + + // TODO: These could be functions + const fileIsJson = file.type === 'application/json'; + + const fileIsPlainText = file.type === 'text/plain' || + file.type === 'text/markdown' || + file.type === 'text/x-markdown'; + + const fileIsBinaryAndSupportedByKeep = file.type === 'video/3gpp' || + file.type === 'audio/amr' || + file.type === 'image/png' || + file.type === 'image/jpeg' || + file.type === 'image/jpg' || + file.type === 'image/webp' || + file.type === 'image/gif'; + + // TODO: Check all these mim-types here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types + const fileIsBinaryAndSupportedByObsidian = file.type === 'image/png' || + file.type === 'image/webp' || + file.type === 'image/jpg' || + file.type === 'image/jpeg' || + file.type === 'image/gif' || + file.type === 'image/bmp' || + file.type === 'image/svg+xml' || + file.type === 'video/mp3' || + file.type === 'video/webm' || + file.type === 'video/wav' || + file.type === 'video/m4a' || + file.type === 'video/ogg' || + file.type === 'video/3gpp' || + file.type === 'video/flac' || + file.type === 'video/mp4' || + file.type === 'video/webm' || + file.type === 'video/ogv' || + file.type === 'video/mov' || + file.type === 'video/mkv' || + file.type === 'application/pdf'; + - if(file.type === 'application/json') { + if(fileIsJson) { + // Assume is Google Keep note and attempt to import if(!noteFolder) noteFolder = await getOrCreateFolder(settings.folderNames.notes, vault); result = await importJson(vault, noteFolder, file, settings); - } else if( file.type === 'video/3gpp' || - file.type === 'audio/amr' || - file.type === 'image/png' || - file.type === 'image/jpeg' || - file.type === 'image/jpg' || - file.type === 'image/webp' || - file.type === 'image/gif' - ) { + + } else if(fileIsPlainText) { + // Import as is if(!assetFolder) assetFolder = await getOrCreateFolder(settings.folderNames.attachments, vault); result = await importBinaryFile(vault, assetFolder, file); - } else { + + } else if(fileIsBinaryAndSupportedByKeep && fileIsBinaryAndSupportedByObsidian) { + // Import as supported binary file + if(!assetFolder) assetFolder = await getOrCreateFolder(settings.folderNames.attachments, vault); + result = await importBinaryFile(vault, assetFolder, file); + + } else if(fileIsBinaryAndSupportedByKeep && !fileIsBinaryAndSupportedByObsidian) { + // Import as unsupported binary file + + if(!unsupportedFolder) unsupportedFolder = await getOrCreateFolder(settings.folderNames.unsupportedAttachments, vault); + result = await importBinaryFile(vault, unsupportedFolder, file); result = { keepFilename: file.name, - outcome: ImportOutcomeType.CreationError, - details: `This file wasn't imported because this plugin doesn\'t support importing ${file.type} files.`, + outcome: ImportOutcomeType.FormatError, + details: `This file type isn't supported by Obsidian. The file has been imported into '${settings.folderNames.unsupportedAttachments}'. Open that folder outside of Obsidian to convert or deleted those files. Any links to those files in notes will also need to be updated.`, + } + + } else if(fileIsBinaryAndSupportedByObsidian) { + // Import as supported binary file + if(!assetFolder) assetFolder = await getOrCreateFolder(settings.folderNames.attachments, vault); + result = await importBinaryFile(vault, assetFolder, file); + + } else { + // Import as unrecognised file + + if(!unsupportedFolder) unsupportedFolder = await getOrCreateFolder(settings.folderNames.unsupportedAttachments, vault); + result = await importBinaryFile(vault, unsupportedFolder, file); + result = { + keepFilename: file.name, + outcome: ImportOutcomeType.FormatError, + details: `This file's format isn't recognised. The file has been imported into '${settings.folderNames.unsupportedAttachments}'. It will appear in Obsidian if supported, otherwise you can access it outside of obsidian.`, } } + // Populate output log on error - if(result.outcome === ImportOutcomeType.CreationError || result.outcome === ImportOutcomeType.ContentError) { + if(result.outcome === ImportOutcomeType.FormatError) { + this.successCount++; + this.outputLog.push({ + status: 'Warning', + title: `${result.keepFilename}`, + desc: `${result.details} ${result.obsidianFilepath || ''}`, + }) + + } else if(result.outcome === ImportOutcomeType.CreationError || result.outcome === ImportOutcomeType.ContentError) { this.failCount++; this.outputLog.push({ status: 'Error', title: `${result.keepFilename}`, - desc: `${result.details} ${result.obsidianFilepath || ''} (${result.error})`, - // modal: importProgressModal, + desc: `${result.details} ${result.obsidianFilepath || ''} ${result.error ? '('+result.error+')' : ''}`, }) + } else { this.successCount++; } diff --git a/src/main.ts b/src/main.ts index 5700e62..854081f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -10,7 +10,8 @@ import { KeepImportSettingTab } from './tabs/settings-tab/settings-tab'; export const DEFAULT_SETTINGS: PluginSettings = { folderNames: { notes: 'Keep Imports', - attachments: 'Keep Imports/Attachments' + attachments: 'Keep Imports/Attachments', + unsupportedAttachments: 'Keep Imports/Unsupported Attachments' }, createdDate: CreatedDateTypes.googleKeep, importArchived: true, diff --git a/src/modals/start-import-modal/start-import-modal.ts b/src/modals/start-import-modal/start-import-modal.ts index 8c503e5..f31355c 100644 --- a/src/modals/start-import-modal/start-import-modal.ts +++ b/src/modals/start-import-modal/start-import-modal.ts @@ -56,7 +56,8 @@ export class StartImportModal extends Modal { attr: { 'multiple': true, 'id': 'uo_file', - 'accept': '.json, .jpg, .png, .3gp', + // 'accept': '.json, .jpg, .png, .3gp', + // 'accept': '.json, .jpg, .png, .3gp', } }) @@ -196,6 +197,5 @@ export class StartImportModal extends Modal { const {titleEl, contentEl} = this; titleEl.empty(); contentEl.empty(); - this.rejectModal('Modal closed without doing anything'); } } \ No newline at end of file diff --git a/src/types/PluginSettings.ts b/src/types/PluginSettings.ts index d2fc4cb..9cd13d0 100644 --- a/src/types/PluginSettings.ts +++ b/src/types/PluginSettings.ts @@ -6,7 +6,8 @@ export enum CreatedDateTypes { export interface PluginSettings { folderNames: { notes: string, - attachments: string + attachments: string, + unsupportedAttachments: string }, createdDate: CreatedDateTypes, importArchived: boolean, diff --git a/src/types/Results.ts b/src/types/Results.ts index 8b5f617..542e093 100644 --- a/src/types/Results.ts +++ b/src/types/Results.ts @@ -3,6 +3,7 @@ export enum ImportOutcomeType { UserIgnored, CreationError, ContentError, + FormatError, Imported, }