1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
import jinja2
import os
import config
import utils
template_factory = jinja2.Environment(loader=jinja2.FileSystemLoader('../interface/'))
admin_dirs = template_factory.get_template("admin_dirs.tmpl")
admin_files = template_factory.get_template("admin_files.tmpl")
admin_upload = template_factory.get_template("admin_upload.tmpl")
admin_delete = template_factory.get_template("admin_delete.tmpl")
admin_rename = template_factory.get_template("admin_rename.tmpl")
admin_aliases = template_factory.get_template("admin_aliases.tmpl")
def route(path, env, post):
auth = config.Auth.get(env)
if not auth:
print("No authorized user for admin access")
return utils.redirect("/")
# Directory overview
if len(path) == 1:
errmsg = None
if post and "new" in post:
try:
dirname = utils.sanitize_filename(post["new"][0])
os.mkdir(utils.userdir(auth["user"]) + dirname)
except FileExistsError:
errmsg = "This directory already exists"
except ValueError:
errmsg = "The directory name was invalid"
dirs = utils.dirlisting(utils.userdir(auth["user"]), False, True)
return [admin_dirs.render({"user": auth["user"], "listing": dirs, "error": errmsg}), [("Content-Type", "text/html")], "200 OK"]
# File listing
if len(path) == 2 and path[1]:
directory = utils.userdir(auth["user"]) + utils.sanitize_filename(path[1]) + "/"
if not os.path.isdir(directory):
return utils.redirect("/admin")
files = utils.dirlisting(directory, True, False)
return [admin_files.render({"user": auth["user"], "listing": files, "directory": utils.sanitize_filename(path[1])}), [("Content-Type", "text/html")], "200 OK"]
# Upload
if len(path) == 3 and path[2] == "upload":
return [admin_upload.render({"user": auth["user"], "directory": utils.sanitize_filename(path[1])}), [("Content-Type", "text/html")], "200 OK"]
# Alias management / Limits config
if len(path) == 3 and path[2] == "aliases":
aliases = utils.aliases(auth["user"], utils.sanitize_filename(path[1]))
return [admin_aliases.render({"user": auth["user"], "directory": utils.sanitize_filename(path[1]), "aliases": aliases, "baseurl": config.baseurl}), [("Content-Type", "text/html")], "200 OK"]
# Renaming
if len(path) == 3 and path[2] == "rename":
# TODO
return [admin_rename.render({"user": auth["user"], "directory": utils.sanitize_filename(path[1])}), [("Content-Type", "text/html")], "200 OK"]
# Deletion
if len(path) == 3 and path[2] == "delete":
directory = utils.userdir(auth["user"]) + utils.sanitize_filename(path[1]) + "/"
if not os.path.isdir(directory):
return utils.redirect("/admin")
files = utils.dirlisting(directory, True, False)
if len(files) == 0 or env.get("QUERY_STRING", "") == "confirm":
utils.cleanup(auth["user"], utils.sanitize_filename(path[1]))
return utils.redirect("/admin")
return [admin_delete.render({"user": auth["user"], "listing": files, "directory": utils.sanitize_filename(path[1])}), [("Content-Type", "text/html")], "200 OK"]
# Default case for admin / fallthrough
return utils.redirect("/")
|