Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

500 internal server error for frappe.utils.file_manager.download_file #26424

Closed
perotom opened this issue May 13, 2024 · 6 comments · Fixed by #26575
Closed

500 internal server error for frappe.utils.file_manager.download_file #26424

perotom opened this issue May 13, 2024 · 6 comments · Fixed by #26575

Comments

@perotom
Copy link

perotom commented May 13, 2024

Description of the issue

Context information (for bug reports)

Output of bench version

erpnext 15.23.0
frappe 15.26.0
hrms 16.0.0-dev
payments 0.0.1

Steps to reproduce the issue

  1. Upload file to doc
  2. Try to download it via API call
f = requests.get('https://domain.com/api/method/frappe.utils.file_manager.download_file', headers={ "Authorization": "token " + key }, params={
    'file_url': fileName
})
f.raise_for_status()

Observed result

500 - INTERNAL SERVER ERROR

Expected result

Content of file

Stacktrace / full error message

Nothing in Error log doc type.
frappe.log:

Form Dict: {'file_url': '/private/files/_INV-IT-176146481-2024-4647.pdf', 'cmd': 'frappe.utils.file_manager.download_file'}
2024-05-13 08:44:25,716 ERROR frappe New Exception collected in error log
Site: *****
Form Dict: {'file_url': '/private/files/_INV-IT-176146481-2024-4647.pdf', 'cmd': 'frappe.utils.file_manager.download_file'}

Additional information

OS version / distribution, Frappe install method, etc.

Ubuntu 22.04.3 LTS

@perotom perotom added the bug label May 13, 2024
@gavindsouza
Copy link
Collaborator

Have you tried looking into the response? Like what did the server respond in the body, r.content according to your example usage?

@perotom
Copy link
Author

perotom commented May 23, 2024

The response is:

{
    "exception":"FileNotFoundError: [Errno 2] No such file or directory: \'./domain.com/public/files/4200075788.pdf\'",
    "exc_type": "FileNotFoundError",
    "exc": "[\\"Traceback (most recent call last):\\\\n  File \\\\\\"apps/frappe/frappe/app.py\\\\\\", line 110, in application\\\\n    response = frappe.api.handle(request)\\\\n  File \\\\\\"apps/frappe/frappe/api/__init__.py\\\\\\", line 49, in handle\\\\n    data = endpoint(**arguments)\\\\n  File \\\\\\"apps/frappe/frappe/api/v1.py\\\\\\", line 36, in handle_rpc_call\\\\n    return frappe.handler.handle()\\\\n  File \\\\\\"apps/frappe/frappe/handler.py\\\\\\", line 49, in handle\\\\n    data = execute_cmd(cmd)\\\\n  File \\\\\\"apps/frappe/frappe/handler.py\\\\\\", line 85, in execute_cmd\\\\n    return frappe.call(method, **frappe.form_dict)\\\\n  File \\\\\\"apps/frappe/frappe/__init__.py\\\\\\", line 1768, in call\\\\n    return fn(*args, **newargs)\\\\n  File \\\\\\"apps/frappe/frappe/utils/typing_validations.py\\\\\\", line 31, in wrapper\\\\n    return func(*args, **kwargs)\\\\n  File \\\\\\"apps/frappe/frappe/utils/file_manager.py\\\\\\", line 415, in download_file\\\\n    with open(path, \\\\\\"rb\\\\\\") as fileobj:\\\\nFileNotFoundError: [Errno 2
    ] No such file or directory: \'./domain.com/public/files/4200075788.pdf\'\\\\n\\"]"
}

I just realised it changed the private path to public. Is it not possible to use this method on private files? Is there any other way to download files without making them public?

@gavindsouza
Copy link
Collaborator

Is it not possible to use this method on private files? Is there any other way to download files without making them public?

You should be able to. Check out the File document for the image you're trying to download, it likely has "public" ticked while it's in private...the state is inconsistent and should not exist under regular conditions.

@perotom
Copy link
Author

perotom commented May 27, 2024

No it is a private file:

{
	"data": {
		"name": "af4e63dc8d",
		"docstatus": 0,
		"idx": 0,
		"file_name": "4200075788.pdf",
		"is_private": 1,
		"file_type": "PDF",
		"is_home_folder": 0,
		"is_attachments_folder": 0,
		"file_size": 71429,
		"file_url": "/private/files/4200075788.pdf",
		"folder": "Home/Attachments",
		"is_folder": 0,
		"attached_to_doctype": "Purchase Invoice",
		"attached_to_name": "ACC-PINV-2024-00126",
		"content_hash": "83b1072b99209acd5e006fca5ab302a3",
		"uploaded_to_dropbox": 0,
		"uploaded_to_google_drive": 0,
		"doctype": "File"
	}
}

@gavindsouza
Copy link
Collaborator

gavindsouza commented May 27, 2024

Okay, for some reason, I didn't realize this earlier - you're using a legacy API that doesn't work intuitively/is buggy.

Since you are using the API, and know the file URL, you can just hit the endpoint directly:

f = requests.get(f'https://domain.com/{fileName}', headers={ "Authorization": f"token {key}" })

Alternatively, you can also use the /api/method/download_file endpoint which should work as a drop-in replacement in your code.

gavindsouza added a commit to gavindsouza/frappe that referenced this issue May 27, 2024
gavindsouza added a commit to gavindsouza/frappe that referenced this issue May 27, 2024
ankush pushed a commit that referenced this issue May 27, 2024
mergify bot pushed a commit that referenced this issue May 27, 2024
ankush pushed a commit that referenced this issue May 27, 2024
Closes #26424

(cherry picked from commit a71df18)

Co-authored-by: gavin <gavin.dsouza@switchup.de>
@frappe-pr-bot
Copy link
Collaborator

🎉 This issue has been resolved in version 15.29.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants