diff --git a/apps/readest-app/src/__tests__/services/import-metahash.test.ts b/apps/readest-app/src/__tests__/services/import-metahash.test.ts index fdb568108..4537f9a09 100644 --- a/apps/readest-app/src/__tests__/services/import-metahash.test.ts +++ b/apps/readest-app/src/__tests__/services/import-metahash.test.ts @@ -1632,6 +1632,50 @@ describe('importBook PDF filename-aware dedup', () => { expect(book).toMatchObject({ originalFilename: 'manual.pdf (1)', title: 'manual' }); }); + // Issue #7313: web-novel downloaders save each chapter as its own HTML file + // carrying the book's title and author, so metadata alone merges chapters. + it('imports HTML chapters with identical metadata as separate books', async () => { + const books: Book[] = []; + for (const name of ['0001.html', '0002.htm']) { + mockPartialMD5.mockResolvedValue(`html-${name}`); + mockOpen.mockResolvedValue({ + book: { metadata: { ...PDF_METADATA }, getCover: vi.fn().mockResolvedValue(null) }, + format: 'HTML', + }); + const book = await service.importBook(new File(['chapter'], name), books); + expect(book!.metaHash).toBe(getMetadataHash(PDF_METADATA, name.replace(/\..*$/, ''))); + } + expect(books.filter((b) => !b.deletedAt)).toHaveLength(2); + }); + + it('salts the metaHash when re-importing an HTML book that has none', async () => { + const books: Book[] = [makeBook({ hash: 'html-old', format: 'HTML' })]; + mockPartialMD5.mockResolvedValue('html-old'); + mockOpen.mockResolvedValue({ + book: { metadata: { ...PDF_METADATA }, getCover: vi.fn().mockResolvedValue(null) }, + format: 'HTML', + }); + + const book = await service.importBook(new File(['chapter'], '0003.html'), books); + + expect(book!.metaHash).toBe(getMetadataHash(PDF_METADATA, '0003')); + }); + + it('refreshBookMetadata preserves the salted metaHash for HTML', async () => { + const book = makeBook({ hash: 'html-hash', format: 'HTML', metaHash: 'salted-import-hash' }); + const fs = service.getFs(); + fs.exists.mockResolvedValue(true); + fs.openFile.mockResolvedValue(new File(['html'], 'Test Book.html')); + mockOpen.mockResolvedValue({ + book: { metadata: { ...PDF_METADATA }, getCover: vi.fn().mockResolvedValue(null) }, + format: 'HTML', + }); + + await refreshBookMetadata(fs as unknown as Parameters[0], book); + + expect(book.metaHash).toBe('salted-import-hash'); + }); + it('refreshBookMetadata preserves the salted metaHash for PDFs', async () => { // The original filename is lost after import (files are stored under the // metadata title), so re-parsing the file cannot reproduce the salt and diff --git a/apps/readest-app/src/services/bookService.ts b/apps/readest-app/src/services/bookService.ts index 5075ce8fb..c63d6961e 100644 --- a/apps/readest-app/src/services/bookService.ts +++ b/apps/readest-app/src/services/bookService.ts @@ -52,18 +52,30 @@ import { type BookFileContentSource, } from './bookContent'; +/** + * Formats whose metaHash is salted with the import filename, because their + * metadata often doesn't tell files apart: PDF boilerplate like "PowerPoint + * Presentation" (issue #5411), and HTML chapters a web-novel downloader saves + * one per file under the book's title and author (issue #7313). A library row + * no longer knows its import filename, so the salted hash is never recomputed. + * Keep in sync with FILENAME_SALTED in readest.koplugin/readest_syncconfig.lua. + */ +function isFilenameSalted(format: Book['format']) { + return format === 'PDF' || format === 'HTML'; +} + /** * Whether a bare UUID identifier of this format can be dropped from the * import-time dedup key (issue #5959). * - * PDFs are left out: #5411 deliberately scopes their identity to the filename, - * and a library row no longer knows the filename it was imported from. + * Filename-salted formats are left out: their identity is deliberately scoped + * to the filename. * FB2/FBZ are left out: their document-info id is a GUID minted once for the * document and kept across revisions, so it is what tells two FB2s of the * same title apart (issue #7150). */ function hasVolatileIdentifiers(format: Book['format']) { - return format !== 'PDF' && format !== 'FB2' && format !== 'FBZ'; + return !isFilenameSalted(format) && format !== 'FB2' && format !== 'FBZ'; } /** @@ -732,11 +744,11 @@ export async function importBook( ? lookupIndex.byHash.get(hash) : books.find((b) => b.hash === hash); const metaHash = - format === 'PDF' && existingBook + isFilenameSalted(format) && existingBook?.metaHash ? existingBook.metaHash : getMetadataHash( loadedBook.metadata, - format === 'PDF' ? getBaseFilename(filename) : undefined, + isFilenameSalted(format) ? getBaseFilename(filename) : undefined, ); const stableHash = hasVolatileIdentifiers(format) ? getStableMetadataHash(loadedBook.metadata) @@ -1324,9 +1336,9 @@ export async function refreshBookMetadata(fs: FileSystem, book: Book): Promise