auto select format if only one

pull/435/head
Andriy Zasypkin 7 years ago
parent e66068d699
commit 28dac34119
No known key found for this signature in database
GPG Key ID: BF7FE58155F2A3E4

@ -43,20 +43,33 @@
<a href="{{url_for('send_to_kindle', book_id=entry.id)}}" id="sendbtn" class="btn btn-primary" role="button"><span class="glyphicon glyphicon-send"></span> {{_('Send to Kindle')}}</a>
{% endif %}
{% if (g.user.role_download() and g.user.is_anonymous) or g.user.is_authenticated %}
{% with formats = entry.data|select('canread')|list %}
{% if formats|length == 1 %}
{% set format = formats|first %}
<div class="btn-group" role="group">
<a target="_blank" class="btn btn-primary" href="{{ url_for('read_book', book_id=entry.id, book_format=format.format|lower) }}">{{format.format}}
<span class="glyphicon glyphicon-eye-open"></span> {{_('Read in browser')}}
</a>
</div>
{% elif formats|length > 1 %}
<div class="btn-group" role="group">
<button id="read-in-browser" type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="glyphicon glyphicon-eye-open"></span> {{_('Read in browser')}}
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="read-in-browser">
{% for format in entry.data %}
{%if format.format|lower == 'epub' or format.format|lower == 'txt' or format.format|lower == 'pdf' or ( format.format|lower == 'cbr' and rarsupport ) or format.format|lower == 'cbt' or format.format|lower == 'cbz' %}
{% for format in formats %}
{%if format.format|lower|canread %}
<li><a target="_blank" href="{{ url_for('read_book', book_id=entry.id, book_format=format.format|lower) }}">{{format.format}}</a></li>
{% endif %}
{%endfor%}
</ul>
</div>
{% endif %}
{%else%}
{{formats|length}}
{%endif%}
{%endwith%}
{%endif%}
</div>
</div>
<h2>{{entry.title}}</h2>

@ -116,6 +116,7 @@ gdrive_watch_callback_token = 'target=calibreweb-watch_files'
global_task = None
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'epub', 'mobi', 'azw', 'azw3', 'cbr', 'cbz', 'cbt', 'djvu', 'prc', 'doc', 'docx', 'fb2'])
READER_EXTENSIONS = set(['txt', 'pdf', 'epub', 'zip', 'cbz', 'tar', 'cbt'] + (['rar','cbr'] if rar_support else []))
def md5(fname):
hash_md5 = hashlib.md5()
@ -473,6 +474,14 @@ def yesno(value, yes, no):
return yes if value else no
@app.template_filter('canread')
@app.template_test('canread')
def canread(ext):
if type(ext) == db.Data:
ext = ext.format
return ext.lower() in READER_EXTENSIONS
def admin_required(f):
"""
Checks if current_user.role == 1
@ -937,10 +946,8 @@ def get_comic_book(book_id, book_format, page):
rarfile.UNRAR_TOOL = config.config_rarfile_location
try:
rf = rarfile.RarFile(cbr_file)
rarNames = sort(rf.namelist())
b64 = codecs.encode(rf.read(rarNames[page]), 'base64').decode()
extractedfile="data:image/png;base64," + b64
fileData={"name": rarNames[page],"page":page, "last":rarNames.__len__()-1, "content": extractedfile}
names = sort(rf.namelist())
extract = lambda page: rf.read(names[page])
except:
# rarfile not valid
app.logger.error('Unrar binary not found, or unable to decompress file ' + cbr_file)
@ -949,25 +956,27 @@ def get_comic_book(book_id, book_format, page):
app.logger.info('Unrar is not supported please install python rarfile extension')
# no support means return nothing
return "", 204
if book_format in ("cbz", "zip"):
elif book_format in ("cbz", "zip"):
zf = zipfile.ZipFile(cbr_file)
zipNames=sort(zf.namelist())
if sys.version_info.major >= 3:
b64 = codecs.encode(zf.read(zipNames[page]), 'base64').decode()
names=sort(zf.namelist())
extract = lambda page: zf.read(names[page])
elif book_format in ("cbt", "tar"):
tf = tarfile.TarFile(cbr_file)
names=sort(tf.getnames())
extract = lambda page: tf.extractfile(names[page]).read()
else:
b64 = zf.read(zipNames[page]).encode('base64')
extractedfile="data:image/png;base64," + b64
fileData={"name": zipNames[page],"page":page, "last":zipNames.__len__()-1, "content": extractedfile}
app.logger.error('unsupported comic format')
return "", 204
if book_format in ("cbt", "tar"):
tf = tarfile.TarFile(cbr_file)
tarNames=sort(tf.getnames())
if sys.version_info.major >= 3:
b64 = codecs.encode(tf.extractfile(tarNames[page]).read(), 'base64').decode()
else:
b64 = (tf.extractfile(tarNames[page]).read()).encode('base64')
extractedfile="data:image/png;base64," + bs
fileData={"name": tarNames[page],"page":page, "last":tarNames.__len__()-1, "content": extractedfile}
b64 = codecs.encode(extract(page), 'base64').decode()
else:
b64 = extract(page).encode('base64')
ext = names[page].rpartition('.')[-1]
if ext not in ('png', 'gif', 'jpg', 'jpeg'):
ext = 'png'
extractedfile="data:image/" + ext + ";base64," + b64
fileData={"name": names[page], "page":page, "last":len(names)-1, "content": extractedfile}
return make_response(json.dumps(fileData))
return "", 204
@ -3264,4 +3273,3 @@ def start_gevent():
app.logger.info('Unable to listen on \'\', trying on IPv4 only...')
gevent_server = WSGIServer(('0.0.0.0', ub.config.config_port), app)
gevent_server.serve_forever()

Loading…
Cancel
Save