diff options
author | cbdev <cb@cbcdn.com> | 2022-07-04 00:44:15 +0200 |
---|---|---|
committer | cbdev <cb@cbcdn.com> | 2022-07-04 00:44:15 +0200 |
commit | 4369d6a7f024f9ebf3c5f41a8fe17bfc65d0a820 (patch) | |
tree | 9f12080da57f3ef871e17a0ff9e872fff4b2c3b0 /backend | |
parent | 1f7c87830776cd422dc93f26e9fe30543ead9104 (diff) | |
download | cargohold-4369d6a7f024f9ebf3c5f41a8fe17bfc65d0a820.tar.gz cargohold-4369d6a7f024f9ebf3c5f41a8fe17bfc65d0a820.tar.bz2 cargohold-4369d6a7f024f9ebf3c5f41a8fe17bfc65d0a820.zip |
Rename instances of session to alias where appropriate
Diffstat (limited to 'backend')
-rw-r--r-- | backend/main.py | 53 |
1 files changed, 26 insertions, 27 deletions
diff --git a/backend/main.py b/backend/main.py index 1cf577d..d1042d8 100644 --- a/backend/main.py +++ b/backend/main.py @@ -11,36 +11,36 @@ def playout(filename, content = "text/html"): def redirect(target): return ["", [('Content-Type','text/html'), ("Location", target)], "302 Redirect"] -def target_filename_internal(session, filename): - target = session["path"] + "/" +def target_filename_internal(alias, filename): + target = alias["path"] + "/" if filename: target += filename if config.user_subdirs: - target = session["user"] + "/" + target + target = alias["user"] + "/" + target return target -def target_filename(session, file): - return config.fileroot + target_filename_internal(session, file) +def target_filename(alias, file): + return config.fileroot + target_filename_internal(alias, file) def resolve_alias(alias): - session = None + resolved = None db = sqlite3.connect(config.database, check_same_thread = False) cursor = db.cursor() cursor.execute("SELECT user, real, access, storage, display FROM aliases WHERE alias=:alias", {"alias": alias}) data = cursor.fetchone() if data: - session = {"user": data[0], "path": data[1], "access": data[2], "limit": data[3], "display": data[4]} + resolved = {"user": data[0], "path": data[1], "access": data[2], "limit": data[3], "display": data[4]} else: print("Unknown alias " + alias) db.close() - return session + return resolved -def listing(session): - listing = {"total": 0, "files": [], "access": session["access"], "display": session["display"]} +def listing(alias): + listing = {"total": 0, "files": [], "access": alias["access"], "display": alias["display"]} # TODO make sorting configurable - if 'r' in session["access"]: - directory = target_filename(session, None) + if 'r' in alias["access"]: + directory = target_filename(alias, None) files = sorted(os.listdir(directory)) for filename in files: @@ -51,11 +51,11 @@ def listing(session): return [json.dumps(listing), [('Content-Type','application/json')], "200 OK"] -def upload(session, post): - if 'c' not in session["access"]: +def upload(alias, post): + if 'c' not in alias["access"]: return ["", [('Content-Type','text/html')], "403 No"] if post["file"].filename: - target = target_filename(session, os.path.basename(post["file"].filename)) + target = target_filename(alias, os.path.basename(post["file"].filename)) while os.path.isfile(target): target += "_dup" @@ -71,31 +71,31 @@ def upload(session, post): print("Upload failed") return ["", [('Content-Type','text/html')], "500 Error"] -def route(path, env, session, post): +def route(path, env, post): # Get mapped user/path/limits - session = resolve_alias(path[0]) + alias = resolve_alias(path[0]) - if not session: + if not alias: return redirect(config.homepage) - #print(json.dumps(session)) + #print(json.dumps(alias)) # Redirect if no slash after alias if len(path) == 1: return redirect(path[0] + "/"); if len(path) > 1 and path[1] == "upload": - return upload(session, post) + return upload(alias, post) if len(path) > 1 and path[1] == "listing": - return listing(session) + return listing(alias) if len(path) > 1 and path[1] == "file": - print("/data/" + target_filename_internal(session, path[2])) - return playout("/data/" + target_filename_internal(session, path[2]), mimetypes.guess_type(path[2])[0]) + print("/data/" + target_filename_internal(alias, path[2])) + return playout("/data/" + target_filename_internal(alias, path[2]), mimetypes.guess_type(path[2])[0]) - if len(path) > 1 and path[1] == "preview" and session["display"] == "gallery": - return playout("/data/" + target_filename_internal(session, "preview/" + path[2]), mimetypes.guess_type(path[2])[0]) + if len(path) > 1 and path[1] == "preview" and alias["display"] == "gallery": + return playout("/data/" + target_filename_internal(alias, "preview/" + path[2]), mimetypes.guess_type(path[2])[0]) return playout("/interface/listing.htm") @@ -103,7 +103,6 @@ def handle_request(env, response): path = env.get('PATH_INFO', '').lstrip('/').split('/') post = None headers = [] - session = None # Read POST data try: @@ -117,7 +116,7 @@ def handle_request(env, response): post = None # Route request - data, addtl_headers, code = route(path, env, session, post) + data, addtl_headers, code = route(path, env, post) headers.extend(addtl_headers) response(code if code else "200 OK", headers) return [bytes(data, "utf-8")] |