agritheory / cloud_storage

S3 protocol storage for Frappe Applications - compatible with S3, Backblaze and DO Spaces
https://agritheory.com/documentation/cloud_storage
Other
12 stars 7 forks source link

Direct File upload gives 500: Internal Server Error #77

Open MyuddinKhatri opened 1 month ago

MyuddinKhatri commented 1 month ago

Is user directly tries to upload the File without being attached to any doctype, then it gives error Stack Trace -

Traceback (most recent call last):
  File "apps/frappe/frappe/app.py", line 97, in application
    response = frappe.api.handle()
  File "apps/frappe/frappe/api.py", line 55, in handle
    return frappe.handler.handle()
  File "apps/frappe/frappe/handler.py", line 48, in handle
    data = execute_cmd(cmd)
  File "apps/frappe/frappe/handler.py", line 86, in execute_cmd
    return frappe.call(method, **frappe.form_dict)
  File "apps/frappe/frappe/__init__.py", line 1619, in call
    return fn(*args, **newargs)
  File "apps/frappe/frappe/handler.py", line 229, in upload_file
    return frappe.get_doc(
  File "apps/frappe/frappe/model/document.py", line 310, in save
    return self._save(*args, **kwargs)
  File "apps/frappe/frappe/model/document.py", line 332, in _save
    return self.insert()
  File "apps/frappe/frappe/model/document.py", line 259, in insert
    self.run_method("before_insert")
  File "apps/frappe/frappe/model/document.py", line 931, in run_method
    out = Document.hook(fn)(self, *args, **kwargs)
  File "apps/frappe/frappe/model/document.py", line 1283, in composer
    return composed(self, method, *args, **kwargs)
  File "apps/frappe/frappe/model/document.py", line 1265, in runner
    add_to_return_value(self, fn(self, *args, **kwargs))
  File "apps/frappe/frappe/model/document.py", line 928, in fn
    return method_object(*args, **kwargs)
  File "apps/frappe/frappe/core/doctype/file/file.py", line 72, in before_insert
    self.save_file(content=self.get_content())
  File "apps/frappe/frappe/core/doctype/file/file.py", line 604, in save_file
    return write_file_method(self)
  File "apps/cloud_storage/cloud_storage/cloud_storage/overrides/file.py", line 490, in write_file
    return upload_file(file)
  File "apps/cloud_storage/cloud_storage/cloud_storage/overrides/file.py", line 407, in upload_file
    path = get_file_path(file, client.folder)
  File "apps/cloud_storage/cloud_storage/cloud_storage/overrides/file.py", line 430, in get_file_path
    file.attached_to_name.replace("#", "%23"),
AttributeError: 'NoneType' object has no attribute 'replace'

As the file is not attached to document and it tries to do file.attached_to_name.replace("#", "%23")

Alchez commented 1 month ago

@MyuddinKhatri this should be an easy enough fix. Let's just check if the file is attached, and only then replace the string. Check for other instances of this as well.