Improvement mimetypes for downloaded files

pull/92/head
OzzieIsaacs 8 years ago
parent 99430fc98e
commit 4b8fdb04e6

@ -56,22 +56,7 @@
{% endif %} {% endif %}
{% for format in entry.data %} {% for format in entry.data %}
<link rel="http://opds-spec.org/acquisition" href="{{ url_for('get_opds_download_link', book_id=entry.id, format=format.format|lower)}}" <link rel="http://opds-spec.org/acquisition" href="{{ url_for('get_opds_download_link', book_id=entry.id, format=format.format|lower)}}"
length="{{format.uncompressed_size}}" mtime="{{entry.timestamp}}" length="{{format.uncompressed_size}}" mtime="{{entry.timestamp}}" type="{{format.format|lower|mimetype}}"/>
{% if format.format|lower == "epub" %}
type="application/epub+zip"/>
{% elif format.format|lower == "cbr" %}
type="application/x-cbr"/>
{% elif format.format|lower == "mobi" %}
type="application/x-mobipocket-ebook"/>
{% elif format.format|lower == "pdf" %}
type="application/pdf"/>
{% elif format.format|lower == "rtf" %}
type="text/rtf"/>
{% elif format.format|lower == "txt" %}
type="text/plain"/>
{% elif format.format|lower == "doc" %}
type="application/msword"/>
{% endif %}
{% endfor %} {% endfor %}
</entry> </entry>
{% endfor %} {% endfor %}

@ -47,7 +47,16 @@ except ImportError, e:
from shutil import copyfile from shutil import copyfile
from cgi import escape from cgi import escape
mimetypes.init()
mimetypes.add_type('application/xhtml+xml', '.xhtml') mimetypes.add_type('application/xhtml+xml', '.xhtml')
mimetypes.add_type('application/epub+zip', '.epub')
mimetypes.add_type('application/x-mobipocket-ebook', '.mobi')
mimetypes.add_type('application/x-mobipocket-ebook', '.prc')
mimetypes.add_type('application/vnd.amazon.ebook', '.azw')
mimetypes.add_type('application/x-cbr', '.cbr')
mimetypes.add_type('application/x-cbz', '.cbz')
mimetypes.add_type('application/x-cbt', '.cbt')
mimetypes.add_type('image/vnd.djvu', '.djvu')
class ReverseProxied(object): class ReverseProxied(object):
@ -262,6 +271,14 @@ def shortentitle_filter(s):
s = textwrap.wrap(s, 60, break_long_words=False)[0] + ' [...]' s = textwrap.wrap(s, 60, break_long_words=False)[0] + ' [...]'
return s return s
@app.template_filter('mimetype')
def mimetype_filter(val):
try:
s = mimetypes.types_map['.'+val]
except:
s= 'application/octet-stream'
return s
def admin_required(f): def admin_required(f):
""" """
@ -1113,23 +1130,29 @@ def get_download_link(book_id, format):
format = format.split(".")[0] format = format.split(".")[0]
book = db.session.query(db.Books).filter(db.Books.id == book_id).first() book = db.session.query(db.Books).filter(db.Books.id == book_id).first()
data = db.session.query(db.Data).filter(db.Data.book == book.id).filter(db.Data.format == format.upper()).first() data = db.session.query(db.Data).filter(db.Data.book == book.id).filter(db.Data.format == format.upper()).first()
if current_user.is_authenticated: # collect downloaded books only for registered user and not for anonymous user if data:
helper.update_download(book_id, int(current_user.id)) if current_user.is_authenticated: # collect downloaded books only for registered user and not for anonymous user
author = helper.get_normalized_author(book.author_sort) helper.update_download(book_id, int(current_user.id))
file_name = book.title author = helper.get_normalized_author(book.author_sort)
if len(author) > 0: file_name = book.title
file_name = author + '-' + file_name if len(author) > 0:
file_name = helper.get_valid_filename(file_name) file_name = author + '-' + file_name
response = make_response(send_from_directory(os.path.join(config.DB_ROOT, book.path), data.name + "." + format)) file_name = helper.get_valid_filename(file_name)
response.headers["Content-Disposition"] = \ response = make_response(send_from_directory(os.path.join(config.DB_ROOT, book.path), data.name + "." + format))
"attachment; " \ try:
"filename={utf_filename}.{suffix};" \ response.headers["Content-Type"]=mimetypes.types_map['.'+format]
"filename*=UTF-8''{utf_filename}.{suffix}".format( except:
utf_filename=file_name.encode('utf-8'), pass
suffix=format response.headers["Content-Disposition"] = \
) "attachment; " \
return response "filename={utf_filename}.{suffix};" \
"filename*=UTF-8''{utf_filename}.{suffix}".format(
utf_filename=file_name.encode('utf-8'),
suffix=format
)
return response
else:
abort(404)
@app.route('/register', methods=['GET', 'POST']) @app.route('/register', methods=['GET', 'POST'])
def register(): def register():

Loading…
Cancel
Save