|
|
@@ -16,6 +16,12 @@ from frappe.utils.nestedset import NestedSet |
|
|
|
from frappe.utils import strip |
|
|
|
import json |
|
|
|
import urllib |
|
|
|
from PIL import Image, ImageOps |
|
|
|
import os |
|
|
|
import requests |
|
|
|
import requests.exceptions |
|
|
|
import StringIO |
|
|
|
import mimetypes, imghdr |
|
|
|
|
|
|
|
class FolderNotEmpty(frappe.ValidationError): pass |
|
|
|
|
|
|
@@ -119,9 +125,6 @@ class File(NestedSet): |
|
|
|
self.delete_file() |
|
|
|
|
|
|
|
def make_thumbnail(self): |
|
|
|
from PIL import Image, ImageOps |
|
|
|
import os |
|
|
|
|
|
|
|
if self.file_url: |
|
|
|
if self.file_url.startswith("/files"): |
|
|
|
try: |
|
|
@@ -132,12 +135,24 @@ class File(NestedSet): |
|
|
|
|
|
|
|
else: |
|
|
|
# downlaod |
|
|
|
import requests, StringIO |
|
|
|
file_url = frappe.utils.get_url(self.file_url) |
|
|
|
r = requests.get(file_url, stream=True) |
|
|
|
r.raise_for_status() |
|
|
|
try: |
|
|
|
r.raise_for_status() |
|
|
|
except requests.exceptions.HTTPError, e: |
|
|
|
if "404" in e.args[0]: |
|
|
|
frappe.msgprint(_("File '{0}' not found").format(self.file_url)) |
|
|
|
|
|
|
|
raise |
|
|
|
|
|
|
|
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( |
|
|
@@ -192,6 +207,7 @@ class File(NestedSet): |
|
|
|
delete_file_data_content(self, only_thumbnail=True) |
|
|
|
|
|
|
|
def on_rollback(self): |
|
|
|
self.flags.on_rollback = True |
|
|
|
self.on_trash() |
|
|
|
|
|
|
|
def on_doctype_update(): |
|
|
|