From 138c7ad012e91ceb938ae4d9c29fd4682ac74cd6 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Fri, 23 Oct 2015 16:37:24 +0530 Subject: [PATCH] [fix] detect image extension from content headers if extension isn't of image type --- frappe/core/doctype/file/file.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/frappe/core/doctype/file/file.py b/frappe/core/doctype/file/file.py index 6f28e4fbd8..8d5dc8b5e9 100644 --- a/frappe/core/doctype/file/file.py +++ b/frappe/core/doctype/file/file.py @@ -21,6 +21,7 @@ import os import requests import requests.exceptions import StringIO +import mimetypes, imghdr class FolderNotEmpty(frappe.ValidationError): pass @@ -146,6 +147,12 @@ class File(NestedSet): image = Image.open(StringIO.StringIO(r.content)) filename, extn = self.file_url.rsplit("/", 1)[1].rsplit(".", 1) + + mimetype = mimetypes.guess_type(filename + "." + extn)[0] + if mimetype is None or not mimetype.startswith("image/"): + # detect file extension by reading image header properties + extn = imghdr.what(filename + "." + extn, h=r.content) + filename = "/files/" + strip(urllib.unquote(filename)) thumbnail = ImageOps.fit(