move imgserver to app/api folder

+ add sqlite methods to configure custom root directories
+ add sqlite.settings module
+ remove date and app name from logger messages
+ add api route to browse directories
This commit is contained in:
geoffrey45
2023-01-21 18:07:20 +03:00
parent 3dc9bc1f15
commit 4e6e1f03dc
9 changed files with 179 additions and 31 deletions
+13 -3
View File
@@ -5,8 +5,17 @@ This module combines all API blueprints into a single Flask app instance.
from flask import Flask
from flask_cors import CORS
from app.api import album, artist, favorites, folder, playlist, search, track
from app.imgserver import imgbp as imgserver
from app.api import (
album,
artist,
favorites,
folder,
playlist,
search,
track,
settings,
imgserver,
)
def create_api():
@@ -25,6 +34,7 @@ def create_api():
app.register_blueprint(folder.folderbp)
app.register_blueprint(playlist.playlistbp)
app.register_blueprint(favorites.favbp)
app.register_blueprint(imgserver)
app.register_blueprint(imgserver.imgbp)
app.register_blueprint(settings.settingsbp)
return app
+27
View File
@@ -1,11 +1,13 @@
"""
Contains all the folder routes.
"""
import os
from flask import Blueprint, request
from app import settings
from app.lib.folderslib import GetFilesAndDirs
folderbp = Blueprint("folder", __name__, url_prefix="/")
@@ -30,3 +32,28 @@ def get_folder_tree():
"tracks": tracks,
"folders": sorted(folders, key=lambda i: i.name),
}
@folderbp.route("/folder/dir-browser", methods=["POST"])
def list_folders():
"""
Returns a list of all the folders in the given folder.
"""
data = request.get_json()
try:
req_dir: str = data["folder"]
except KeyError:
req_dir = settings.HOME_DIR
if req_dir == "$home":
req_dir = settings.HOME_DIR
entries = os.scandir(req_dir)
dirs = [e.name for e in entries if e.is_dir() and not e.name.startswith(".")]
dirs = [{"name": d, "path": os.path.join(req_dir, d)} for d in dirs]
return {
"folders": sorted(dirs, key=lambda i: i["name"]),
}
+114
View File
@@ -0,0 +1,114 @@
import os
from pathlib import Path
from flask import Blueprint, send_from_directory
imgbp = Blueprint("imgserver", __name__, url_prefix="/img")
SUPPORTED_IMAGES = (".jpg", ".png", ".webp", ".jpeg")
HOME = os.path.expanduser("~")
APP_DIR = Path(HOME) / ".swing"
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"
@imgbp.route("/")
def hello():
return "<h1>Image Server</h1>"
def send_fallback_img(filename: str = "default.webp"):
img = ASSETS_PATH / filename
if not img.exists():
return "", 404
return send_from_directory(ASSETS_PATH, filename)
@imgbp.route("/t/<imgpath>")
def send_lg_thumbnail(imgpath: str):
fpath = LG_THUMB_PATH / imgpath
if fpath.exists():
return send_from_directory(LG_THUMB_PATH, imgpath)
return send_fallback_img()
@imgbp.route("/t/s/<imgpath>")
def send_sm_thumbnail(imgpath: str):
fpath = SM_THUMB_PATH / imgpath
if fpath.exists():
return send_from_directory(SM_THUMB_PATH, imgpath)
return send_fallback_img()
@imgbp.route("/a/<imgpath>")
def send_lg_artist_image(imgpath: str):
fpath = ARTIST_LG_PATH / imgpath
if fpath.exists():
return send_from_directory(ARTIST_LG_PATH, imgpath)
return send_fallback_img("artist.webp")
@imgbp.route("/a/s/<imgpath>")
def send_sm_artist_image(imgpath: str):
fpath = ARTIST_SM_PATH / imgpath
if fpath.exists():
return send_from_directory(ARTIST_SM_PATH, imgpath)
return send_fallback_img("artist.webp")
@imgbp.route("/p/<imgpath>")
def send_playlist_image(imgpath: str):
fpath = PLAYLIST_PATH / imgpath
if fpath.exists():
return send_from_directory(PLAYLIST_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)
+61
View File
@@ -0,0 +1,61 @@
from flask import Blueprint, request
from app.db.sqlite.settings import SettingsSQLMethods as sdb
settingsbp = Blueprint("settings", __name__, url_prefix="/")
@settingsbp.route("/settings/add-root-dirs", methods=["POST"])
def add_root_dirs():
"""
Add custom root directories to the database.
"""
msg = {"msg": "Failed! No directories were given."}
data = request.get_json()
if data is None:
return msg, 400
try:
new_dirs = data["new_dirs"]
removed_dirs = data["removed"]
except KeyError:
return msg, 400
sdb.add_root_dirs(new_dirs)
sdb.remove_root_dirs(removed_dirs)
return {"msg": "Added root directories to the database."}
@settingsbp.route("/settings/get-root-dirs", methods=["GET"])
def get_root_dirs():
"""
Get custom root directories from the database.
"""
dirs = sdb.get_root_dirs()
return {"dirs": dirs}
# CURRENTLY UNUSED ROUTE 👇
@settingsbp.route("/settings/remove-root-dirs", methods=["POST"])
def remove_root_dirs():
"""
Remove custom root directories from the database.
"""
msg = {"msg": "Failed! No directories were given."}
data = request.get_json()
if data is None:
return msg, 400
try:
dirs = data["dirs"]
except KeyError:
return msg, 400
sdb.remove_root_dirs(dirs)
return {"msg": "Removed root directories from the database."}