From de8d338b5a19073859706d725ea880ed4f779e8a Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Wed, 11 Sep 2019 14:04:09 +0530 Subject: [PATCH 1/2] fix: Check if duplicate file exists on disk --- frappe/core/doctype/file/file.py | 37 +++++++++++++++++++------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/frappe/core/doctype/file/file.py b/frappe/core/doctype/file/file.py index d334dc5346..390b091298 100755 --- a/frappe/core/doctype/file/file.py +++ b/frappe/core/doctype/file/file.py @@ -22,7 +22,7 @@ import requests import requests.exceptions import imghdr -from frappe.utils import get_hook_method, get_files_path, random_string, encode, cstr, call_hook_method, cint +from frappe.utils import get_hook_method, get_files_path, random_string, encode, cstr, call_hook_method, cint, get_site_path from frappe import _ from frappe import conf from frappe.utils.nestedset import NestedSet @@ -201,14 +201,16 @@ class File(NestedSet): duplicate_file = frappe.db.get_value('File', filters, ['name', 'file_url'], as_dict=1) if duplicate_file: - # if it is attached to a document then throw DuplicateEntryError - if self.attached_to_doctype and self.attached_to_name: - self.duplicate_entry = duplicate_file.name - frappe.throw(_("Same file has already been attached to the record"), - frappe.DuplicateEntryError) - # else just use the url, to avoid uploading a duplicate - else: - self.file_url = duplicate_file.file_url + duplicate_file_doc = frappe.get_cached_doc('File', duplicate_file.name) + if duplicate_file_doc.exists_on_disk(): + # if it is attached to a document then throw DuplicateEntryError + if self.attached_to_doctype and self.attached_to_name: + self.duplicate_entry = duplicate_file.name + frappe.throw(_("Same file has already been attached to the record"), + frappe.DuplicateEntryError) + # else just use the url, to avoid uploading a duplicate + else: + self.file_url = duplicate_file.file_url def validate_file_name(self): if not self.file_name and self.file_url: @@ -332,6 +334,9 @@ class File(NestedSet): data = frappe.db.get_value("File", self.file_data_name, ["file_name", "file_url"], as_dict=True) return data.file_url or data.file_name + def exists_on_disk(self): + exists = os.path.exists(self.get_full_path()) + return exists def upload(self): # get record details @@ -495,19 +500,21 @@ class File(NestedSet): self.content_hash = get_content_hash(self.content) self.content_type = mimetypes.guess_type(self.file_name)[0] - _file = False + duplicate_file = None # check if a file exists with the same content hash and is also in the same folder (public or private) if not ignore_existing_file_check: - _file = frappe.get_value("File", { + duplicate_file = frappe.get_value("File", { "content_hash": self.content_hash, "is_private": self.is_private }, - ["file_url"]) + ["file_url", "name"], as_dict=True) - if _file: - self.file_url = _file - file_exists = True + if duplicate_file: + file_doc = frappe.get_cached_doc('File', duplicate_file.name) + if file_doc.exists_on_disk(): + self.file_url = duplicate_file.file_url + file_exists = True if os.path.exists(encode(get_files_path(self.file_name, is_private=self.is_private))): self.file_name = get_file_name(self.file_name, self.content_hash[-6:]) From ba96f26f2481faa3480cf1c28a6e03150746a1c6 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Wed, 11 Sep 2019 15:46:34 +0530 Subject: [PATCH 2/2] fix: Remove unused import --- frappe/core/doctype/file/file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frappe/core/doctype/file/file.py b/frappe/core/doctype/file/file.py index 390b091298..3c89519faf 100755 --- a/frappe/core/doctype/file/file.py +++ b/frappe/core/doctype/file/file.py @@ -22,7 +22,7 @@ import requests import requests.exceptions import imghdr -from frappe.utils import get_hook_method, get_files_path, random_string, encode, cstr, call_hook_method, cint, get_site_path +from frappe.utils import get_hook_method, get_files_path, random_string, encode, cstr, call_hook_method, cint from frappe import _ from frappe import conf from frappe.utils.nestedset import NestedSet