Skip to content

Commit

Permalink
Merge pull request #198 from TriliumNext/bugfix/note_import_type
Browse files Browse the repository at this point in the history
Fix import note type
  • Loading branch information
eliandoran authored Jul 13, 2024
2 parents 5460359 + 606490a commit 6ecbf1c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/becca/entities/rows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ export interface BranchRow {
* end user. Those types should be used only for checking against, they are
* not for direct use.
*/
export type NoteType = ("file" | "image" | "search" | "noteMap" | "launcher" | "doc" | "contentWidget" | "text" | "relationMap" | "render" | "canvas" | "mermaid" | "book" | "webView" | "code");
export const ALLOWED_NOTE_TYPES = [ "file", "image", "search", "noteMap", "launcher", "doc", "contentWidget", "text", "relationMap", "render", "canvas", "mermaid", "book", "webView", "code" ] as const;
export type NoteType = typeof ALLOWED_NOTE_TYPES[number];

export interface NoteRow {
noteId: string;
Expand All @@ -106,5 +107,5 @@ export interface NoteRow {
dateModified: string;
utcDateCreated: string;
utcDateModified: string;
content?: string;
content?: string | Buffer;
}
12 changes: 6 additions & 6 deletions src/services/import/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import BNote = require('../../becca/entities/bnote');
import NoteMeta = require('../meta/note_meta');
import AttributeMeta = require('../meta/attribute_meta');
import { Stream } from 'stream';
import { NoteType } from '../../becca/entities/rows';
import { ALLOWED_NOTE_TYPES, NoteType } from '../../becca/entities/rows';

interface MetaFile {
files: NoteMeta[]
Expand Down Expand Up @@ -499,10 +499,6 @@ async function importZip(taskContext: TaskContext, fileBuffer: Buffer, importRoo
}
}
else {
if (typeof content !== "string") {
throw new Error("Incorrect content type.");
}

({note} = noteService.createNewNote({
parentNoteId: parentNoteId,
title: noteTitle || "",
Expand Down Expand Up @@ -653,7 +649,11 @@ function resolveNoteType(type: string | undefined): NoteType {
return 'webView';
}

return "text";
if (type && (ALLOWED_NOTE_TYPES as readonly string[]).includes(type)) {
return type as NoteType;
} else {
return "text";
}
}

export = {
Expand Down
2 changes: 1 addition & 1 deletion src/services/note-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface NoteParams {
parentNoteId: string;
templateNoteId?: string;
title: string;
content: string;
content: string | Buffer;
/** text, code, file, image, search, book, relationMap, canvas, webView */
type: NoteType;
/** default value is derived from default mimes for type */
Expand Down
4 changes: 2 additions & 2 deletions src/services/search/expressions/note_content_fulltext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class NoteContentFulltextExp extends Expression {
}

if (isProtected) {
if (!protectedSessionService.isProtectedSessionAvailable() || !content) {
if (!protectedSessionService.isProtectedSessionAvailable() || !content || typeof content !== "string") {
return;
}

Expand All @@ -86,7 +86,7 @@ class NoteContentFulltextExp extends Expression {
}
}

if (!content) {
if (!content || typeof content !== "string") {
return;
}

Expand Down

0 comments on commit 6ecbf1c

Please sign in to comment.