Browse Source

[fix] set random filename if not found, when extracting image from html

version-14
Anand Doshi 10 years ago
parent
commit
af8e98a430
1 changed files with 11 additions and 3 deletions
  1. +11
    -3
      frappe/utils/file_manager.py

+ 11
- 3
frappe/utils/file_manager.py View File

@@ -6,7 +6,7 @@ import frappe
import os, base64, re
import hashlib
import mimetypes
from frappe.utils import get_site_path, get_hook_method, get_files_path
from frappe.utils import get_site_path, get_hook_method, get_files_path, random_string
from frappe import _
from frappe import conf
from copy import copy
@@ -89,8 +89,16 @@ def extract_images_from_html(doc, fieldname):

def _save_file(match):
data = match.group(1)
data = data.split("data:")[1]
headers, content = data.split(",")
filename = headers.split("filename=")[-1]

if "filename=" in headers:
filename = headers.split("filename=")[-1]
else:
mtype = headers.split(";")[0]
extn = mimetypes.guess_extension(mtype)
filename = random_string(7) + extn

# TODO fix this
file_url = save_file(filename, content, doc.doctype, doc.name, decode=True).get("file_url")
if not frappe.flags.has_dataurl:
@@ -99,7 +107,7 @@ def extract_images_from_html(doc, fieldname):
return '<img src="{file_url}"'.format(file_url=file_url)

if content:
content = re.sub('<img\s*src=\s*["\'](data:[^"\']*)["\']', _save_file, content)
content = re.sub('<img\s*src=\s*["\'](.*?)["\']', _save_file, content)
if frappe.flags.has_dataurl:
doc.set(fieldname, content)



Loading…
Cancel
Save