From 5925798a967006ba8f4b914fa06a36a7bcef9125 Mon Sep 17 00:00:00 2001 From: Dale de Silva Date: Thu, 30 Mar 2023 09:08:58 +1100 Subject: [PATCH] Added data shape check for jsons --- src/logic/import-logic.ts | 15 ++++++++++----- .../start-import-modal/start-import-modal.ts | 8 +++++--- src/types/keep-data.ts | 13 +++++++++++++ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/logic/import-logic.ts b/src/logic/import-logic.ts index af4e126..815c914 100644 --- a/src/logic/import-logic.ts +++ b/src/logic/import-logic.ts @@ -3,7 +3,7 @@ import MyPlugin from "src/main"; import { ImportProgressModal } from "src/modals/import-progress-modal/import-progress-modal"; import { filenameSanitize } from "./string-processes"; import { CreatedDateTypes, PluginSettings } from "src/types/plugin-settings"; -import { KeepJson } from "src/types/keep-data"; +import { KeepJson, objectIsKeepJson } from "src/types/keep-data"; import { IgnoreImportReason, ImportResult, ImportOutcomeType } from "src/types/results"; import { StartImportModal } from "src/modals/start-import-modal/start-import-modal"; @@ -246,8 +246,9 @@ export class FileImporter { /** * Returns if a file is a JSON file. */ -function fileIsJson(file: File) { - return file.type === 'application/json'; +export function fileIsJson(file: File) { + const ext = file.name.slice(-5); + return ext === '.json' || file.type === 'application/json'; } /** @@ -256,7 +257,6 @@ function fileIsJson(file: File) { */ function fileIsMarkdown(file: File) { const ext = file.name.slice(-3); - return ext === '.md' || file.type === 'text/markdown' || file.type === 'text/x-markdown'; @@ -335,7 +335,12 @@ async function importJson(vault: Vault, folder: TFolder, file: File, settings: P } const content: KeepJson = JSON.parse(readerEvent.target.result as string); - // TODO: resolve with error if file is not Keep Json + + if(!objectIsKeepJson(content)) { + result.outcome = ImportOutcomeType.ContentError; + result.details = `JSON file doesn't match the expected Google Keep format.`; + return resolve(result); + } diff --git a/src/modals/start-import-modal/start-import-modal.ts b/src/modals/start-import-modal/start-import-modal.ts index a9b7a3e..097abcf 100644 --- a/src/modals/start-import-modal/start-import-modal.ts +++ b/src/modals/start-import-modal/start-import-modal.ts @@ -4,6 +4,7 @@ import MyPlugin from "src/main"; import { EditSettingsModal } from "../edit-settings-modal/edit-settings-modal"; import { SupportButtonSet } from 'src/components/support-button-set/support-button-set'; import { ImportSummary } from "src/components/import-summary/import-summary"; +import { fileIsJson } from "src/logic/import-logic"; /////////////////// @@ -217,9 +218,10 @@ export class StartImportModal extends Modal { for(let i=0; i; title: string; userEditedTimestampUsec: number; +} + +/** + * Returns if an imported json matches the Keep interface shape at runtime. + */ +export function objectIsKeepJson(fileContents: KeepJson) { + return typeof fileContents.color !== 'undefined' && + typeof fileContents.isTrashed !== 'undefined' && + typeof fileContents.isPinned !== 'undefined' && + typeof fileContents.isArchived !== 'undefined' && + typeof fileContents.title !== 'undefined' && + typeof fileContents.userEditedTimestampUsec !== 'undefined' && + typeof fileContents.createdTimestampUsec !== 'undefined'; } \ No newline at end of file