Skip to content
This repository has been archived by the owner on Aug 29, 2022. It is now read-only.

German Special characters #103

Open
Torsteng5 opened this issue Feb 28, 2018 · 3 comments
Open

German Special characters #103

Torsteng5 opened this issue Feb 28, 2018 · 3 comments

Comments

@Torsteng5
Copy link

Hello,

I can't download files with german umlaut:

[2018-02-28 08:44:14,778] ERROR in errors:
Title: Internal Error
Message: An internal error has occured and has been forwarded to our support team.
Exception Type: <type 'exceptions.UnicodeEncodeError'>
Exception Message: 'ascii' codec can't encode character u'\xfc' in position 9: ordinal not in range(128)
HTTP Path: /home/download/Documents/Daten/Office/Auftrag für ein Diensthandy.doc
HTTP Method: GET
Client IP Address:
User Agent: Mozilla/5.0 (Linux; Android 6.0.1; SM-G900F Build/MMB29M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.137 Mobile Safari/537.36
User Platform: android
User Browser: chrome
User Browser Version: 64.0.3282.137
Username:

@Torsteng5
Copy link
Author

Fix:
lib/smb.py: Line 514
resp = make_response(send_file(file_object,add_etags=False,as_attachment=attach,attachment_filename=entryname.encode('utf-8'),mimetype=mtype))

@divad divad self-assigned this Feb 28, 2018
@divad
Copy link
Collaborator

divad commented Feb 28, 2018

Whilst your solution fixes the problem, it might not be the correct fix.

Technically this is a bug in Flask's send_file. When I wrote this part of bargate, the Flask documentation said:

filename_or_fp – the filename of the file to send.

Everything in Flask variable wise is unicode, so I sent it a unicode object.

However, in Flask 0.11 onwards, the documentation was changed (but there was nothing in the release notes, which I read, so I was not able to spot it): The docs now say:

filename_or_fp – the filename of the file to send in latin-1.

What I assume this means is, send a str object, but encoded as latin-1. Not UTF-8: latin-1. I can't send latin-1 because bargate is unicode-native, everything inside bargate is unicode, so the strings could contain characters that aren't in latin-1 - in fact, they most likely will.

The reason Flask now says the filename must be in 'latin-1' is because until recently there was no way - or at least no agreed way - to send unicode filenames to browsers via HTTP headers (strictly, HTTP headers are latin-1 only). So Flask quietly changed the docs to reflect reality - sending anything other than latin-1 won't likely work.

Basically, your fix might work in some browsers, but not all - as such, I can't fix it in this way, you're not allowed to just send UTF-8 filenames.

Earlier I said this is technically a bug in Flask's send_file? Well... it is actually a recognised flaw in Flask, and Flask 0.13 plans to fix it:

https://github.com/pallets/flask/blob/78cb96fde0ffb9d6ce67b8f5c850c7a60cab86b3/flask/helpers.py#L539-L555

However, I have no idea when Flask 0.13 will be released.

I think I need to just take the code from Flask 0.13 and do that myself, in bargate. That way bargate will work with older versions of Flask even when Flask 0.13 is released. I'll think about this, and another option (disabling attachment downloads, but I'd hate to do that). I'll get back to you soon.

@divad divad added the question label Feb 28, 2018
@divad
Copy link
Collaborator

divad commented Mar 2, 2018

Quick update: I've written a function to replace send_file which supports sending the filename as UTF-8. I'll test it out some more and release a fixed bargate 1.5.x soon with this included.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants