Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Begin to establish 1:1 mapping between media_id (xklb-metadata.db) <-> Calibre-Web's book_id (metadata.db) #255

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
15 changes: 11 additions & 4 deletions cps/editbooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -285,14 +286,16 @@ def move_mediafile(requested_file, current_user_name=None, shelf_id=None):
link = '<a href="{}">{}</a>'.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:
shelf.add_to_shelf_as_guest(shelf_id, book_id)

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))
Expand All @@ -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}
Expand All @@ -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)
Expand Down