From fa94330dbf752d7a3cd4caa4d4337b9503261910 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Tue, 27 Oct 2015 16:54:24 +0530 Subject: [PATCH] [fix] validate if file exists in the file system --- frappe/core/doctype/file/file.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/frappe/core/doctype/file/file.py b/frappe/core/doctype/file/file.py index 8d5dc8b5e9..592d152e2f 100644 --- a/frappe/core/doctype/file/file.py +++ b/frappe/core/doctype/file/file.py @@ -22,6 +22,7 @@ import requests import requests.exceptions import StringIO import mimetypes, imghdr +from frappe.utils import get_files_path class FolderNotEmpty(frappe.ValidationError): pass @@ -66,6 +67,7 @@ class File(NestedSet): if self.is_new(): self.validate_duplicate_entry() self.validate_folder() + self.validate_file() self.set_folder_size() def set_folder_size(self): @@ -101,6 +103,14 @@ class File(NestedSet): not self.flags.ignore_folder_validate: frappe.throw(_("Folder is mandatory")) + def validate_file(self): + if (self.file_url or "").startswith("/files/"): + if not self.file_name: + self.file_name = self.file_url.split("/files/")[-1] + + if not os.path.exists(get_files_path(self.file_name)): + frappe.throw(_("File {0} does not exist").format(self.file_url), IOError) + def validate_duplicate_entry(self): if not self.flags.ignore_duplicate_entry_error and not self.is_folder: # check duplicate name