diff --git a/server/app/imgserver/__init__.py b/server/app/imgserver/__init__.py index cc5057b6..04a3b59e 100644 --- a/server/app/imgserver/__init__.py +++ b/server/app/imgserver/__init__.py @@ -12,11 +12,13 @@ def join(*args: Tuple[str]) -> str: HOME = path.expanduser("~") -ROOT_PATH = path.join(HOME, ".alice", "images") +APP_DIR = join(HOME, ".alice") +IMG_PATH = path.join(APP_DIR, "images") -THUMB_PATH = join(ROOT_PATH, "thumbnails") -ARTIST_PATH = join(ROOT_PATH, "artists") -PLAYLIST_PATH = join(ROOT_PATH, "playlists") +ASSETS_PATH = join(APP_DIR, "assets") +THUMB_PATH = join(IMG_PATH, "thumbnails") +ARTIST_PATH = join(IMG_PATH, "artists") +PLAYLIST_PATH = join(IMG_PATH, "playlists") @app.route("/") @@ -24,6 +26,16 @@ def hello(): return "Hello mf" +def send_fallback_img(): + img = join(ASSETS_PATH, "default.webp") + exists = path.exists(img) + + if not exists: + return "", 404 + + return send_from_directory(ASSETS_PATH, "default.webp") + + @app.route("/t/") def send_thumbnail(imgpath: str): fpath = join(THUMB_PATH, imgpath) @@ -32,7 +44,7 @@ def send_thumbnail(imgpath: str): if exists: return send_from_directory(THUMB_PATH, imgpath) - return {"msg": "Not found"}, 404 + return send_fallback_img() @app.route("/a/") @@ -43,7 +55,7 @@ def send_artist_image(imgpath: str): if exists: return send_from_directory(ARTIST_PATH, imgpath) - return {"msg": "Not found"}, 404 + return send_fallback_img() @app.route("/p/") @@ -54,11 +66,8 @@ def send_playlist_image(imgpath: str): if exists: return send_from_directory(PLAYLIST_PATH, imgpath) - return {"msg": "Not found"}, 404 + return send_fallback_img() -# TODO -# Return Fallback images instead of JSON - if __name__ == "__main__": app.run(threaded=True, port=9877) diff --git a/server/app/prep.py b/server/app/prep.py index 55831a9b..0335c371 100644 --- a/server/app/prep.py +++ b/server/app/prep.py @@ -2,10 +2,42 @@ Contains the functions to prepare the server for use. """ import os +import shutil from app import settings +class CopyFiles: + """Copies assets to the app directory.""" + + def __init__(self) -> None: + files = [ + { + "src": "assets", + "dest": os.path.join(settings.APP_DIR, "assets"), + "is_dir": True, + } + ] + + for entry in files: + src = os.path.join(os.getcwd(), entry["src"]) + print(f"Copying {src} to {entry['dest']}") + + if entry["is_dir"]: + shutil.copytree( + src, + entry["dest"], + ignore=shutil.ignore_patterns( + "*.pyc", + ), + copy_function=shutil.copy2, + dirs_exist_ok=True, + ) + break + + shutil.copy2(src, entry["dest"]) + + def create_config_dir() -> None: """ Creates the config directory if it doesn't exist. @@ -29,3 +61,5 @@ def create_config_dir() -> None: if not exists: os.makedirs(path) os.chmod(path, 0o755) + + CopyFiles() diff --git a/server/app/settings.py b/server/app/settings.py index 9cce056e..de2e0450 100644 --- a/server/app/settings.py +++ b/server/app/settings.py @@ -10,7 +10,9 @@ import multiprocessing CONFIG_FOLDER = ".alice" HOME_DIR = os.path.expanduser("~") APP_DIR = os.path.join(HOME_DIR, CONFIG_FOLDER) -THUMBS_PATH = os.path.join(APP_DIR, "images", "thumbnails") +IMG_PATH = os.path.join(APP_DIR, "images") + +THUMBS_PATH = os.path.join(IMG_PATH, "thumbnails") TEST_DIR = "/home/cwilvx/Music/Link to Music/Chill/Wolftyla Radio" # HOME_DIR = TEST_DIR # URL @@ -42,4 +44,3 @@ CPU_COUNT = multiprocessing.cpu_count() class logger: enable = True -