From 1e7af1a8002386e5a4372af6ea2edf6f9f520c1f Mon Sep 17 00:00:00 2001 From: geoffrey45 Date: Mon, 2 May 2022 13:45:24 +0300 Subject: [PATCH] add a flask module to serve images --- server/app/functions.py | 4 --- server/app/imgserver/__init__.py | 50 ++++++++++++++++++++++++++++++++ server/setup/nginx.setup.sh | 0 server/start.sh | 9 ++++-- 4 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 server/app/imgserver/__init__.py mode change 100644 => 100755 server/setup/nginx.setup.sh diff --git a/server/app/functions.py b/server/app/functions.py index ccc040a9..e9eaf41f 100644 --- a/server/app/functions.py +++ b/server/app/functions.py @@ -3,12 +3,9 @@ This module contains functions for the server """ import datetime import os -from pprint import pprint -import random import time from dataclasses import asdict from io import BytesIO -from typing import List, Type import requests from app import api @@ -20,7 +17,6 @@ from app.lib import albumslib from app.lib import folderslib from app.lib import watchdoge from app.lib.taglib import get_tags -from app.lib.taglib import return_album_art from app.logger import Log from PIL import Image from progress.bar import Bar diff --git a/server/app/imgserver/__init__.py b/server/app/imgserver/__init__.py new file mode 100644 index 00000000..7e751a3d --- /dev/null +++ b/server/app/imgserver/__init__.py @@ -0,0 +1,50 @@ +import os +from typing import Tuple +from flask import Flask, send_from_directory + +app = Flask(__name__) + + +def join(*args: Tuple[str]) -> str: + return os.path.join(*args) + + +HOME = os.path.expanduser("~") +ROOT_PATH = os.path.join(HOME, ".alice", "images") + +THUMB_PATH = join(ROOT_PATH, "thumbnails") +ARTIST_PATH = join(ROOT_PATH, "artists") + + +@app.route("/") +def hello(): + return "Hello mf" + + +@app.route("/thumb/") +def send_thumbnail(path: str): + name = path + ".webp" + path = join(THUMB_PATH, name) + exists = os.path.exists(path) + + if exists: + return send_from_directory(THUMB_PATH, name) + + return {"msg": "Not found"}, 404 + + +@app.route("/artist/") +def send_artist_image(path: str): + print(ARTIST_PATH) + name = path + ".webp" + path = join(ARTIST_PATH, name) + exists = os.path.exists(path) + + if exists: + return send_from_directory(ARTIST_PATH, name) + + return {"msg": "Not found"}, 404 + + +if __name__ == "__main__": + app.run(threaded=True, port=9877) diff --git a/server/setup/nginx.setup.sh b/server/setup/nginx.setup.sh old mode 100644 new mode 100755 diff --git a/server/start.sh b/server/start.sh index 5cceeae4..0b7bede2 100755 --- a/server/start.sh +++ b/server/start.sh @@ -3,6 +3,11 @@ # $ppath manage.py #python manage.py -gpath=$(poetry run which gunicorn) -"$gpath" -b 0.0.0.0:9876 -w 1 --threads=4 "manage:create_app()" #--log-level=debug +gpath=$(poetry run which gunicorn) +cd app +$gpath -b 0.0.0.0:9877 -w 4 --threads=2 "imgserver:app" & +echo "Booted image server" +cd ../ +$gpath -b 0.0.0.0:9876 -w 1 --threads=4 "manage:create_app()" #--log-level=debug +