provide fallback image

return fallback image if image is not found @imgserver
This commit is contained in:
geoffrey45
2022-06-20 13:00:59 +03:00
parent 12c8406a0d
commit 84880f8cad
3 changed files with 56 additions and 12 deletions
+19 -10
View File
@@ -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/<imgpath>")
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/<imgpath>")
@@ -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/<imgpath>")
@@ -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)
+34
View File
@@ -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()
+3 -2
View File
@@ -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