Переглянути джерело

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

version-14
Anand Doshi 10 роки тому
джерело
коміт
af8e98a430
1 змінених файлів з 11 додано та 3 видалено
  1. +11
    -3
      frappe/utils/file_manager.py

+ 11
- 3
frappe/utils/file_manager.py Переглянути файл

@@ -6,7 +6,7 @@ import frappe
import os, base64, re import os, base64, re
import hashlib import hashlib
import mimetypes 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 _
from frappe import conf from frappe import conf
from copy import copy from copy import copy
@@ -89,8 +89,16 @@ def extract_images_from_html(doc, fieldname):


def _save_file(match): def _save_file(match):
data = match.group(1) data = match.group(1)
data = data.split("data:")[1]
headers, content = data.split(",") 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 # TODO fix this
file_url = save_file(filename, content, doc.doctype, doc.name, decode=True).get("file_url") file_url = save_file(filename, content, doc.doctype, doc.name, decode=True).get("file_url")
if not frappe.flags.has_dataurl: 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) return '<img src="{file_url}"'.format(file_url=file_url)


if content: 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: if frappe.flags.has_dataurl:
doc.set(fieldname, content) doc.set(fieldname, content)




Завантаження…
Відмінити
Зберегти