浏览代码

Download files using REST API and OAuth 2 Token (#3499)

* Download files using REST API and OAuth 2 Token

* Check permissions before file download via API

* Solves Codacy issues

https://www.codacy.com/app/netchampfaris/frappe/file/7377221800/issues/source?bid=4679759&fileBranchId=4768213#l364
version-14
Revant Nandgaonkar 8 年前
committed by Rushabh Mehta
父节点
当前提交
b06f987e55
共有 1 个文件被更改,包括 19 次插入0 次删除
  1. +19
    -0
      frappe/utils/file_manager.py

+ 19
- 0
frappe/utils/file_manager.py 查看文件

@@ -350,3 +350,22 @@ def get_file_name(fname, optional_suffix):
partial, extn = f[0], "." + f[1]
return '{partial}{suffix}{extn}'.format(partial=partial, extn=extn, suffix=optional_suffix)
return fname

@frappe.whitelist()
def download_file(file_url):
"""
Download file using token and REST API. Valid session or
token is required to download private files.

Method : GET
Endpoint : frappe.utils.file_manager.download_file
URL Params : file_name = /path/to/file relative to site path
"""
file_doc = frappe.get_doc("File", {"file_url":file_url})
file_doc.check_permission("read")

with open(getattr(frappe.local, "site_path", None) + file_url, "rb") as fileobj:
filedata = fileobj.read()
frappe.local.response.filename = file_url[file_url.rfind("/")+1:]
frappe.local.response.filecontent = filedata
frappe.local.response.type = "download"

正在加载...
取消
保存