mirror of
				https://github.com/janeczku/calibre-web
				synced 2025-10-30 23:03:02 +00:00 
			
		
		
		
	Handle kindle browser download filename can only contain ASCII characters (#3266)
This commit is contained in:
		| @@ -237,7 +237,7 @@ def send_mail(book_id, book_format, convert, ereader_mail, calibrepath, user_id) | |||||||
|     return _("The requested file could not be read. Maybe wrong permissions?") |     return _("The requested file could not be read. Maybe wrong permissions?") | ||||||
|  |  | ||||||
|  |  | ||||||
| def get_valid_filename(value, replace_whitespace=True, chars=128): | def get_valid_filename(value, replace_whitespace=True, chars=128, force_unidecode=False): | ||||||
|     """ |     """ | ||||||
|     Returns the given string converted to a string that can be used for a clean |     Returns the given string converted to a string that can be used for a clean | ||||||
|     filename. Limits num characters to 128 max. |     filename. Limits num characters to 128 max. | ||||||
| @@ -245,7 +245,7 @@ def get_valid_filename(value, replace_whitespace=True, chars=128): | |||||||
|     if value[-1:] == '.': |     if value[-1:] == '.': | ||||||
|         value = value[:-1]+'_' |         value = value[:-1]+'_' | ||||||
|     value = value.replace("/", "_").replace(":", "_").strip('\0') |     value = value.replace("/", "_").replace(":", "_").strip('\0') | ||||||
|     if config.config_unicode_filename: |     if config.config_unicode_filename or force_unidecode: | ||||||
|         value = (unidecode.unidecode(value)) |         value = (unidecode.unidecode(value)) | ||||||
|     if replace_whitespace: |     if replace_whitespace: | ||||||
|         #  *+:\"/<>? are replaced by _ |         #  *+:\"/<>? are replaced by _ | ||||||
| @@ -1105,11 +1105,14 @@ def get_download_link(book_id, book_format, client): | |||||||
|             file_name = book.title |             file_name = book.title | ||||||
|             if len(book.authors) > 0: |             if len(book.authors) > 0: | ||||||
|                 file_name = file_name + ' - ' + book.authors[0].name |                 file_name = file_name + ' - ' + book.authors[0].name | ||||||
|             file_name = get_valid_filename(file_name, replace_whitespace=False) |             if client != "kindle": | ||||||
|  |                 file_name = get_valid_filename(file_name, replace_whitespace=False, force_unidecode=True) | ||||||
|  |             else: | ||||||
|  |                 file_name = quote(get_valid_filename(file_name, replace_whitespace=False)) | ||||||
|             headers = Headers() |             headers = Headers() | ||||||
|             headers["Content-Type"] = mimetypes.types_map.get('.' + book_format, "application/octet-stream") |             headers["Content-Type"] = mimetypes.types_map.get('.' + book_format, "application/octet-stream") | ||||||
|             headers["Content-Disposition"] = "attachment; filename=%s.%s; filename*=UTF-8''%s.%s" % ( |             headers["Content-Disposition"] = ('attachment; filename="{}.{}"; filename*=UTF-8\'\'{}.{}').format( | ||||||
|                 quote(file_name), book_format, quote(file_name), book_format) |                 file_name, book_format, file_name, book_format) | ||||||
|             return do_download_file(book, book_format, client, data1, headers) |             return do_download_file(book, book_format, client, data1, headers) | ||||||
|     else: |     else: | ||||||
|         log.error("Book id {} not found for downloading".format(book_id)) |         log.error("Book id {} not found for downloading".format(book_id)) | ||||||
|   | |||||||
| @@ -1242,7 +1242,12 @@ def serve_book(book_id, book_format, anyname): | |||||||
| @login_required_if_no_ano | @login_required_if_no_ano | ||||||
| @download_required | @download_required | ||||||
| def download_link(book_id, book_format, anyname): | def download_link(book_id, book_format, anyname): | ||||||
|     client = "kobo" if "Kobo" in request.headers.get('User-Agent') else "" |     if "kindle" in request.headers.get('User-Agent').lower(): | ||||||
|  |         client = "kindle" | ||||||
|  |     elif "Kobo" in request.headers.get('User-Agent').lower(): | ||||||
|  |         client = "kobo" | ||||||
|  |     else: | ||||||
|  |         client = "" | ||||||
|     return get_download_link(book_id, book_format, client) |     return get_download_link(book_id, book_format, client) | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Ozzie Isaacs
					Ozzie Isaacs