diff --git a/serve.py b/serve.py index 56a0068..47fedb6 100644 --- a/serve.py +++ b/serve.py @@ -12,6 +12,17 @@ GRAB_SYMLINKS = True # Toggle symlink handling on/off SYMLINK_DIR = os.path.abspath("symlinks") # Directory to resolve symlinks to STATIC_SHARE_RESTRICTION = True # Toggle restriction on/off +def create_symlink_to_dir(symlink_target, link_path): + try: + if os.path.islink(link_path) or os.path.exists(link_path): + print(f"Path '{link_path}' already exists.") + return + + os.symlink(symlink_target, link_path) + print(f"Symlink created: {link_path} -> {symlink_target}") + except OSError as e: + print(f"Failed to create symlink: {e}") + def get_project_directory(project_id): project_dir = os.path.join(BASE_DIR, "projects", project_id) if GRAB_SYMLINKS and not os.path.exists(project_dir): @@ -21,8 +32,11 @@ def get_project_directory(project_id): # Find the symlink directory that starts with the project_id symlink_dirs = [d for d in os.listdir(SYMLINK_DIR) if os.path.isdir(os.path.join(SYMLINK_DIR, d))] for symlink_dir in symlink_dirs: - if symlink_dir.startswith(project_id): - return os.path.join(SYMLINK_DIR, symlink_dir) + if symlink_dir == project_id or symlink_dir.startswith(project_id + "-"): + symlink_dir = os.path.join(SYMLINK_DIR, symlink_dir) + if os.path.exists(symlink_dir): + create_symlink_to_dir(symlink_dir, project_dir) + break return project_dir class CustomHandler(http.server.SimpleHTTPRequestHandler):