From fe310342d0712747e1c0b19b01c773ae973b1254 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 9 Oct 2022 12:12:44 +0530 Subject: [PATCH] Content server: When adding books, run all file type plugins before reading metadata. Fixes #1992244 [Metadata wrong for books added through the web server](https://bugs.launchpad.net/calibre/+bug/1992244) --- src/calibre/srv/cdb.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/calibre/srv/cdb.py b/src/calibre/srv/cdb.py index 92f36ccf73..944da9e363 100644 --- a/src/calibre/srv/cdb.py +++ b/src/calibre/srv/cdb.py @@ -3,6 +3,8 @@ import os +import shutil +import time from functools import partial from io import BytesIO @@ -91,11 +93,15 @@ def cdb_add_book(ctx, rd, job_id, add_duplicates, filename, library_id): raise HTTPBadRequest('A request body containing the file data must be specified') add_duplicates = add_duplicates in ('y', '1') path = os.path.join(rd.tdir, sfilename) - rd.request_body_file.name = path rd.request_body_file.seek(0) - mi = get_metadata(rd.request_body_file, stream_type=fmt, use_libprs_metadata=True) - rd.request_body_file.seek(0) - ids, duplicates = db.add_books([(mi, {fmt: rd.request_body_file})], add_duplicates=add_duplicates) + with open(path, 'wb') as f: + shutil.copyfileobj(rd.request_body_file, f) + from calibre.ebooks.metadata.worker import run_import_plugins + path = run_import_plugins((path,), time.monotonic_ns(), rd.tdir)[0] + with open(path, 'rb') as f: + mi = get_metadata(f, stream_type=os.path.splitext(path)[1][1:], use_libprs_metadata=True) + f.seek(0) + ids, duplicates = db.add_books([(mi, {fmt: f})], add_duplicates=add_duplicates) ans = {'title': mi.title, 'authors': mi.authors, 'languages': mi.languages, 'filename': filename, 'id': job_id} if ids: ans['book_id'] = ids[0]