actionFile allows file details to be passed as parameter

This commit is contained in:
shortcutme 2017-10-04 12:46:06 +02:00
parent 0dd34403a2
commit 4042de460e
No known key found for this signature in database
GPG key ID: 5B63BAE6CB9613AE

View file

@ -537,10 +537,11 @@ class UiRequest(object):
return template return template
# Stream a file to client # Stream a file to client
def actionFile(self, file_path, block_size=64 * 1024, send_header=True, header_length=True, header_noscript=False, header_allow_ajax=False): def actionFile(self, file_path, block_size=64 * 1024, send_header=True, header_length=True, header_noscript=False, header_allow_ajax=False, file_size=None, file_obj=None, path_parts=None):
if ".." in file_path: if file_size is None:
raise Exception("Invalid path") file_size = helper.getFilesize(file_path)
if os.path.isfile(file_path):
if file_size is not None:
# Try to figure out content type by extension # Try to figure out content type by extension
content_type = self.getContentType(file_path) content_type = self.getContentType(file_path)
@ -548,7 +549,6 @@ class UiRequest(object):
range_start = None range_start = None
if send_header: if send_header:
extra_headers = {} extra_headers = {}
file_size = os.path.getsize(file_path)
extra_headers["Accept-Ranges"] = "bytes" extra_headers["Accept-Ranges"] = "bytes"
if header_length: if header_length:
extra_headers["Content-Length"] = str(file_size) extra_headers["Content-Length"] = str(file_size)
@ -568,18 +568,20 @@ class UiRequest(object):
extra_headers["Access-Control-Allow-Origin"] = "null" extra_headers["Access-Control-Allow-Origin"] = "null"
self.sendHeader(status, content_type=content_type, noscript=header_noscript, extra_headers=extra_headers.items()) self.sendHeader(status, content_type=content_type, noscript=header_noscript, extra_headers=extra_headers.items())
if self.env["REQUEST_METHOD"] != "OPTIONS": if self.env["REQUEST_METHOD"] != "OPTIONS":
file = open(file_path, "rb") if not file_obj:
file_obj = open(file_path, "rb")
if range_start: if range_start:
file.seek(range_start) file_obj.seek(range_start)
while 1: while 1:
try: try:
block = file.read(block_size) block = file_obj.read(block_size)
if block: if block:
yield block yield block
else: else:
raise StopIteration raise StopIteration
except StopIteration: except StopIteration:
file.close() file_obj.close()
break break
else: # File not exists else: # File not exists
yield self.error404(file_path) yield self.error404(file_path)