diff --git a/cps/editbooks.py b/cps/editbooks.py index b3080be1a4..3b99e2fde2 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 .services.xb_utils import MappingService 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) @@ -285,7 +286,7 @@ def move_mediafile(requested_file, current_user_name=None, shelf_id=None): 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: @@ -293,6 +294,8 @@ def move_mediafile(requested_file, current_user_name=None, shelf_id=None): book_path = db_book.path + MappingService().add_book_media_mapping(media_id, book_id) + except (OperationalError, IntegrityError, StaleDataError) as e: calibre_db.session.rollback() log.error_or_exception("Database error: {}".format(e)) @@ -303,6 +306,9 @@ def move_mediafile(requested_file, current_user_name=None, shelf_id=None): ), 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} @@ -312,13 +318,14 @@ def move_mediafile(requested_file, current_user_name=None, shelf_id=None): 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) + resp = move_mediafile(requested_file, current_user_name, shelf_id, media_id) return jsonify(resp) 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)