diff options
author | lonkaars <l.leblansch@gmail.com> | 2021-03-14 14:21:11 +0100 |
---|---|---|
committer | lonkaars <l.leblansch@gmail.com> | 2021-03-14 14:21:11 +0100 |
commit | 7ad4c4a45d9ce21e164f0dcfb5e22b23b97c1b31 (patch) | |
tree | 893ebf5861a405cdbc1cb484561833c1e611213e | |
parent | a87831846d1f82c0da851e55c6599f6f5a24791f (diff) |
avatar endpoint for serving avatars :tada:
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | api/user/avatar.py | 31 | ||||
-rw-r--r-- | database/avatars/default.png | bin | 0 -> 3135 bytes |
3 files changed, 33 insertions, 0 deletions
@@ -18,6 +18,8 @@ ENV/ database/*.db database/*.db-journal database/*.sqlext +database/avatars/* +!database/avatars/default.png # node / ts *.js diff --git a/api/user/avatar.py b/api/user/avatar.py new file mode 100644 index 0000000..b0d2701 --- /dev/null +++ b/api/user/avatar.py @@ -0,0 +1,31 @@ +from flask import Blueprint, request, Response +from db import cursor +from auth.login_token import token_login +from user.info import valid_user_id +from os.path import exists + +default_avatar = open("database/avatars/default.png", "rb").read() + +avatar = Blueprint('avatar', __name__) + +@avatar.route('/avatar', methods = ["GET"]) +def get_avatar(): + token = request.cookies.get("token") or "" + login = token_login(token) or "" + + user_id = request.args.get("id") or login + if not user_id: return "", 400 + if not valid_user_id(user_id): return "", 403 + + avatar_path = f"database/avatars/{user_id}.png" + avatar = "" + if exists(avatar_path): + avatar = open(avatar_path, "rb").read() + return Response(avatar or default_avatar, 200, mimetype="image/png") + +@avatar.route('/avatar', methods = ["POST"]) #TODO: pillow image size validation (client side resize) +def update_avatar(): + return "", 200 + +dynamic_route = ["/user", avatar] + diff --git a/database/avatars/default.png b/database/avatars/default.png Binary files differnew file mode 100644 index 0000000..ae6313e --- /dev/null +++ b/database/avatars/default.png |