From d8d9d405e9e0ca45ca9e499b9ae32e09b2c15adc Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Fri, 16 Aug 2024 21:11:54 +0200 Subject: [PATCH] First step format upload with progress --- cps/editbooks.py | 43 +++++++++++++++++++++++++----------- cps/static/js/edit_books.js | 4 ++-- cps/static/js/main.js | 35 ++++++++++++++++++++++------- cps/templates/book_edit.html | 22 ++++++++++++------ cps/templates/layout.html | 1 + 5 files changed, 75 insertions(+), 30 deletions(-) diff --git a/cps/editbooks.py b/cps/editbooks.py index 122b1c2c..86c28188 100644 --- a/cps/editbooks.py +++ b/cps/editbooks.py @@ -131,18 +131,12 @@ def edit_book(book_id): calibre_db.session.rollback() book = calibre_db.get_filtered_book(book_id, allow_show_archived=True) - # handle upload other formats from local disk - meta = upload_single_file(request, book, book_id) - # only merge metadata if file was uploaded and no error occurred (meta equals not false or none) - upload_format = False - if meta: - upload_format = merge_metadata(to_save, meta) # handle upload covers from local disk cover_upload_success = upload_cover(request, book) if cover_upload_success: book.has_cover = 1 modify_date = True - + meta ={} # upload new covers or new file formats to google drive if config.config_use_google_drive: gdriveutils.updateGdriveCalibreFromLocal() @@ -180,7 +174,7 @@ def edit_book(book_id): modify_date |= edit_book_publisher(to_save['publisher'], book) # handle book languages try: - modify_date |= edit_book_languages(to_save['languages'], book, upload_format) + modify_date |= edit_book_languages(to_save['languages'], book) except ValueError as e: flash(str(e), category="error") edit_error = True @@ -238,9 +232,31 @@ def edit_book(book_id): @login_required_if_no_ano @upload_required def upload(): - if not config.config_uploading: - abort(404) - if request.method == 'POST' and 'btn-upload' in request.files: + if len(request.files.getlist("btn-upload-format")): + # create the function for sorting... + calibre_db.update_title_sort(config) + book_id = request.form.get('book_id', -1) + + book = calibre_db.get_filtered_book(book_id, allow_show_archived=True) + # Book not found + if not book: + flash(_("Oops! Selected book is unavailable. File does not exist or is not accessible"), + category="error") + return redirect(url_for("web.index")) + + # handle upload other formats from local disk + for requested_file in request.files.getlist("btn-upload-format"): + meta = upload_single_file(requested_file, book, book_id) + # save data to database, reread data + calibre_db.session.commit() + + resp = {"location": url_for('edit-book.show_edit_book', book_id=book_id)} + return Response(json.dumps(resp), mimetype='application/json') + + # only merge metadata if file was uploaded and no error occurred (meta equals not false or none) + + + elif len(request.files.getlist("btn-upload")): for requested_file in request.files.getlist("btn-upload"): try: modify_date = False @@ -309,6 +325,7 @@ def upload(): flash(_("Oops! Database Error: %(error)s.", error=e.orig if hasattr(e, "orig") else e), category="error") return Response(json.dumps({"location": url_for("web.index")}), mimetype='application/json') + abort(404) @editbook.route("/admin/book/convert/", methods=['POST']) @@ -1192,9 +1209,9 @@ def edit_cc_data(book_id, book, to_save, cc): # returns None if no file is uploaded # returns False if an error occurs, in all other cases the ebook metadata is returned -def upload_single_file(file_request, book, book_id): +def upload_single_file(requested_file, book, book_id): # Check and handle Uploaded file - requested_file = file_request.files.get('btn-upload-format', None) + # requested_file = file_request.files.get('btn-upload-format', None) allowed_extensions = config.config_upload_formats.split(',') if requested_file: if config.config_check_extensions and allowed_extensions != ['']: diff --git a/cps/static/js/edit_books.js b/cps/static/js/edit_books.js index c1eb319d..e794d9a4 100644 --- a/cps/static/js/edit_books.js +++ b/cps/static/js/edit_books.js @@ -243,13 +243,13 @@ $("#search").on("change input.typeahead:selected", function(event) { }); }); -$("#btn-upload-format").on("change", function () { +/*$("#btn-upload-format").on("change", function () { var filename = $(this).val(); if (filename.substring(3, 11) === "fakepath") { filename = filename.substring(12); } // Remove c:\fake at beginning from localhost chrome $("#upload-format").text(filename); -}); +});*/ $("#btn-upload-cover").on("change", function () { var filename = $(this).val(); diff --git a/cps/static/js/main.js b/cps/static/js/main.js index 2dbea6bc..48947ee5 100644 --- a/cps/static/js/main.js +++ b/cps/static/js/main.js @@ -130,8 +130,13 @@ $(".container-fluid").bind('drop', function (e) { } }); if (dt.files.length) { - $("#btn-upload")[0].files = dt.files; - $("#form-upload").submit(); + if($("btn-upload-format").length) { + $("#btn-upload-format")[0].files = dt.files; + $("#form-upload-format").submit(); + } else { + $("#btn-upload")[0].files = dt.files; + $("#form-upload").submit(); + } } } }); @@ -140,14 +145,28 @@ $("#btn-upload").change(function() { $("#form-upload").submit(); }); -$("#form-upload").uploadprogress({ - redirect_url: getPath() + "/", //"{{ url_for('web.index')}}", - uploadedMsg: $("#form-upload").data("message"), //"{{_('Upload done, processing, please wait...')}}", - modalTitle: $("#form-upload").data("title"), //"{{_('Uploading...')}}", - modalFooter: $("#form-upload").data("footer"), //"{{_('Close')}}", - modalTitleFailed: $("#form-upload").data("failed") //"{{_('Error')}}" +$("#btn-upload-format").change(function() { + $("#form-upload-format").submit(); }); + +$("#form-upload").uploadprogress({ + redirect_url: getPath() + "/", + uploadedMsg: $("#form-upload").data("message"), + modalTitle: $("#form-upload").data("title"), + modalFooter: $("#form-upload").data("footer"), + modalTitleFailed: $("#form-upload").data("failed") +}); + +$("#form-upload-format").uploadprogress({ + redirect_url: getPath() + "/", + uploadedMsg: $("#form-upload-format").data("message"), + modalTitle: $("#form-upload-format").data("title"), + modalFooter: $("#form-upload-format").data("footer"), + modalTitleFailed: $("#form-upload-format").data("failed") +}); + + $(document).ready(function() { var inp = $('#query').first() if (inp.length) { diff --git a/cps/templates/book_edit.html b/cps/templates/book_edit.html index 57fe701c..e3053b2b 100644 --- a/cps/templates/book_edit.html +++ b/cps/templates/book_edit.html @@ -47,8 +47,23 @@ {% endif %} +{% if current_user.role_upload() and g.allow_upload %} +
+
+
+ + +
+ +
+ +
+
+
+{% endif %} +
@@ -196,13 +211,6 @@
{% endfor %} {% endif %} - {% if current_user.role_upload() and g.allow_upload %} -
- -
- -
- {% endif %}