From 4ba8ec637d3fd4acc41d8f2fc54910ebb608daa6 Mon Sep 17 00:00:00 2001 From: sokumon Date: Thu, 20 Feb 2025 19:20:20 +0530 Subject: [PATCH 1/5] fix: check if duplicate_file url matches incoming file url --- frappe/core/doctype/file/file.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/frappe/core/doctype/file/file.py b/frappe/core/doctype/file/file.py index 2eac72a6de..6a0b0a0b61 100755 --- a/frappe/core/doctype/file/file.py +++ b/frappe/core/doctype/file/file.py @@ -386,8 +386,6 @@ class File(Document): if self.name: filters.update({"name": ("!=", self.name)}) - else: - filters.update({"file_name": self.file_name}) if self.attached_to_doctype and self.attached_to_name: filters.update( @@ -663,7 +661,8 @@ class File(Document): if duplicate_file: file_doc: File = frappe.get_cached_doc("File", duplicate_file.name) if file_doc.exists_on_disk(): - self.file_url = duplicate_file.file_url + if self.make_file() == duplicate_file.file_url: + self.file_url = duplicate_file.file_url file_exists = True if not file_exists: @@ -690,6 +689,15 @@ class File(Document): return {"file_name": os.path.basename(fpath), "file_url": self.file_url} + def make_file_url(self): + file_url = None + safe_file_name = re.sub(r"[/\\%?#]", "_", self.file_name) + if self.is_private: + file_url = f"/private/files/{safe_file_name}" + else: + file_url = f"/files/{safe_file_name}" + return file_url + def check_max_file_size(self): from frappe.core.api.file import get_max_file_size From 5557ede3d5c7af9851bfa93efd83121c1d7e14c9 Mon Sep 17 00:00:00 2001 From: sokumon Date: Thu, 20 Feb 2025 19:33:43 +0530 Subject: [PATCH 2/5] fix: typo in method name --- 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 6a0b0a0b61..b7ce007650 100755 --- a/frappe/core/doctype/file/file.py +++ b/frappe/core/doctype/file/file.py @@ -661,7 +661,7 @@ class File(Document): if duplicate_file: file_doc: File = frappe.get_cached_doc("File", duplicate_file.name) if file_doc.exists_on_disk(): - if self.make_file() == duplicate_file.file_url: + if self.make_file_url() == duplicate_file.file_url: self.file_url = duplicate_file.file_url file_exists = True From 620c8bfef2658c92e0b537cf65db165d26ada526 Mon Sep 17 00:00:00 2001 From: sokumon Date: Sat, 22 Feb 2025 13:23:26 +0530 Subject: [PATCH 3/5] fix: cleanup copied methods and simpler fix --- frappe/core/doctype/file/file.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/frappe/core/doctype/file/file.py b/frappe/core/doctype/file/file.py index b7ce007650..4eefd86cfe 100755 --- a/frappe/core/doctype/file/file.py +++ b/frappe/core/doctype/file/file.py @@ -661,7 +661,9 @@ class File(Document): if duplicate_file: file_doc: File = frappe.get_cached_doc("File", duplicate_file.name) if file_doc.exists_on_disk(): - if self.make_file_url() == duplicate_file.file_url: + if self.exists_on_disk(): + self.file_url = self.save_file_on_filesystem(make_file_url=True) + else: self.file_url = duplicate_file.file_url file_exists = True @@ -678,26 +680,19 @@ class File(Document): return write_file_method(self) return self.save_file_on_filesystem() - def save_file_on_filesystem(self): + def save_file_on_filesystem(self, make_file_url=None): safe_file_name = re.sub(r"[/\\%?#]", "_", self.file_name) if self.is_private: self.file_url = f"/private/files/{safe_file_name}" else: self.file_url = f"/files/{safe_file_name}" + if make_file_url: + return fpath = self.write_file() return {"file_name": os.path.basename(fpath), "file_url": self.file_url} - def make_file_url(self): - file_url = None - safe_file_name = re.sub(r"[/\\%?#]", "_", self.file_name) - if self.is_private: - file_url = f"/private/files/{safe_file_name}" - else: - file_url = f"/files/{safe_file_name}" - return file_url - def check_max_file_size(self): from frappe.core.api.file import get_max_file_size From 287a5f50a95e5a44c2e14b1f15ef2be3f767f67e Mon Sep 17 00:00:00 2001 From: sokumon Date: Sat, 22 Feb 2025 17:21:18 +0530 Subject: [PATCH 4/5] fix: add duplicate file url if no file url --- frappe/core/doctype/file/file.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frappe/core/doctype/file/file.py b/frappe/core/doctype/file/file.py index 4eefd86cfe..c92e896b43 100755 --- a/frappe/core/doctype/file/file.py +++ b/frappe/core/doctype/file/file.py @@ -662,7 +662,8 @@ class File(Document): file_doc: File = frappe.get_cached_doc("File", duplicate_file.name) if file_doc.exists_on_disk(): if self.exists_on_disk(): - self.file_url = self.save_file_on_filesystem(make_file_url=True) + if not self.file_url: + self.file_url = duplicate_file.file_url else: self.file_url = duplicate_file.file_url file_exists = True From 0c945af0fb4c950045f01e3ae509d114b93d9b70 Mon Sep 17 00:00:00 2001 From: sokumon Date: Sat, 22 Feb 2025 18:16:07 +0530 Subject: [PATCH 5/5] fix: cleanup save_file_on_filesystem method --- frappe/core/doctype/file/file.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/frappe/core/doctype/file/file.py b/frappe/core/doctype/file/file.py index c92e896b43..5011632663 100755 --- a/frappe/core/doctype/file/file.py +++ b/frappe/core/doctype/file/file.py @@ -681,15 +681,13 @@ class File(Document): return write_file_method(self) return self.save_file_on_filesystem() - def save_file_on_filesystem(self, make_file_url=None): + def save_file_on_filesystem(self): safe_file_name = re.sub(r"[/\\%?#]", "_", self.file_name) if self.is_private: self.file_url = f"/private/files/{safe_file_name}" else: self.file_url = f"/files/{safe_file_name}" - if make_file_url: - return fpath = self.write_file() return {"file_name": os.path.basename(fpath), "file_url": self.file_url}