Added data shape check for jsons

This commit is contained in:
Dale de Silva 2023-03-30 09:08:58 +11:00
parent 3ae5f3eb72
commit 5925798a96
3 changed files with 28 additions and 8 deletions

View file

@ -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);
}

View file

@ -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<this.fileBacklog.length; i++) {
const file = this.fileBacklog[i] as File;
switch(file.type) {
case 'application/json': notes++; break;
default: assets++; break;
if(fileIsJson(file)) {
notes++;
} else {
assets++;
}
}

View file

@ -22,4 +22,17 @@ export interface KeepJson {
attachments?: Array<KeepAttachment>;
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';
}