Combinate isfile and filesize query to one function

This commit is contained in:
shortcutme 2017-10-04 12:44:34 +02:00
parent edb9d3f719
commit 0dd34403a2
No known key found for this signature in database
GPG key ID: 5B63BAE6CB9613AE
2 changed files with 25 additions and 10 deletions

View file

@ -11,6 +11,7 @@ from User import UserManager
from Plugin import PluginManager from Plugin import PluginManager
from Ui.UiWebsocket import UiWebsocket from Ui.UiWebsocket import UiWebsocket
from Crypt import CryptHash from Crypt import CryptHash
from util import helper
status_texts = { status_texts = {
200: "200 OK", 200: "200 OK",
@ -454,18 +455,22 @@ class UiRequest(object):
if site and site.settings["own"]: if site and site.settings["own"]:
from Debug import DebugMedia from Debug import DebugMedia
DebugMedia.merge(file_path) DebugMedia.merge(file_path)
if not address or address == ".": if not address or address == ".":
return self.error403(path_parts["inner_path"]) return self.error403(path_parts["inner_path"])
if os.path.isfile(file_path): # File exists header_allow_ajax = False
header_allow_ajax = False if self.get.get("ajax_key"):
if self.get.get("ajax_key"): site = SiteManager.site_manager.get(path_parts["request_address"])
site = SiteManager.site_manager.get(path_parts["request_address"]) if self.get["ajax_key"] == site.settings["ajax_key"]:
if self.get["ajax_key"] == site.settings["ajax_key"]: header_allow_ajax = True
header_allow_ajax = True else:
else: return self.error403("Invalid ajax_key")
return self.error403("Invalid ajax_key")
return self.actionFile(file_path, header_length=header_length, header_noscript=header_noscript, header_allow_ajax=header_allow_ajax) file_size = helper.getFilesize(file_path)
if file_size is not None:
return self.actionFile(file_path, header_length=header_length, header_noscript=header_noscript, header_allow_ajax=header_allow_ajax, file_size=file_size, path_parts=path_parts)
elif os.path.isdir(file_path): # If this is actually a folder, add "/" and redirect elif os.path.isdir(file_path): # If this is actually a folder, add "/" and redirect
if path_parts["inner_path"]: if path_parts["inner_path"]:
@ -484,7 +489,8 @@ class UiRequest(object):
result = site.needFile(path_parts["inner_path"], priority=15) # Wait until file downloads result = site.needFile(path_parts["inner_path"], priority=15) # Wait until file downloads
if result: if result:
return self.actionFile(file_path, header_length=header_length, header_noscript=header_noscript) file_size = helper.getFilesize(file_path)
return self.actionFile(file_path, header_length=header_length, header_noscript=header_noscript, header_allow_ajax=header_allow_ajax, file_size=file_size, path_parts=path_parts)
else: else:
self.log.debug("File not found: %s" % path_parts["inner_path"]) self.log.debug("File not found: %s" % path_parts["inner_path"])
# Site larger than allowed, re-add wrapper nonce to allow reload # Site larger than allowed, re-add wrapper nonce to allow reload

View file

@ -123,6 +123,15 @@ def getDirname(path):
def getFilename(path): def getFilename(path):
return path[path.rfind("/") + 1:] return path[path.rfind("/") + 1:]
def getFilesize(path):
try:
s = os.stat(path)
except:
return None
if stat.S_ISREG(s.st_mode): # Test if it's file
return s.st_size
else:
return None
# Convert hash to hashid for hashfield # Convert hash to hashid for hashfield
def toHashId(hash): def toHashId(hash):