Skip to content

Commit

Permalink
fix: add custom validate for File(v15) (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
MyuddinKhatri authored Aug 20, 2024
1 parent 0835c95 commit 8c15d38
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions cloud_storage/cloud_storage/overrides/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import types
import uuid
from mimetypes import guess_type
from urllib.parse import quote
from urllib.parse import quote, unquote
from urllib.request import urlopen

import frappe
Expand Down Expand Up @@ -45,10 +45,33 @@ def validate(self) -> None:
if self.flags.cloud_storage or self.flags.ignore_file_validate:
return
if not self.is_remote_file:
super().validate()
self.custom_validate()
else:
self.validate_file_url()

def custom_validate(self):
if self.is_folder:
return

# Ensure correct formatting and type
self.file_url = unquote(self.file_url) if self.file_url else ""

self.validate_attachment_references()

# when dict is passed to get_doc for creation of new_doc, is_new returns None
# this case is handled inside handle_is_private_changed
if not self.is_new() and self.has_value_changed("is_private"):
self.handle_is_private_changed()

self.validate_file_path()
self.validate_file_url()

config = frappe.conf.cloud_storage_settings
if not config or config.get("use_local"):
self.validate_file_on_disk()

self.file_size = frappe.form_dict.file_size or self.file_size

def after_insert(self) -> File:
if self.attached_to_doctype and self.attached_to_name and not self.file_association: # type: ignore
if not self.content_hash and "/api/method/retrieve" in self.file_url: # type: ignore
Expand Down Expand Up @@ -406,10 +429,14 @@ def upload_file(file: File) -> File:
def get_file_path(file: File, folder: str | None = None) -> str:
parent_doctype = file.attached_to_doctype or "No Doctype"

attached_to_name = ""
if file.attached_to_name:
attached_to_name = file.attached_to_name.replace("#", "%23")

fragments = [
folder,
parent_doctype,
file.attached_to_name.replace("#", "%23") if file.attached_to_name else "No Doctype",
attached_to_name,
file.file_name.replace("#", "%23"),
]

Expand Down

1 comment on commit 8c15d38

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
__init__.py11554%10–11, 17–19
customize.py25250%1–2, 4, 7–9, 12–19, 24–32, 44–45
hooks.py150100% 
cloud_storage
   __init__.py00100% 
cloud_storage/doctype
   __init__.py00100% 
cloud_storage/doctype/file_association
   __init__.py00100% 
   file_association.py30100% 
cloud_storage/doctype/file_version
   __init__.py00100% 
   file_version.py330%5, 8–9
cloud_storage/overrides
   file.py35416254%48, 53–54, 57, 59, 63–64, 66–67, 69–71, 73, 78, 106, 111, 118, 127, 133, 165, 168, 178–185, 187–195, 200, 205–206, 210–211, 213–218, 220–223, 225–226, 228, 230, 239, 242–243, 245, 248, 251, 256–257, 260, 263, 274–278, 283, 292, 294–295, 297, 302–306, 309–311, 345, 351, 357, 363, 369, 375, 385, 387–389, 393, 401–403, 405, 419–420, 422, 449–453, 461–462, 465–466, 487–488, 495–496, 511–512, 515, 518–527, 534–535, 537–539, 542–543, 548–550, 552, 557–560, 565, 567, 579–580, 582, 587–591, 593, 598–604
config
   __init__.py00100% 
   desktop.py330%1, 4–5
   docs.py660%1, 3–5, 9–10
tests
   conftest.py26196%36
   test_file.py900100% 
TOTAL53620561% 

Please sign in to comment.