Skip to content

Commit

Permalink
FIX no doc file or permissions showed as 404 error instead of no perm…
Browse files Browse the repository at this point in the history
…issions or not found document; Redis cached files cleared after file updates (private on/of etc.); CLEAN removed not needed ?? method (temporarily until confirmed with collaborator)
  • Loading branch information
developmentforpeople committed Oct 14, 2023
1 parent cc44a3e commit bd5360c
Showing 1 changed file with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,10 @@ def dfp_external_storage_upload_file(self,local_file=None,delete_file=True):
else:
frappe.throw(error_msg)

def set_is_private(self):
if not self.dfp_external_storage_s3_key:
super(DFPExternalStorageFile, self).set_is_private()
# TODO: remove after double check with Khoran
# def set_is_private(self):
# if not self.dfp_external_storage_s3_key:
# super(DFPExternalStorageFile, self).set_is_private()

def dfp_external_storage_delete_file(self):
if not self.dfp_external_storage_s3_key or not self.dfp_external_storage_doc:
Expand Down Expand Up @@ -510,6 +511,9 @@ def hook_file_on_update(doc, method):
# Existent local file, but new storage selected: upload to remote
if not doc.dfp_external_storage_s3_key and doc.dfp_external_storage and not previous.dfp_external_storage:
doc.dfp_external_storage_upload_file()
cache_key = f"{DFP_EXTERNAL_STORAGE_PUBLIC_CACHE_PREFIX}{doc.name}"
frappe.cache().delete_key(cache_key)



def hook_file_after_delete(doc, method):
Expand Down Expand Up @@ -539,18 +543,19 @@ def render(self):


def file(name:str, file:str):
if not name or not file:
raise frappe.PageDoesNotExistError()

cache_key = f"{DFP_EXTERNAL_STORAGE_PUBLIC_CACHE_PREFIX}{name}"

response_values = frappe.cache().get_value(cache_key)
if not response_values:
if not name or not file:
raise frappe.PageDoesNotExistError()
doc = frappe.get_doc("File", name)
if not doc or not doc.is_downloadable() or doc.file_name != file:
raise frappe.PageDoesNotExistError()
if not doc.has_permission("read"):
# For security reasons never inform about if there are "permission" issues
# giving information about file existence so not using "raise frappe.PermissionError"
try:
doc = frappe.get_doc("File", name)
if not doc or not doc.is_downloadable() or doc.file_name != file:
raise Exception("File not available")
except Exception:
# If no document, no read permissions, etc. For security reasons do not give any information, so just raise a 404 error
raise frappe.PageDoesNotExistError()

response_values = {}
Expand Down

0 comments on commit bd5360c

Please sign in to comment.