Add uploaded profile images (SCR-1)
CI / validate-monitoring (push) Successful in 8s
CI / lint (push) Successful in 16s
CI / federation-interop (push) Successful in 1m40s
CI / test (push) Successful in 4m48s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / trigger-deploy (push) Skipped
CI / notify (push) Successful in 5s
CI / validate-monitoring (push) Successful in 8s
CI / lint (push) Successful in 16s
CI / federation-interop (push) Successful in 1m40s
CI / test (push) Successful in 4m48s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / trigger-deploy (push) Skipped
CI / notify (push) Successful in 5s
Users can upload, fetch and remove a profile image at /api/v1/me/avatar. The bytes live in an avatars folder inside ART_STORAGE_DIR; the users row records the source and content type. Adds metrics, AVATAR_UPLOAD_MAX_BYTES and the regenerated OpenAPI document. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
81e0b0cf42
commit
c55a6596d0
11 files changed
+555
-1
No files matched your search
@@ -0,0 +1,69 @@
|
||||
from flask import Response, current_app, g, request
|
||||
from flask_smorest import Blueprint, abort
|
||||
|
||||
from scrobbler import metrics
|
||||
from scrobbler.api.decorators import authenticated
|
||||
from scrobbler.schemas import AvatarUploadSchema, ErrorSchema, UserSchema
|
||||
from scrobbler.services import avatars
|
||||
|
||||
blp = Blueprint(
|
||||
"Avatar",
|
||||
__name__,
|
||||
description="Your profile image: upload one, or remove it.",
|
||||
)
|
||||
|
||||
|
||||
@blp.route("", methods=["GET"])
|
||||
@authenticated(blp)
|
||||
@blp.response(200, content_type="image/jpeg", description="Your profile image (JPEG, PNG or WebP)")
|
||||
@blp.alt_response(404, schema=ErrorSchema, description="You have no profile image")
|
||||
def get_avatar():
|
||||
"""Your profile image"""
|
||||
data = avatars.read(g.user)
|
||||
if data is None:
|
||||
abort(404, code="no_avatar", message="You have no profile image")
|
||||
response = Response(data, mimetype=g.user.avatar_content_type or "image/jpeg")
|
||||
response.headers["Cache-Control"] = "private, no-cache" # revalidate: it can change
|
||||
response.add_etag()
|
||||
return response.make_conditional(request)
|
||||
|
||||
|
||||
@blp.route("", methods=["PUT"])
|
||||
@authenticated(blp)
|
||||
@blp.arguments(AvatarUploadSchema, location="files")
|
||||
@blp.response(200, UserSchema)
|
||||
@blp.alt_response(400, schema=ErrorSchema, description="Empty file")
|
||||
@blp.alt_response(413, schema=ErrorSchema, description="Image too large")
|
||||
@blp.alt_response(415, schema=ErrorSchema, description="Not a JPEG, PNG or WebP image")
|
||||
def upload_avatar(files):
|
||||
"""Upload a profile image
|
||||
|
||||
Replaces your current one, whatever its source.
|
||||
"""
|
||||
limit = current_app.config["AVATAR_UPLOAD_MAX_BYTES"]
|
||||
data = files["file"].read(limit + 1)
|
||||
if not data:
|
||||
metrics.avatar_uploads_total.labels("empty").inc()
|
||||
abort(400, code="empty_file", message="That file is empty.")
|
||||
if len(data) > limit:
|
||||
metrics.avatar_uploads_total.labels("too_large").inc()
|
||||
abort(413, code="file_too_large", message="That image is larger than this server accepts.")
|
||||
try:
|
||||
content_type = avatars.sniff_image_type(data)
|
||||
except avatars.UnsupportedImage:
|
||||
metrics.avatar_uploads_total.labels("bad_type").inc()
|
||||
abort(415, code="unsupported_image", message="Upload a JPEG, PNG or WebP image.")
|
||||
avatars.save(g.user, data, content_type, "upload")
|
||||
metrics.avatar_uploads_total.labels("ok").inc()
|
||||
metrics.avatar_upload_bytes.observe(len(data))
|
||||
return g.user
|
||||
|
||||
|
||||
@blp.route("", methods=["DELETE"])
|
||||
@authenticated(blp)
|
||||
@blp.response(204)
|
||||
@blp.alt_response(404, schema=ErrorSchema, description="You have no profile image")
|
||||
def delete_avatar():
|
||||
"""Remove your profile image"""
|
||||
if not avatars.remove(g.user):
|
||||
abort(404, code="no_avatar", message="You have no profile image")
|
||||
Reference in new issue
Block a user