Added option to upload a new format to a given book

pull/272/head
kyos 7 years ago
parent fe52de6b4b
commit aa4a5dc3ef

@ -62,3 +62,6 @@ span.glyphicon.glyphicon-tags {padding-right: 5px;color: #999;vertical-align: te
#shelf-action-errors {
margin-left: 5px;
}
.upload-format-input-text {display: initial;}
#btn-upload-format {display: none;}

@ -200,3 +200,11 @@ $("#search").on("change input.typeahead:selected", 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').html(filename);
});

@ -1,7 +1,7 @@
{% extends "layout.html" %}
{% block body %}
{% if book %}
<form role="form" action="{{ url_for('edit_book', book_id=book.id) }}" method="post">
<form role="form" action="{{ url_for('edit_book', book_id=book.id) }}" method="post" enctype="multipart/form-data">
<div class="col-sm-3 col-lg-3 col-xs-12">
<div class="cover">
@ -115,7 +115,15 @@
</div>
{% endfor %}
{% endif %}
{% if g.user.role_upload() or g.user.role_admin()%}
{% if g.allow_upload %}
<div role="group" aria-label="Upload new book format">
<label class="btn btn-default 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 %}
{% endif %}
<div class="checkbox">
<label>

@ -480,6 +480,10 @@ msgstr "Descarga"
msgid "Upload"
msgstr "Subir archivo"
#: cps/templates/detail.html:14
msgid "Upload format"
msgstr "Subir formato"
#: cps/templates/admin.html:15
msgid "Edit"
msgstr "Editar"

@ -2719,6 +2719,35 @@ def edit_book(book_id):
# Update book
edited_books_id = set()
# Check and handle Uploaded file
if 'btn-upload-format' in request.files and '.' in request.files['btn-upload-format'].filename:
requested_file = request.files['btn-upload-format']
file_ext = requested_file.filename.rsplit('.', 1)[-1].lower()
if file_ext not in ALLOWED_EXTENSIONS:
flash(_('File extension "%s" is not allowed to be uploaded to this server' % file_ext), category="error")
return redirect(url_for('index'))
file_name = book.path.rsplit('/', 1)[-1]
filepath = config.config_calibre_dir + os.sep + book.path
filepath = os.path.normpath(filepath)
saved_filename = filepath + os.sep + file_name + '.' + file_ext
file_size = os.path.getsize(requested_file.stream.name)
try:
requested_file.save(saved_filename)
except OSError:
flash(_(u"Failed to store file %s." % saved_filename), category="error")
return redirect(url_for('index'))
is_format = db.session.query(db.Data).filter(db.Data.book == book_id).filter(db.Data.format == file_ext.upper()).first()
if is_format:
# Format entry already exists, no need to update the database
pass
else:
db_format = db.Data(book_id, file_ext.upper(), file_size, file_name)
db.session.add(db_format)
to_save = request.form.to_dict()
if book.title != to_save["book_title"]:
book.title = to_save["book_title"]

Loading…
Cancel
Save