diff --git a/frappe/core/doctype/file_data/file_data.py b/frappe/core/doctype/file_data/file_data.py index 91fb3cb6fd..9e8bd0c156 100644 --- a/frappe/core/doctype/file_data/file_data.py +++ b/frappe/core/doctype/file_data/file_data.py @@ -44,8 +44,8 @@ class FileData(Document): pass # if file not attached to any other record, delete it - if self.file_name and not frappe.db.count("File Data", - {"file_name": self.file_name, "name": ["!=", self.name]}): + if self.doc.file_name and not frappe.db.count("File Data", + {"content_hash": self.content_hash, "name": ["!=", self.name]}): delete_file_data_content(self) def on_rollback(self): diff --git a/frappe/hooks.txt b/frappe/hooks.txt index c3aff8d520..dbc5a5b071 100644 --- a/frappe/hooks.txt +++ b/frappe/hooks.txt @@ -48,3 +48,5 @@ doc_event:Website Route Permission:on_update = frappe.templates.generators.websi doc_event:*:on_update = frappe.core.doctype.notification_count.notification_count.clear_doctype_notifications doc_event:*:on_cancel = frappe.core.doctype.notification_count.notification_count.clear_doctype_notifications doc_event:*:on_trash = frappe.core.doctype.notification_count.notification_count.clear_doctype_notifications + +write_file_keys = file_url diff --git a/frappe/patches/4_0/file_manager_hooks.py b/frappe/patches/4_0/file_manager_hooks.py index 080db3241f..f5e83ec69b 100644 --- a/frappe/patches/4_0/file_manager_hooks.py +++ b/frappe/patches/4_0/file_manager_hooks.py @@ -6,6 +6,7 @@ from __future__ import unicode_literals import frappe import os from frappe.utils import get_files_path +from frappe.utils.filemanager import get_content_hash, get_file def execute(): for name, file_name, file_url in frappe.db.sql( @@ -18,5 +19,7 @@ def execute(): b.doc.file_url = os.path.normpath('/' + old_file_name) else: b.doc.file_url = os.path.normpath('/files/' + old_file_name) + _file_name, content = get_file(file_name) + b.doc.content_hash = get_content_hash(content) b.save() diff --git a/frappe/utils/file_manager.py b/frappe/utils/file_manager.py index 3557d7ea90..c4a8d78647 100644 --- a/frappe/utils/file_manager.py +++ b/frappe/utils/file_manager.py @@ -107,14 +107,17 @@ def save_file(fname, content, dt, dn, decode=False): method = get_hook_method('write_file', fallback=save_file_on_filesystem) - file_data = method(fname, content, content_type=content_type) - file_data = copy(file_data) + file_data = get_file_data_from_hash(content_hash) + if not file_data: + file_data = method(fname, content, content_type=content_type) + file_data = copy(file_data) file_data.update({ "doctype": "File Data", "attached_to_doctype": dt, "attached_to_name": dn, "file_size": file_size, - "file_hash": content_hash + "file_hash": content_hash, + "file_name": fname }) f = frappe.bean(file_data) @@ -125,6 +128,12 @@ def save_file(fname, content, dt, dn, decode=False): return frappe.doc("File Data", f.doc.duplicate_entry) return f.doc + +def get_file_data_from_hash(content_hash): + for name in frappe.db.sql_list("select name from `tabFile Data` where content_hash='{}'".format(content_hash)): + b = frappe.bean('File Data', name) + return {k:b.doc.fields[k] for k in frappe.get_hooks()['write_file_keys']} + return False def save_file_on_filesystem(fname, content, content_type=None): import filecmp