(user) better avatar cache-control
etag is added to header, If etag is the same a not modified will be respond
This commit is contained in:
parent
4be7cccadb
commit
319889ee43
|
@ -1,8 +1,11 @@
|
||||||
import re
|
import re
|
||||||
import secrets
|
import secrets
|
||||||
|
import hashlib
|
||||||
|
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from typing import Optional, Union
|
from typing import Optional, Union
|
||||||
|
from flask import make_response
|
||||||
|
from flask.json import provider
|
||||||
from sqlalchemy import exc
|
from sqlalchemy import exc
|
||||||
from sqlalchemy_utils import merge_references
|
from sqlalchemy_utils import merge_references
|
||||||
from datetime import datetime, timedelta, timezone
|
from datetime import datetime, timedelta, timezone
|
||||||
|
@ -273,8 +276,11 @@ def load_avatar(user: User, etag: Union[str, None] = None):
|
||||||
else:
|
else:
|
||||||
provider = get_provider(user.userid)
|
provider = get_provider(user.userid)
|
||||||
avatar = provider.get_avatar(user)
|
avatar = provider.get_avatar(user)
|
||||||
|
new_etag = hashlib.md5(avatar.binary).hexdigest()
|
||||||
|
if new_etag == etag:
|
||||||
|
return make_response("", 304)
|
||||||
if len(avatar.binary) > 0:
|
if len(avatar.binary) > 0:
|
||||||
return send_file(BytesIO(avatar.binary), avatar.mimetype)
|
return send_file(BytesIO(avatar.binary), avatar.mimetype, etag=new_etag)
|
||||||
raise NotFound
|
raise NotFound
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,8 +3,9 @@
|
||||||
Provides routes used to manage users
|
Provides routes used to manage users
|
||||||
"""
|
"""
|
||||||
from http.client import CREATED
|
from http.client import CREATED
|
||||||
from flask import Blueprint, request, jsonify, make_response
|
from flask import Blueprint, request, jsonify, make_response, after_this_request, Response
|
||||||
from werkzeug.exceptions import BadRequest, Forbidden, MethodNotAllowed
|
from werkzeug.exceptions import BadRequest, Forbidden, MethodNotAllowed
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
from . import permissions
|
from . import permissions
|
||||||
from flaschengeist import logger
|
from flaschengeist import logger
|
||||||
|
@ -118,10 +119,13 @@ def frontend(userid, current_session):
|
||||||
|
|
||||||
|
|
||||||
@UsersPlugin.blueprint.route("/users/<userid>/avatar", methods=["GET"])
|
@UsersPlugin.blueprint.route("/users/<userid>/avatar", methods=["GET"])
|
||||||
@headers({"Cache-Control": "public, max-age=604800"})
|
@headers({"Cache-Control": "public, must-revalidate, max-age=10"})
|
||||||
def get_avatar(userid):
|
def get_avatar(userid):
|
||||||
|
etag = None
|
||||||
|
if "If-None-Match" in request.headers:
|
||||||
|
etag = request.headers["If-None-Match"]
|
||||||
user = userController.get_user(userid)
|
user = userController.get_user(userid)
|
||||||
return userController.load_avatar(user)
|
return userController.load_avatar(user, etag)
|
||||||
|
|
||||||
|
|
||||||
@UsersPlugin.blueprint.route("/users/<userid>/avatar", methods=["POST"])
|
@UsersPlugin.blueprint.route("/users/<userid>/avatar", methods=["POST"])
|
||||||
|
|
Loading…
Reference in New Issue