From fe71615a3fa82afb2ac4a756164f4fc74c8445e2 Mon Sep 17 00:00:00 2001 From: j Date: Sat, 7 Mar 2015 17:29:59 +0530 Subject: [PATCH] fall back to latin-1 if txt files is not utf-8 --- oml/item/handlers.py | 8 +++++++- oml/update.py | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/oml/item/handlers.py b/oml/item/handlers.py index 16e7331..41a4e63 100644 --- a/oml/item/handlers.py +++ b/oml/item/handlers.py @@ -83,11 +83,17 @@ class FileHandler(OMLHandler): if not item or not path: self.set_status(404) return - mimetype={ + mimetype = { 'epub': 'application/epub+zip', 'pdf': 'application/pdf', 'txt': 'text/plain', }.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: disposition = os.path.basename(path) else: diff --git a/oml/update.py b/oml/update.py index 3bf6d7e..143ca5b 100644 --- a/oml/update.py +++ b/oml/update.py @@ -75,7 +75,7 @@ def download(): base_url = settings.server.get('release_url').rsplit('/', 1)[0] url = '/'.join([base_url, release['modules'][module]['name']]) 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) if ox.sha1sum(module_tar) != release['modules'][module]['sha1']: os.unlink(module_tar)