Optimize path modifying functions
This commit is contained in:
parent
f83a466919
commit
ec70c47aa0
2 changed files with 8 additions and 11 deletions
|
@ -245,14 +245,12 @@ class SiteStorage:
|
||||||
# Security check and return path of site's file
|
# Security check and return path of site's file
|
||||||
def getPath(self, inner_path):
|
def getPath(self, inner_path):
|
||||||
inner_path = inner_path.replace("\\", "/") # Windows separator fix
|
inner_path = inner_path.replace("\\", "/") # Windows separator fix
|
||||||
inner_path = re.sub("^%s/" % re.escape(self.directory), "", inner_path) # Remove site directory if begins with it
|
|
||||||
file_path = u"%s/%s" % (self.directory, inner_path)
|
|
||||||
if not inner_path:
|
if not inner_path:
|
||||||
return self.directory
|
return self.directory
|
||||||
|
|
||||||
file_abspath = os.path.dirname(os.path.abspath(file_path))
|
file_path = u"%s/%s" % (self.directory, inner_path)
|
||||||
if ".." in file_path or not file_abspath.startswith(self.allowed_dir):
|
|
||||||
self.site.log.error(u"File %s not in allowed dir: %s" % (file_path, self.allowed_dir))
|
if ".." in file_path:
|
||||||
raise Exception(u"File not allowed: %s" % file_path)
|
raise Exception(u"File not allowed: %s" % file_path)
|
||||||
return file_path
|
return file_path
|
||||||
|
|
||||||
|
|
|
@ -86,16 +86,15 @@ def unpackOnionAddress(packed):
|
||||||
# Get dir from file
|
# Get dir from file
|
||||||
# Return: data/site/content.json -> data/site
|
# Return: data/site/content.json -> data/site
|
||||||
def getDirname(path):
|
def getDirname(path):
|
||||||
file_dir = re.sub("[^/]*?$", "", path).rstrip("/")
|
if "/" in path:
|
||||||
if file_dir:
|
return path[:path.rfind("/")+1]
|
||||||
file_dir += "/" # Add / at end if its not the root
|
else:
|
||||||
return file_dir
|
return ""
|
||||||
|
|
||||||
|
|
||||||
# Get dir from file
|
# Get dir from file
|
||||||
# Return: data/site/content.json -> content.json
|
# Return: data/site/content.json -> content.json
|
||||||
def getFilename(path):
|
def getFilename(path):
|
||||||
return re.sub("^.*/", "", path)
|
return path[path.rfind("/")+1:]
|
||||||
|
|
||||||
|
|
||||||
# Convert hash to hashid for hashfield
|
# Convert hash to hashid for hashfield
|
||||||
|
|
Loading…
Reference in a new issue