add --config flag to modify config path

+ use getters instead of constants in settings classes
+ refactor previous references
+ move get_xdg_config_dir() from settings.py to app.utils.xdg_utils.py
This commit is contained in:
geoffrey45
2023-04-09 01:01:48 +03:00
parent 30256a7998
commit 1e4484b1c8
17 changed files with 149 additions and 130 deletions
+18 -54
View File
@@ -5,21 +5,6 @@ from flask import Blueprint, send_from_directory
from app.settings import Paths
api = Blueprint("imgserver", __name__, url_prefix="/img")
SUPPORTED_IMAGES = (".jpg", ".png", ".webp", ".jpeg")
APP_DIR = Path(Paths.APP_DIR)
IMG_PATH = APP_DIR / "images"
ASSETS_PATH = APP_DIR / "assets"
THUMB_PATH = IMG_PATH / "thumbnails"
LG_THUMB_PATH = THUMB_PATH / "large"
SM_THUMB_PATH = THUMB_PATH / "small"
ARTIST_PATH = IMG_PATH / "artists"
ARTIST_LG_PATH = ARTIST_PATH / "large"
ARTIST_SM_PATH = ARTIST_PATH / "small"
PLAYLIST_PATH = IMG_PATH / "playlists"
@api.route("/")
@@ -28,86 +13,65 @@ def hello():
def send_fallback_img(filename: str = "default.webp"):
img = ASSETS_PATH / filename
path = Paths.get_assets_path()
img = Path(path) / filename
if not img.exists():
return "", 404
return send_from_directory(ASSETS_PATH, filename)
return send_from_directory(path, filename)
@api.route("/t/<imgpath>")
def send_lg_thumbnail(imgpath: str):
fpath = LG_THUMB_PATH / imgpath
path = Paths.get_lg_thumb_path()
fpath = Path(path) / imgpath
if fpath.exists():
return send_from_directory(LG_THUMB_PATH, imgpath)
return send_from_directory(path, imgpath)
return send_fallback_img()
@api.route("/t/s/<imgpath>")
def send_sm_thumbnail(imgpath: str):
fpath = SM_THUMB_PATH / imgpath
path = Paths.get_sm_thumb_path()
fpath = Path(path) / imgpath
if fpath.exists():
return send_from_directory(SM_THUMB_PATH, imgpath)
return send_from_directory(path, imgpath)
return send_fallback_img()
@api.route("/a/<imgpath>")
def send_lg_artist_image(imgpath: str):
fpath = ARTIST_LG_PATH / imgpath
path = Paths.get_artist_img_lg_path()
fpath = Path(path) / imgpath
if fpath.exists():
return send_from_directory(ARTIST_LG_PATH, imgpath)
return send_from_directory(path, imgpath)
return send_fallback_img("artist.webp")
@api.route("/a/s/<imgpath>")
def send_sm_artist_image(imgpath: str):
fpath = ARTIST_SM_PATH / imgpath
path = Paths.get_artist_img_sm_path()
fpath = Path(path) / imgpath
if fpath.exists():
return send_from_directory(ARTIST_SM_PATH, imgpath)
return send_from_directory(path, imgpath)
return send_fallback_img("artist.webp")
@api.route("/p/<imgpath>")
def send_playlist_image(imgpath: str):
fpath = PLAYLIST_PATH / imgpath
path = Paths.get_playlist_img_path()
fpath = Path(path) / imgpath
if fpath.exists():
return send_from_directory(PLAYLIST_PATH, imgpath)
return send_from_directory(path, imgpath)
return send_fallback_img("playlist.svg")
# @app.route("/raw")
# @app.route("/raw/<path:imgpath>")
# def send_from_filepath(imgpath: str = ""):
# imgpath = "/" + imgpath
# filename = path.basename(imgpath)
# def verify_is_image():
# _, ext = path.splitext(filename)
# return ext in SUPPORTED_IMAGES
# verified = verify_is_image()
# if not verified:
# return imgpath, 404
# exists = path.exists(imgpath)
# if verified and exists:
# return send_from_directory(path.dirname(imgpath), filename)
# return imgpath, 404
# def serve_imgs():
# app.run(threaded=True, port=1971, host="0.0.0.0", debug=True)