fall back to latin-1 if txt files is not utf-8

This commit is contained in:
j 2015-03-07 17:29:59 +05:30
parent 8b23ea9658
commit fe71615a3f
2 changed files with 8 additions and 2 deletions

View file

@ -83,11 +83,17 @@ class FileHandler(OMLHandler):
if not item or not path: if not item or not path:
self.set_status(404) self.set_status(404)
return return
mimetype={ mimetype = {
'epub': 'application/epub+zip', 'epub': 'application/epub+zip',
'pdf': 'application/pdf', 'pdf': 'application/pdf',
'txt': 'text/plain', 'txt': 'text/plain',
}.get(path.split('.')[-1], None) }.get(path.split('.')[-1], None)
if mimetype == 'text/plain':
try:
open(path, 'rb').read().decode('utf-8')
mimetype = 'text/plain; charset=utf-8'
except:
mimetype = 'text/plain; charset=latin-1'
if self._attachment: if self._attachment:
disposition = os.path.basename(path) disposition = os.path.basename(path)
else: else:

View file

@ -75,7 +75,7 @@ def download():
base_url = settings.server.get('release_url').rsplit('/', 1)[0] base_url = settings.server.get('release_url').rsplit('/', 1)[0]
url = '/'.join([base_url, release['modules'][module]['name']]) url = '/'.join([base_url, release['modules'][module]['name']])
if not os.path.exists(module_tar): if not os.path.exists(module_tar):
print('download', os.path.basename(module_tar)) print('downloading', os.path.basename(module_tar))
get(url, module_tar) get(url, module_tar)
if ox.sha1sum(module_tar) != release['modules'][module]['sha1']: if ox.sha1sum(module_tar) != release['modules'][module]['sha1']:
os.unlink(module_tar) os.unlink(module_tar)