From 01db8f90fa0a8683c2593a7a934b9d62cb13cc23 Mon Sep 17 00:00:00 2001 From: Huang Xin Date: Sun, 30 Mar 2025 16:27:45 +0800 Subject: [PATCH] fix: store the filepath for transient import, closes #760 (#768) --- apps/readest-app/src/services/appService.ts | 18 +++++++++++++++++- apps/readest-app/src/types/book.ts | 4 +++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/apps/readest-app/src/services/appService.ts b/apps/readest-app/src/services/appService.ts index 8352471e..1c32bfb7 100644 --- a/apps/readest-app/src/services/appService.ts +++ b/apps/readest-app/src/services/appService.ts @@ -140,6 +140,10 @@ export abstract class BaseAppService implements AppService { let filename: string; let fileobj: File; + if (transient && typeof file !== 'string') { + throw new Error('Transient import is only supported for file paths'); + } + try { if (typeof file === 'string') { filename = file; @@ -184,7 +188,11 @@ export abstract class BaseAppService implements AppService { if (!(await this.fs.exists(getDir(book), 'Books'))) { await this.fs.createDir(getDir(book), 'Books'); } - if (saveBook && (!(await this.fs.exists(getLocalBookFilename(book), 'Books')) || overwrite)) { + if ( + saveBook && + !transient && + (!(await this.fs.exists(getLocalBookFilename(book), 'Books')) || overwrite) + ) { if (typeof file === 'string' && !isValidURL(file) && !filename.endsWith('.txt')) { await this.fs.copyFile(file, getLocalBookFilename(book), 'Books'); } else { @@ -204,8 +212,14 @@ export abstract class BaseAppService implements AppService { } else { existingBook.title = book.title; existingBook.author = book.author; + if (transient) { + existingBook.filePath = filename; + } } + if (transient) { + book.filePath = filename; + } if (typeof file === 'string' && isValidURL(file)) { book.url = file; } @@ -369,6 +383,8 @@ export abstract class BaseAppService implements AppService { } else { file = await new RemoteFile(this.fs.getURL(`${this.localBooksDir}/${fp}`), fp).open(); } + } else if (book.filePath) { + file = await new RemoteFile(this.fs.getURL(book.filePath), fp).open(); } else if (book.url) { file = await new RemoteFile(book.url).open(); } else { diff --git a/apps/readest-app/src/types/book.ts b/apps/readest-app/src/types/book.ts index 0e457618..4163a81a 100644 --- a/apps/readest-app/src/types/book.ts +++ b/apps/readest-app/src/types/book.ts @@ -4,8 +4,10 @@ export type HighlightStyle = 'highlight' | 'underline' | 'squiggly'; export type HighlightColor = 'red' | 'yellow' | 'green' | 'blue' | 'violet'; export interface Book { - // if Book is a remote book we just lazy load the book content + // if Book is a remote book we just lazy load the book content via url url?: string; + // if Book is a transient local book we can load the book content via filePath + filePath?: string; hash: string; format: BookFormat; title: string;