Refactored path building and caching on /pp/ route

This commit is contained in:
Michael House 2022-06-08 06:53:15 -05:00
parent 658eacb45d
commit ea98d51fb2
2 changed files with 36 additions and 4 deletions

View file

@ -63,6 +63,29 @@ def get_user(username, v=None, graceful=False):
return user return user
def get_users(usernames, v=None, graceful=False):
if not usernames:
if not graceful: abort(404)
else: return []
def clean(n):
return n.replace('\\', '').replace('_', '\_').replace('%', '').strip()
usernames = [ clean(n) for n in usernames ]
users = g.db.query(User).filter(
or_(
User.username == any_(usernames),
User.original_username == any_(usernames)
)
).all()
if not users:
if not graceful: abort(404)
else: return []
return users
def get_account(id, v=None): def get_account(id, v=None):
try: id = int(id) try: id = int(id)

View file

@ -1080,6 +1080,7 @@ def remove_follow(username, v):
return {"message": "Follower removed!"} return {"message": "Follower removed!"}
from urllib.parse import urlparse from urllib.parse import urlparse
import re
@app.get("/pp/<id>") @app.get("/pp/<id>")
@app.get("/uid/<id>/pic") @app.get("/uid/<id>/pic")
@ -1096,14 +1097,22 @@ def user_profile_uid(v, id):
path = cache.get(name) path = cache.get(name)
tout = 5 * 60 # 5 min tout = 5 * 60 # 5 min
# if the path isn't cached then make it
if not path: if not path:
user = get_account(id) user = get_account(id)
path = urlparse(user.profile_url).path path = urlparse(user.profile_url).path
if path.startswith('/assets'):
path = path.lstrip('/')
path = os.path.join(app.root_path, path)
cache.set(name,path,timeout=tout) cache.set(name,path,timeout=tout)
# if not found, search relative to the root
if not os.path.exists(path):
path = os.path.join(app.root_path,path.lstrip('/'))
cache.set(name,path,timeout=tout)
# if not found, fail
if not os.path.exists(path):
cache.set(name,None)
abort(404)
return send_file(path) return send_file(path)
@app.get("/@<username>/pic") @app.get("/@<username>/pic")