1
0
mirror of https://github.com/janeczku/calibre-web synced 2024-11-14 05:44:53 +00:00

First step format upload with progress

This commit is contained in:
Ozzie Isaacs 2024-08-16 21:11:54 +02:00
parent 8f2f6f5c91
commit d8d9d405e9
5 changed files with 75 additions and 30 deletions

View File

@ -131,18 +131,12 @@ def edit_book(book_id):
calibre_db.session.rollback() calibre_db.session.rollback()
book = calibre_db.get_filtered_book(book_id, allow_show_archived=True) 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 # handle upload covers from local disk
cover_upload_success = upload_cover(request, book) cover_upload_success = upload_cover(request, book)
if cover_upload_success: if cover_upload_success:
book.has_cover = 1 book.has_cover = 1
modify_date = True modify_date = True
meta ={}
# upload new covers or new file formats to google drive # upload new covers or new file formats to google drive
if config.config_use_google_drive: if config.config_use_google_drive:
gdriveutils.updateGdriveCalibreFromLocal() gdriveutils.updateGdriveCalibreFromLocal()
@ -180,7 +174,7 @@ def edit_book(book_id):
modify_date |= edit_book_publisher(to_save['publisher'], book) modify_date |= edit_book_publisher(to_save['publisher'], book)
# handle book languages # handle book languages
try: 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: except ValueError as e:
flash(str(e), category="error") flash(str(e), category="error")
edit_error = True edit_error = True
@ -238,9 +232,31 @@ def edit_book(book_id):
@login_required_if_no_ano @login_required_if_no_ano
@upload_required @upload_required
def upload(): def upload():
if not config.config_uploading: if len(request.files.getlist("btn-upload-format")):
abort(404) # create the function for sorting...
if request.method == 'POST' and 'btn-upload' in request.files: 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"): for requested_file in request.files.getlist("btn-upload"):
try: try:
modify_date = False modify_date = False
@ -309,6 +325,7 @@ def upload():
flash(_("Oops! Database Error: %(error)s.", error=e.orig if hasattr(e, "orig") else e), flash(_("Oops! Database Error: %(error)s.", error=e.orig if hasattr(e, "orig") else e),
category="error") category="error")
return Response(json.dumps({"location": url_for("web.index")}), mimetype='application/json') return Response(json.dumps({"location": url_for("web.index")}), mimetype='application/json')
abort(404)
@editbook.route("/admin/book/convert/<int:book_id>", methods=['POST']) @editbook.route("/admin/book/convert/<int:book_id>", methods=['POST'])
@ -1192,9 +1209,9 @@ def edit_cc_data(book_id, book, to_save, cc):
# returns None if no file is uploaded # returns None if no file is uploaded
# returns False if an error occurs, in all other cases the ebook metadata is returned # 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 # 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(',') allowed_extensions = config.config_upload_formats.split(',')
if requested_file: if requested_file:
if config.config_check_extensions and allowed_extensions != ['']: if config.config_check_extensions and allowed_extensions != ['']:

View File

@ -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(); var filename = $(this).val();
if (filename.substring(3, 11) === "fakepath") { if (filename.substring(3, 11) === "fakepath") {
filename = filename.substring(12); filename = filename.substring(12);
} // Remove c:\fake at beginning from localhost chrome } // Remove c:\fake at beginning from localhost chrome
$("#upload-format").text(filename); $("#upload-format").text(filename);
}); });*/
$("#btn-upload-cover").on("change", function () { $("#btn-upload-cover").on("change", function () {
var filename = $(this).val(); var filename = $(this).val();

View File

@ -130,8 +130,13 @@ $(".container-fluid").bind('drop', function (e) {
} }
}); });
if (dt.files.length) { if (dt.files.length) {
$("#btn-upload")[0].files = dt.files; if($("btn-upload-format").length) {
$("#form-upload").submit(); $("#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").submit();
}); });
$("#form-upload").uploadprogress({ $("#btn-upload-format").change(function() {
redirect_url: getPath() + "/", //"{{ url_for('web.index')}}", $("#form-upload-format").submit();
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')}}"
}); });
$("#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() { $(document).ready(function() {
var inp = $('#query').first() var inp = $('#query').first()
if (inp.length) { if (inp.length) {

View File

@ -47,8 +47,23 @@
</form> </form>
</div> </div>
{% endif %} {% endif %}
{% if current_user.role_upload() and g.allow_upload %}
<div class="text-center more-stuff"><!--h4 aria-label="Upload new book format"></h4-->
<form id="form-upload-format" action="{{ url_for('edit-book.upload') }}" data-title="{{_('Uploading...')}}" data-footer="{{_('Close')}}" data-failed="{{_('Error')}}" data-message="{{_('Upload done, processing, please wait...')}}" method="post" enctype="multipart/form-data">
<div class="text-center">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
<input type="hidden" name="book_id" value="{{ book.id }}">
<div role="group" aria-label="Upload new book format">
<label class="btn btn-primary btn-file" for="btn-upload-format">{{ _('Upload Format') }}</label>
<div class="upload-format-input-text" id="upload-format"></div>
<input id="btn-upload-format" name="btn-upload-format" type="file" accept="{% for format in accept %}.{% if format != ''%}{{format}}{% else %}*{% endif %}{{ ',' if not loop.last }}{% endfor %}" multiple>
</div>
</div>
</form>
</div> </div>
{% endif %}
</div>
<form role="form" action="{{ url_for('edit-book.edit_book', book_id=book.id) }}" method="post" enctype="multipart/form-data" id="book_edit_frm"> <form role="form" action="{{ url_for('edit-book.edit_book', book_id=book.id) }}" method="post" enctype="multipart/form-data" id="book_edit_frm">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"> <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
<div class="col-sm-9 col-xs-12"> <div class="col-sm-9 col-xs-12">
@ -196,13 +211,6 @@
</div> </div>
{% endfor %} {% endfor %}
{% endif %} {% endif %}
{% if current_user.role_upload() and g.allow_upload %}
<div role="group" aria-label="Upload new book format">
<label class="btn btn-primary btn-file" for="btn-upload-format">{{ _('Upload Format') }}</label>
<div class="upload-format-input-text" id="upload-format"></div>
<input id="btn-upload-format" name="btn-upload-format" type="file">
</div>
{% endif %}
<div class="checkbox"> <div class="checkbox">
<label> <label>

View File

@ -80,6 +80,7 @@
<div class="form-group"> <div class="form-group">
<span class="btn btn-default btn-file">{{_('Upload')}}<input id="btn-upload" name="btn-upload" <span class="btn btn-default btn-file">{{_('Upload')}}<input id="btn-upload" name="btn-upload"
type="file" accept="{% for format in accept %}.{% if format != ''%}{{format}}{% else %}*{% endif %}{{ ',' if not loop.last }}{% endfor %}" multiple></span> type="file" accept="{% for format in accept %}.{% if format != ''%}{{format}}{% else %}*{% endif %}{{ ',' if not loop.last }}{% endfor %}" multiple></span>
<input class="hide" id="btn-upload2" name="btn-upload2" type="file" accept="{% for format in accept %}.{% if format != ''%}{{format}}{% else %}*{% endif %}{{ ',' if not loop.last }}{% endfor %}">
</div> </div>
</form> </form>
</li> </li>