summaryrefslogtreecommitdiff
path: root/flaskapi.py
blob: 7d10a7ed927d7649d4b78264eb0693d090bc519d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from flask import Flask, Blueprint, request, send_from_directory, send_file, abort, redirect, jsonify
from flask_socketio import SocketIO, emit
from flask_cors import CORS
from engineio.async_drivers import threading  # For pyinstaller

from manager import PixMan
from settings import Config


app = Flask(__name__, static_folder=PixMan().static_dir, static_url_path="/")
api = Blueprint("api", __name__)
socketio = SocketIO(app, async_mode="threading", cors_allowed_origins="*")  # NOTE debug, async_mode for pyinstaller
CORS(api, origins="*")  # NOTE debug


@app.route("/")
@app.route("/slideshow")
@app.route("/albums")
@app.route("/settings")
def home():
    return send_from_directory(PixMan().static_public, "index.html")


@socketio.on("seek")
def seek(increment):
    if not (display := PixMan().display):
        return {}
    display.queue.put(lambda: display.seek(increment))
    while not display.queue.empty():
        pass
    return { "imageIndex": display.current_texture_index }


@api.route("/albums/update", methods=["POST"])
def albums_update():
    return { "success": PixMan().update_config({ "album_list": request.json }) }

@api.route("/albums")
def albums_get():
    if not (ic := PixMan().immich_connector):
        return {}
    keys = [ "albumName", "albumThumbnailAssetId", "id", "startDate", "endDate", "assetCount", "shared", ]
    selected_albums = PixMan().config.album_list
    return [{
        key: album.get(key, None) for key in keys
    } | { "selected": album["id"] in selected_albums } for album in ic.load_all_albums() if album["assetCount"] ]


@api.route("/asset/<key>/filename")
def get_asset_name(key):
    if not (ic := PixMan().immich_connector):
        return {}
    # TODO ensure getting actual album thumb
    name = ic.load_image_filename(key)
    if name is None:
        abort(400)
    return { "filename": name }


@api.route("/asset/<key>/fullsize",  defaults={ "size": "fullsize" })
@api.route("/asset/<key>/thumbnail", defaults={ "size": "thumbnail" })
@api.route("/asset/<key>",           defaults={ "size": "preview" })
def get_asset(key, size):
    if not (ic := PixMan().immich_connector):
        return {}
    # TODO ensure getting actual album thumb
    image_data, mimetype = ic.load_image(key, size=size)
    if image_data is None:
        abort(400)
    return send_file(image_data, mimetype=mimetype)


@api.route("/redirect/<path:path>")
def immich_redirect(path):
    if not (ic := PixMan().immich_connector):
        return {}
    return redirect(f"{ic.server_url}/{path}")


@api.route("/config/update", methods=["POST"])
def config_update():
    return { "success": PixMan().update_config(request.json) }

@api.route("/config")
def config_get():
    return jsonify(PixMan().config)


app.register_blueprint(api, url_prefix="/api")