fix: store the filepath for transient import, closes #760 (#768)

This commit is contained in:
Huang Xin
2025-03-30 16:27:45 +08:00
committed by GitHub
parent e70c46ff02
commit 01db8f90fa
2 changed files with 20 additions and 2 deletions
+17 -1
View File
@@ -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 {
+3 -1
View File
@@ -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;