From ea98d51fb200aea5773e6bedb1149d97a2185c4a Mon Sep 17 00:00:00 2001 From: Michael House Date: Wed, 8 Jun 2022 06:53:15 -0500 Subject: [PATCH] Refactored path building and caching on /pp/ route --- files/helpers/get.py | 23 +++++++++++++++++++++++ files/routes/users.py | 17 +++++++++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/files/helpers/get.py b/files/helpers/get.py index 788e3dc49..cae403be2 100644 --- a/files/helpers/get.py +++ b/files/helpers/get.py @@ -63,6 +63,29 @@ def get_user(username, v=None, graceful=False): 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): try: id = int(id) diff --git a/files/routes/users.py b/files/routes/users.py index b84fe5231..541f8db1e 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -1080,6 +1080,7 @@ def remove_follow(username, v): return {"message": "Follower removed!"} from urllib.parse import urlparse +import re @app.get("/pp/") @app.get("/uid//pic") @@ -1096,14 +1097,22 @@ def user_profile_uid(v, id): path = cache.get(name) tout = 5 * 60 # 5 min + # if the path isn't cached then make it if not path: user = get_account(id) 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) - + + # 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) @app.get("/@/pic")