From 60f3008d62dad3f50db599afeb71093eae97d60b Mon Sep 17 00:00:00 2001 From: test test Date: Thu, 27 May 2021 17:23:16 +0200 Subject: [PATCH] Add guess_mimetypes method and refactor every call to old mimetypes.guess --- teleforma/exam/models.py | 12 ++++-------- teleforma/models/core.py | 9 +++++---- teleforma/utils.py | 8 ++++++++ teleforma/views/core.py | 15 ++------------- teleforma/views/pages.py | 3 ++- 5 files changed, 21 insertions(+), 26 deletions(-) create mode 100644 teleforma/utils.py diff --git a/teleforma/exam/models.py b/teleforma/exam/models.py index bb19e87a..5d9d5a2b 100755 --- a/teleforma/exam/models.py +++ b/teleforma/exam/models.py @@ -39,6 +39,7 @@ from __future__ import division import datetime import mimetypes import os +from teleforma.utils import guess_mimetypes import urllib import uuid @@ -123,11 +124,6 @@ def sha1sum_file(filename): sha1.update(chunk) return sha1.hexdigest() - -def mimetype_file(path): - return mimetypes.guess_type(path)[0] - - def check_unique_mimetype(l): i = 0 for d in l: @@ -448,7 +444,7 @@ class Script(BaseResource): self.auto_reject('file not found') return - mime_type = mimetype_file(self.file.path) + mime_type = guess_mimetypes(self.file.path) if mime_type: if not 'pdf' in mime_type: self.auto_reject('wrong format') @@ -503,9 +499,9 @@ def set_file_properties(sender, instance, **kwargs): if instance.file: trig_save = False if not instance.mime_type: - mime_type = mimetype_file(instance.file.path) + mime_type = guess_mimetypes(instance.file.path) if mime_type: - instance.mime_type = mimetype_file(instance.file.path) + instance.mime_type = guess_mimetypes(instance.file.path) trig_save = True # HOTFIX else: diff --git a/teleforma/models/core.py b/teleforma/models/core.py index 29aa881e..a2730c8d 100755 --- a/teleforma/models/core.py +++ b/teleforma/models/core.py @@ -38,6 +38,7 @@ import datetime import mimetypes import os import string +from teleforma.utils import guess_mimetypes import django.db.models as models from django.conf import settings @@ -688,7 +689,7 @@ class Document(MediaBase): return 'image' in self.mime_type or is_url_image def set_mime_type(self): - self.mime_type = mimetypes.guess_type(self.file.path)[0] + self.mime_type = guess_mimetypes(self.file.path) def __str__(self): types = ' - '.join([str(t) for t in self.course_type.all()]) @@ -726,7 +727,7 @@ class DocumentSimple(MediaBase): return 'image' in self.mime_type or is_url_image def set_mime_type(self): - self.mime_type = mimetypes.guess_type(self.file.path)[0] + self.mime_type = guess_mimetypes(self.file.path) def __str__(self): return self.title @@ -759,7 +760,7 @@ class MediaTranscoded(models.Model): if not self.mimetype: if self.file: if os.path.exists(self.file.path): - self.mimetype = mimetypes.guess_type(self.file.path)[0] + self.mimetype = guess_mimetypes(self.file.path) self.save() return self.mimetype else: @@ -806,7 +807,7 @@ class Media(MediaBase): def set_mime_type(self): if self.item.file: - mime_type = mimetypes.guess_type(self.file.path)[0] + mime_type = guess_mimetypes(self.file.path) if mime_type == 'audio/mpeg': self.mime_type = 'audio/mp3' else: diff --git a/teleforma/utils.py b/teleforma/utils.py new file mode 100644 index 00000000..a435d751 --- /dev/null +++ b/teleforma/utils.py @@ -0,0 +1,8 @@ +import mimetypes + +def guess_mimetypes(path): + try: + path = path.decode() + except (UnicodeDecodeError, AttributeError): + pass + return mimetypes.guess_type(path)[0] \ No newline at end of file diff --git a/teleforma/views/core.py b/teleforma/views/core.py index a75168ad..9290b476 100644 --- a/teleforma/views/core.py +++ b/teleforma/views/core.py @@ -38,6 +38,7 @@ import mimetypes import os from html import escape from io import StringIO +from teleforma.utils import guess_mimetypes from django.conf import settings from django.contrib import messages @@ -228,7 +229,7 @@ def render_to_pdf(request, template, context, filename=None, encoding='utf-8', def serve_media(media_path, content_type="", buffering=True, streaming=False): if not content_type: - content_type = mimetypes.guess_type(media_path)[0] + content_type = guess_mimetypes(media_path) if not settings.DEBUG: return nginx_media_accel(media_path, content_type=content_type, @@ -685,13 +686,6 @@ class DocumentView(CourseAccessMixin, DetailView): document = Document.objects.get(pk=pk) if get_access(document, courses): return serve_media(document.file.path.encode('utf8'), streaming=False) - #fsock = open(document.file.path.encode('utf8'), 'r') - #mimetype = mimetypes.guess_type(document.file.path)[0] - #extension = mimetypes.guess_extension(mimetype) - #response = HttpResponse(fsock, mimetype=mimetype) - # response['Content-Disposition'] = "attachment; filename=%s%s" % \ - # (document.title.encode('utf8'), extension) - # return response else: return redirect('teleforma-home') @@ -700,11 +694,6 @@ class DocumentView(CourseAccessMixin, DetailView): document = Document.objects.get(pk=pk) if get_access(document, courses): return serve_media(document.file.path.encode('utf8'), streaming=True) - #fsock = open(document.file.path.encode('utf8'), 'r') - #mimetype = mimetypes.guess_type(document.file.path)[0] - #extension = mimetypes.guess_extension(mimetype) - #response = HttpResponse(fsock, mimetype=mimetype) - # return response else: return redirect('teleforma-home') diff --git a/teleforma/views/pages.py b/teleforma/views/pages.py index 86333187..dcf75a0f 100644 --- a/teleforma/views/pages.py +++ b/teleforma/views/pages.py @@ -1,3 +1,4 @@ +from teleforma.utils import guess_mimetypes from django.conf import settings import re import os @@ -31,7 +32,7 @@ class PageAttachment(object): self.path = path def mimetype(self): - type, encoding = mimetypes.guess_type(self.filename) + type = guess_mimetypes(self.filename) return type def __iter__(self): -- 2.39.5