From a32fa112677162fa23de1c69fed341cfe2254df0 Mon Sep 17 00:00:00 2001 From: Blondel MONDESIR Date: Thu, 12 Sep 2024 12:04:32 -0400 Subject: [PATCH 1/7] Map media_id with relevant book_id --- cps/editbooks.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cps/editbooks.py b/cps/editbooks.py index d8336f04a7..eaa8fb070f 100644 --- a/cps/editbooks.py +++ b/cps/editbooks.py @@ -49,6 +49,7 @@ from .file_helper import validate_mime_type from .usermanagement import user_login_required, login_required_if_no_ano from .string_helper import strip_whitespaces +from .xb import add_book_media_mapping editbook = Blueprint('edit-book', __name__) log = logger.create() @@ -233,7 +234,7 @@ def create_shelf(current_user_name=None, shelf_title=None): return resp log.info("Received metadata request: %s", request.args) - def move_mediafile(requested_file, current_user_name=None, shelf_id=None): + def move_mediafile(requested_file, current_user_name=None, shelf_id=None, media_id=None): log.info("Requested file: %s", requested_file) requested_file = open(requested_file, "rb") requested_file.filename = os.path.basename(requested_file.name) @@ -293,6 +294,8 @@ def move_mediafile(requested_file, current_user_name=None, shelf_id=None): book_path = db_book.path + add_book_media_mapping(book_id, media_id) + except (OperationalError, IntegrityError, StaleDataError) as e: calibre_db.session.rollback() log.error_or_exception("Database error: {}".format(e)) @@ -313,7 +316,7 @@ def move_mediafile(requested_file, current_user_name=None, shelf_id=None): current_user_name = request.args.get("current_user_name", None) shelf_id = request.args.get("shelf_id", None) try : - resp = move_mediafile(requested_file, current_user_name, shelf_id) + resp = move_mediafile(requested_file, current_user_name, shelf_id, media_id) return jsonify(resp) except Exception as ex: log.error_or_exception(ex) From c2d7daa168e10d66d3901f552fede19060757993 Mon Sep 17 00:00:00 2001 From: Blondel MONDESIR Date: Thu, 12 Sep 2024 21:59:45 -0400 Subject: [PATCH 2/7] Get media_id from request --- cps/editbooks.py | 1 + 1 file changed, 1 insertion(+) diff --git a/cps/editbooks.py b/cps/editbooks.py index eaa8fb070f..d668483572 100644 --- a/cps/editbooks.py +++ b/cps/editbooks.py @@ -315,6 +315,7 @@ def move_mediafile(requested_file, current_user_name=None, shelf_id=None, media_ requested_file = request.args.get("requested_file", None) current_user_name = request.args.get("current_user_name", None) shelf_id = request.args.get("shelf_id", None) + media_id = request.args.get("media_id", None) try : resp = move_mediafile(requested_file, current_user_name, shelf_id, media_id) return jsonify(resp) From f23b742c6fcb9625bbd08b002aad6954c8de4f9d Mon Sep 17 00:00:00 2001 From: Blondel MONDESIR Date: Thu, 12 Sep 2024 22:02:53 -0400 Subject: [PATCH 3/7] Set and fetch media_id --- cps/tasks/download.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/cps/tasks/download.py b/cps/tasks/download.py index d5d28945d8..425af518ab 100644 --- a/cps/tasks/download.py +++ b/cps/tasks/download.py @@ -20,6 +20,7 @@ def __init__(self, task_message, media_url, original_url, current_user_name, she self.message = task_message self.media_url = media_url self.media_url_link = f'{media_url}' + self.media_id = None self.original_url = original_url self.current_user_name = current_user_name self.shelf_id = shelf_id @@ -89,7 +90,8 @@ def run(self, worker_thread): # Database operations with sqlite3.connect(XKLB_DB_FILE) as conn: try: - requested_file = conn.execute("SELECT path FROM media WHERE webpath = ? AND path NOT LIKE 'http%'", (self.media_url,)).fetchone()[0] + # requested_file = conn.execute("SELECT path FROM media WHERE webpath = ? AND path NOT LIKE 'http%'", (self.media_url,)).fetchone()[0] + self.media_id, requested_file = conn.execute("SELECT id, path FROM media WHERE webpath = ? AND path NOT LIKE 'http%'", (self.media_url,)).fetchone() # Abort if there is not a path if not requested_file: @@ -101,16 +103,16 @@ def run(self, worker_thread): else: log.error("%s failed to download: No path or error found in the database (likely the video failed due to unavailable fragments?)", self.media_url) self.message = f"{self.media_url_link} failed to download: No path or error found in the database (likely the video failed due to unavailable fragments?)" - media_id = conn.execute("SELECT id FROM media WHERE webpath = ?", (self.media_url,)).fetchone()[0] + self.media_id = conn.execute("SELECT id FROM media WHERE webpath = ?", (self.media_url,)).fetchone()[0] conn.execute("DELETE FROM media WHERE webpath = ?", (self.media_url,)) - conn.execute("DELETE FROM captions WHERE media_id = ?", (media_id,)) + conn.execute("DELETE FROM captions WHERE media_id = ?", (self.media_id,)) return except sqlite3.Error as db_error: log.error("An error occurred while trying to connect to the database: %s", db_error) self.message = f"{self.media_url_link} failed to download: {db_error}" self.message = self.message + "\n" + f"Almost done..." - response = requests.get(self.original_url, params={"requested_file": requested_file, "current_user_name": self.current_user_name, "shelf_id": self.shelf_id}) + response = requests.get(self.original_url, params={"requested_file": requested_file, "current_user_name": self.current_user_name, "shelf_id": self.shelf_id, "media_id": self.media_id}) if response.status_code == 200: log.info("Successfully sent the requested file to %s", self.original_url) file_downloaded = response.json()["file_downloaded"] From f785a4966f562e75125d63cb1360f13da7fa3426 Mon Sep 17 00:00:00 2001 From: Blondel MONDESIR Date: Tue, 24 Sep 2024 22:10:37 -0400 Subject: [PATCH 4/7] Remove mapping for now --- cps/editbooks.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cps/editbooks.py b/cps/editbooks.py index 2b9ea99aaa..7de038d12a 100644 --- a/cps/editbooks.py +++ b/cps/editbooks.py @@ -49,7 +49,7 @@ from .file_helper import validate_mime_type from .usermanagement import user_login_required, login_required_if_no_ano from .string_helper import strip_whitespaces -from .xb import add_book_media_mapping +# from .xb import add_book_media_mapping editbook = Blueprint('edit-book', __name__) log = logger.create() @@ -286,7 +286,7 @@ def move_mediafile(requested_file, current_user_name=None, shelf_id=None, media_ link = '{}'.format( url_for("web.show_book", book_id=book_id), escape(title) ) - + helper.add_book_to_thumbnail_cache(book_id) if shelf_id is not None: @@ -294,7 +294,7 @@ def move_mediafile(requested_file, current_user_name=None, shelf_id=None, media_ book_path = db_book.path - add_book_media_mapping(book_id, media_id) + # add_book_media_mapping(book_id, media_id) except (OperationalError, IntegrityError, StaleDataError) as e: calibre_db.session.rollback() @@ -322,7 +322,7 @@ def move_mediafile(requested_file, current_user_name=None, shelf_id=None, media_ except Exception as ex: log.error_or_exception(ex) return jsonify({"error": str(ex)}), 500 - + if request.method == "GET" and "shelf_title" in request.args: shelf_title = request.args.get("shelf_title", None) current_user_name = request.args.get("current_user_name", None) From 1850537342c1af6ee748507f19522228ef09806d Mon Sep 17 00:00:00 2001 From: Blondel MONDESIR Date: Mon, 14 Oct 2024 11:39:11 -0400 Subject: [PATCH 5/7] Enable mapping between media_id and book_id --- cps/editbooks.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cps/editbooks.py b/cps/editbooks.py index 7de038d12a..1155ebd780 100644 --- a/cps/editbooks.py +++ b/cps/editbooks.py @@ -49,7 +49,7 @@ from .file_helper import validate_mime_type from .usermanagement import user_login_required, login_required_if_no_ano from .string_helper import strip_whitespaces -# from .xb import add_book_media_mapping +from .services.xb_utils import MappingService editbook = Blueprint('edit-book', __name__) log = logger.create() @@ -294,7 +294,7 @@ def move_mediafile(requested_file, current_user_name=None, shelf_id=None, media_ book_path = db_book.path - # add_book_media_mapping(book_id, media_id) + MappingService().add_book_media_mapping(media_id, book_id) except (OperationalError, IntegrityError, StaleDataError) as e: calibre_db.session.rollback() From 84f83482e8b4578f1f6ff9a0ab36bff154a16a53 Mon Sep 17 00:00:00 2001 From: Blondel MONDESIR Date: Sat, 19 Oct 2024 16:39:32 -0400 Subject: [PATCH 6/7] Revert changes to avoid conflict with #259 --- cps/tasks/download.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/cps/tasks/download.py b/cps/tasks/download.py index 425af518ab..d5d28945d8 100644 --- a/cps/tasks/download.py +++ b/cps/tasks/download.py @@ -20,7 +20,6 @@ def __init__(self, task_message, media_url, original_url, current_user_name, she self.message = task_message self.media_url = media_url self.media_url_link = f'{media_url}' - self.media_id = None self.original_url = original_url self.current_user_name = current_user_name self.shelf_id = shelf_id @@ -90,8 +89,7 @@ def run(self, worker_thread): # Database operations with sqlite3.connect(XKLB_DB_FILE) as conn: try: - # requested_file = conn.execute("SELECT path FROM media WHERE webpath = ? AND path NOT LIKE 'http%'", (self.media_url,)).fetchone()[0] - self.media_id, requested_file = conn.execute("SELECT id, path FROM media WHERE webpath = ? AND path NOT LIKE 'http%'", (self.media_url,)).fetchone() + requested_file = conn.execute("SELECT path FROM media WHERE webpath = ? AND path NOT LIKE 'http%'", (self.media_url,)).fetchone()[0] # Abort if there is not a path if not requested_file: @@ -103,16 +101,16 @@ def run(self, worker_thread): else: log.error("%s failed to download: No path or error found in the database (likely the video failed due to unavailable fragments?)", self.media_url) self.message = f"{self.media_url_link} failed to download: No path or error found in the database (likely the video failed due to unavailable fragments?)" - self.media_id = conn.execute("SELECT id FROM media WHERE webpath = ?", (self.media_url,)).fetchone()[0] + media_id = conn.execute("SELECT id FROM media WHERE webpath = ?", (self.media_url,)).fetchone()[0] conn.execute("DELETE FROM media WHERE webpath = ?", (self.media_url,)) - conn.execute("DELETE FROM captions WHERE media_id = ?", (self.media_id,)) + conn.execute("DELETE FROM captions WHERE media_id = ?", (media_id,)) return except sqlite3.Error as db_error: log.error("An error occurred while trying to connect to the database: %s", db_error) self.message = f"{self.media_url_link} failed to download: {db_error}" self.message = self.message + "\n" + f"Almost done..." - response = requests.get(self.original_url, params={"requested_file": requested_file, "current_user_name": self.current_user_name, "shelf_id": self.shelf_id, "media_id": self.media_id}) + response = requests.get(self.original_url, params={"requested_file": requested_file, "current_user_name": self.current_user_name, "shelf_id": self.shelf_id}) if response.status_code == 200: log.info("Successfully sent the requested file to %s", self.original_url) file_downloaded = response.json()["file_downloaded"] From 7dc2bd8cdcefe13278f09ac29266f758627b64ca Mon Sep 17 00:00:00 2001 From: Blondel MONDESIR Date: Fri, 1 Nov 2024 23:25:52 -0400 Subject: [PATCH 7/7] Close session after mapping --- cps/editbooks.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cps/editbooks.py b/cps/editbooks.py index 1155ebd780..3b99e2fde2 100644 --- a/cps/editbooks.py +++ b/cps/editbooks.py @@ -306,6 +306,9 @@ def move_mediafile(requested_file, current_user_name=None, shelf_id=None, media_ ), category="error", ) + finally: + MappingService().session.close() + MappingService().db.remove_session() new_book_path = os.path.join(config.config_calibre_dir, book_path) resp = {"file_downloaded": link, "shelf_id": shelf_id, "new_book_path": new_book_path}