60 lines
1.9 KiB
Python
60 lines
1.9 KiB
Python
|
|
import crud
|
||
|
|
from flask import jsonify, request
|
||
|
|
|
||
|
|
# from storage.minio_client import get_minio_client, check_existing_avatar_on_minio, upload_to_minio
|
||
|
|
ALLOWED_IMAGE_TYPES = {"image/jpeg", "image/png", "image/gif", "image/webp"}
|
||
|
|
|
||
|
|
|
||
|
|
def main():
|
||
|
|
"""
|
||
|
|
```fission
|
||
|
|
{
|
||
|
|
"name": "address-users-insert-get",
|
||
|
|
"http_triggers": {
|
||
|
|
"address-users-insert-get-http": {
|
||
|
|
"url": "/ailbl/users/addresses",
|
||
|
|
"methods": ["POST", "GET"]
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
```
|
||
|
|
"""
|
||
|
|
try:
|
||
|
|
if request.method == "POST":
|
||
|
|
return make_insert_request()
|
||
|
|
elif request.method == "GET":
|
||
|
|
return make_get_request()
|
||
|
|
else:
|
||
|
|
return {"error": "Method not allow"}, 405
|
||
|
|
except Exception as ex:
|
||
|
|
return jsonify({"error": str(ex)}), 500
|
||
|
|
|
||
|
|
|
||
|
|
def make_insert_request():
|
||
|
|
try:
|
||
|
|
user_id = request.headers.get("X-User") # Lay user_id tu header X-User
|
||
|
|
# Lay file tu form-data voi key la 'avatar'
|
||
|
|
file = request.files.get("avatar")
|
||
|
|
if not user_id or not file:
|
||
|
|
return jsonify({"error": "user_id or file is required"}), 400
|
||
|
|
# Check mimetype(kieu du lieu cua file anh)
|
||
|
|
if file.mimetype not in ALLOWED_IMAGE_TYPES:
|
||
|
|
return jsonify(
|
||
|
|
{"error": "Invalid file type. Only JPG, PNG, GIF, WEBP are allowed."}
|
||
|
|
), 400
|
||
|
|
response, status = crud.update_or_create_avatar(user_id, file)
|
||
|
|
return jsonify(response), status
|
||
|
|
except Exception as e:
|
||
|
|
return jsonify({"error": str(e)}), 500
|
||
|
|
|
||
|
|
|
||
|
|
def make_get_request():
|
||
|
|
try:
|
||
|
|
user_id = request.headers.get("X-User")
|
||
|
|
if not user_id:
|
||
|
|
return jsonify({"error": "user_id is required"}), 400
|
||
|
|
return crud.get_avatar_url(user_id)
|
||
|
|
# return jsonify(response), status
|
||
|
|
except Exception as e:
|
||
|
|
return jsonify({"error": str(e)}), 500
|