]> git.parisson.com Git - teleforma.git/commitdiff
Add guess_mimetypes method and refactor every call to old mimetypes.guess
authortest test <yoanl@pilotsystems.net>
Thu, 27 May 2021 15:23:16 +0000 (17:23 +0200)
committerYoan Le Clanche <yoanl@pilotsystems.net>
Thu, 27 May 2021 15:23:16 +0000 (17:23 +0200)
teleforma/exam/models.py
teleforma/models/core.py
teleforma/utils.py [new file with mode: 0644]
teleforma/views/core.py
teleforma/views/pages.py

index bb19e87acac21b6131b7c7c57a156d24dff3f196..5d9d5a2b9b023606cc17d1ef046131a54fd58e95 100755 (executable)
@@ -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:
index 29aa881ef141410ebe15e1bb841ea0cd56b3e696..a2730c8d2597597ac66e70645a1fe23883982eba 100755 (executable)
@@ -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 (file)
index 0000000..a435d75
--- /dev/null
@@ -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
index a75168ad5d3054a876f3e4107701452e11c03959..9290b476f6ed80614827724587f219db26941b78 100644 (file)
@@ -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')
 
index 863331877cdd278ae73a7e8e81f1dccfe507f1d0..dcf75a0fa7bb414ce0daa5707c410e55e142a3b8 100644 (file)
@@ -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):