Skip to contentSkip to conversation

toaster: Use Python's mimetypes module

PATCH Applied Elliot Smith 3 participants 3 messages Sep 24, 2015 – Oct 7, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 toaster: Use Python's mimetypes module

filemagic is used to guess the mimetype of files when a user requests a download. However, this adds a dependency on an external library.

Python does have a mimetypes module, though this guesses the mimetype rather than doing anything clever with the actual file content. But for our purposes, it's more than adequate. (NB Django also uses this module when serving static files.)

Use this instead of relying on any external code, and remove the filemagic dependency.

---
 bitbake/lib/toaster/toastergui/views.py | 15 +++++++++++----
 bitbake/toaster-requirements.txt        |  1 -
 2 files changed, 11 insertions(+), 5 deletions(-)
bitbake/lib/toaster/toastergui/views.py +11 -4
index 8689a12..0a2a959 100755
@@ -46,19 +46,26 @@ from toastergui.templatetags.projecttags import json as jsonfilter
 import json
 from os.path import dirname
 import itertools
+import mimetypes
 
-import magic
 import logging
 
 logger = logging.getLogger("toaster")
 
 class MimeTypeFinder(object):
-    _magic = magic.Magic(flags = magic.MAGIC_MIME_TYPE)
+    # setting this to False enables additional non-standard mimetypes
+    # to be included in the guess
+    _strict = False
 
-    # returns the mimetype for a file path
+    # returns the mimetype for a file path as a string,
+    # or 'application/octet-stream' if the type couldn't be guessed
     @classmethod
     def get_mimetype(self, path):
-        return self._magic.id_filename(path)
+        guess = mimetypes.guess_type(path, self._strict)
+        guessed_type = guess[0]
+        if guessed_type == None:
+            guessed_type = 'application/octet-stream'
+        return guessed_type
 
 # all new sessions should come through the landing page;
 # determine in which mode we are running in, and redirect appropriately
bitbake/toaster-requirements.txt +0 -1
index c4a2221..1d7d21b 100644
@@ -2,5 +2,4 @@ Django==1.6
 South==0.8.4
 argparse==1.2.1
 wsgiref==0.1.2
-filemagic==1.6
 beautifulsoup4>=4.4.0
Signature · 14 lines
Elliot Smith
Software Engineer
Intel OTC

---------------------------------------------------------------------
Intel Corporation (UK) Limited
Registered No. 1134945 (England)
Registered Office: Pipers Way, Swindon SN3 1RJ
VAT No: 860 2173 47

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
Acked-by on v1 1/1
#1 lore raw

Hi Elliot,

Patch looks good to me, thanks.

Quoted 84 lines · Elliot Smith

filemagic is used to guess the mimetype of files when a user requests a download. However, this adds a dependency on an external library.

Python does have a mimetypes module, though this guesses the mimetype rather than doing anything clever with the actual file content. But for our purposes, it's more than adequate. (NB Django also uses this module when serving static files.)

Use this instead of relying on any external code, and remove the filemagic dependency.

---
 bitbake/lib/toaster/toastergui/views.py | 15 +++++++++++----
 bitbake/toaster-requirements.txt        |  1 -
 2 files changed, 11 insertions(+), 5 deletions(-)
bitbake/lib/toaster/toastergui/views.py +11 -4
index 8689a12..0a2a959 100755
@@ -46,19 +46,26 @@ from toastergui.templatetags.projecttags import json as jsonfilter
 import json
 from os.path import dirname
 import itertools
+import mimetypes
 
-import magic
 import logging
 
 logger = logging.getLogger("toaster")
 
 class MimeTypeFinder(object):
-    _magic = magic.Magic(flags = magic.MAGIC_MIME_TYPE)
+    # setting this to False enables additional non-standard mimetypes
+    # to be included in the guess
+    _strict = False
 
-    # returns the mimetype for a file path
+    # returns the mimetype for a file path as a string,
+    # or 'application/octet-stream' if the type couldn't be guessed
     @classmethod
     def get_mimetype(self, path):
-        return self._magic.id_filename(path)
+        guess = mimetypes.guess_type(path, self._strict)
+        guessed_type = guess[0]
+        if guessed_type == None:
+            guessed_type = 'application/octet-stream'
+        return guessed_type
 
 # all new sessions should come through the landing page;
 # determine in which mode we are running in, and redirect appropriately
bitbake/toaster-requirements.txt +0 -1
index c4a2221..1d7d21b 100644
@@ -2,5 +2,4 @@ Django==1.6
 South==0.8.4
 argparse==1.2.1
 wsgiref==0.1.2
-filemagic==1.6
 beautifulsoup4>=4.4.0
Signature · 20 lines
Elliot Smith
Software Engineer
Intel OTC

---------------------------------------------------------------------
Intel Corporation (UK) Limited
Registered No. 1134945 (England)
Registered Office: Pipers Way, Swindon SN3 1RJ
VAT No: 860 2173 47

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.

-- 
_______________________________________________
toaster mailing list
[email protected]
https://lists.yoctoproject.org/listinfo/toaster
Signature · 3 lines
--
Regards,
Ed
Applied
#2 lore raw

pushed to bitbake and to toaster-next.
-b

Quoted 101 lines · Ed Bartosh

Hi Elliot,

Patch looks good to me, thanks.

Quoted 79 lines · Elliot Smith

filemagic is used to guess the mimetype of files when a user requests a download. However, this adds a dependency on an external library.

Python does have a mimetypes module, though this guesses the mimetype rather than doing anything clever with the actual file content. But for our purposes, it's more than adequate. (NB Django also uses this module when serving static files.)

Use this instead of relying on any external code, and remove the filemagic dependency.

---
 bitbake/lib/toaster/toastergui/views.py | 15 +++++++++++----
 bitbake/toaster-requirements.txt        |  1 -
 2 files changed, 11 insertions(+), 5 deletions(-)
bitbake/lib/toaster/toastergui/views.py +11 -4
index 8689a12..0a2a959 100755
@@ -46,19 +46,26 @@ from toastergui.templatetags.projecttags import json as jsonfilter
 import json
 from os.path import dirname
 import itertools
+import mimetypes
 
-import magic
 import logging
 
 logger = logging.getLogger("toaster")
 
 class MimeTypeFinder(object):
-    _magic = magic.Magic(flags = magic.MAGIC_MIME_TYPE)
+    # setting this to False enables additional non-standard mimetypes
+    # to be included in the guess
+    _strict = False
 
-    # returns the mimetype for a file path
+    # returns the mimetype for a file path as a string,
+    # or 'application/octet-stream' if the type couldn't be guessed
     @classmethod
     def get_mimetype(self, path):
-        return self._magic.id_filename(path)
+        guess = mimetypes.guess_type(path, self._strict)
+        guessed_type = guess[0]
+        if guessed_type == None:
+            guessed_type = 'application/octet-stream'
+        return guessed_type
 
 # all new sessions should come through the landing page;
 # determine in which mode we are running in, and redirect appropriately
bitbake/toaster-requirements.txt +0 -2
index c4a2221..1d7d21b 100644
@@ -2,5 +2,4 @@ Django==1.6
 South==0.8.4
 argparse==1.2.1
 wsgiref==0.1.2
-filemagic==1.6
 beautifulsoup4>=4.4.0
--

Elliot Smith
Software Engineer
Intel OTC

---------------------------------------------------------------------
Intel Corporation (UK) Limited
Registered No. 1134945 (England)
Registered Office: Pipers Way, Swindon SN3 1RJ
VAT No: 860 2173 47

This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.

Quoted 5 lines

--
_______________________________________________
toaster mailing list
[email protected]
https://lists.yoctoproject.org/listinfo/toaster

--
--
Regards,
Ed
--
_______________________________________________
toaster mailing list
[email protected]
https://lists.yoctoproject.org/listinfo/toaster