89 lines
3.3 KiB
Python
89 lines
3.3 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": "avatar-admin-get-insert-delete-put",
|
||
|
|
"http_triggers": {
|
||
|
|
"avatar-admin-get-insert-delete-put-http": {
|
||
|
|
"url": "/gh/users/avatars",
|
||
|
|
"methods": ["PUT", "POST", "DELETE", "GET"]
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
```
|
||
|
|
"""
|
||
|
|
try:
|
||
|
|
if request.method == "PUT":
|
||
|
|
return make_update_avatar_request()
|
||
|
|
elif request.method == "DELETE":
|
||
|
|
return make_delete_avatar_request()
|
||
|
|
elif request.method == "POST":
|
||
|
|
return make_insert_request()
|
||
|
|
elif request.method == "GET":
|
||
|
|
return make_get_avatar_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
|
||
|
|
file = request.files.get("avatar") #Lay file tu form-data voi key la 'avatar'
|
||
|
|
if not user_id or not file:
|
||
|
|
return jsonify({"error": "user_id or file is required"}), 400
|
||
|
|
if file.mimetype not in ALLOWED_IMAGE_TYPES: #Check mimetype(kieu du lieu cua file anh)
|
||
|
|
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_update_avatar_request():
|
||
|
|
try:
|
||
|
|
user_id = request.headers.get("X-User") # Lay user_id tu header X-User, neu co giao dien roi thi cookies se tu dong duoc gui len o trong header
|
||
|
|
file = request.files.get("avatar") #Lay file tu form-data voi key la 'avatar'
|
||
|
|
if not user_id or not file:
|
||
|
|
return jsonify({"error": "user_id or file is required"}), 400
|
||
|
|
if file.mimetype not in ALLOWED_IMAGE_TYPES: #Check mimetype(kieu du lieu cua file anh)
|
||
|
|
return jsonify(
|
||
|
|
{"error": "Invalid file type. Only JPG, PNG, GIF, WEBP are allowed."}
|
||
|
|
), 400
|
||
|
|
response, status = crud.update_or_create_avatar(user_id, file) # Call CRUD function to update avatar
|
||
|
|
return jsonify(response), status
|
||
|
|
except Exception as e:
|
||
|
|
return jsonify({"error": str(e)}), 500
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
def make_delete_avatar_request():
|
||
|
|
try:
|
||
|
|
user_id = request.headers.get("X-User") # Lay user_id tu header X-User
|
||
|
|
if not user_id:
|
||
|
|
return jsonify({"error": "user_id is required"}), 400
|
||
|
|
response, status = crud.delete_avatar(user_id) # Call CRUD function to delete avatar
|
||
|
|
return jsonify(response), status
|
||
|
|
except Exception as e:
|
||
|
|
return jsonify({"error": str(e)}), 500
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
def make_get_avatar_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
|