aboutsummaryrefslogtreecommitdiff
path: root/api/user/avatar.py
blob: 35896537cfce2b6547f58a57fe9d7a37310efe72 (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
from flask import Blueprint, request, Response
from db import cursor
from hierarchy import auth_required
from os.path import exists
from codecs import decode
import valid

default_avatar = open("database/avatars/default.png", "rb").read()

avatar = Blueprint('avatar', __name__)


@avatar.route('/avatar', methods=["GET"])
@auth_required("none")
def get_avatar(token_id):
    user_id = request.args.get("id") or token_id
    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")


#TODO: pillow image size validation (client side resize)
@avatar.route('/avatar', methods=["POST"])
@auth_required("user")
def update_avatar(user_id):
    if not request.data: return "", 400

    open(f"database/avatars/{user_id}.png", "wb") \
           .write(decode(request.data, "base64"))

    return "", 200


dynamic_route = ["/user", avatar]