From b7e8ad6e7a302e356db8336aeccc420ff2392c60 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sat, 12 Dec 2020 14:23:14 +0000 Subject: [PATCH] Tidying --- webapp_utils.py | 50 ++++++++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/webapp_utils.py b/webapp_utils.py index 9f2c6bf41..71d0ec0d4 100644 --- a/webapp_utils.py +++ b/webapp_utils.py @@ -281,36 +281,26 @@ def updateAvatarImageCache(session, baseDir: str, httpPrefix: str, return None actorStr = actor.replace('/', '-') avatarImagePath = baseDir + '/cache/avatars/' + actorStr - if avatarUrl.endswith('.png') or \ - '.png?' in avatarUrl: - sessionHeaders = { - 'Accept': 'image/png' - } - avatarImageFilename = avatarImagePath + '.png' - elif (avatarUrl.endswith('.jpg') or - avatarUrl.endswith('.jpeg') or - '.jpg?' in avatarUrl or - '.jpeg?' in avatarUrl): - sessionHeaders = { - 'Accept': 'image/jpeg' - } - avatarImageFilename = avatarImagePath + '.jpg' - elif avatarUrl.endswith('.gif') or '.gif?' in avatarUrl: - sessionHeaders = { - 'Accept': 'image/gif' - } - avatarImageFilename = avatarImagePath + '.gif' - elif avatarUrl.endswith('.webp') or '.webp?' in avatarUrl: - sessionHeaders = { - 'Accept': 'image/webp' - } - avatarImageFilename = avatarImagePath + '.webp' - elif avatarUrl.endswith('.avif') or '.avif?' in avatarUrl: - sessionHeaders = { - 'Accept': 'image/avif' - } - avatarImageFilename = avatarImagePath + '.avif' - else: + + # try different image types + imageFormats = { + 'png': 'png', + 'jpg': 'jpeg', + 'jpeg': 'jpeg', + 'gif': 'gif', + 'webp': 'webp', + 'avif': 'avif' + } + avatarImageFilename = None + for imFormat, mimeType in imageFormats.items(): + if avatarUrl.endswith('.' + imFormat) or \ + '.' + imFormat + '?' in avatarUrl: + sessionHeaders = { + 'Accept': 'image/' + mimeType + } + avatarImageFilename = avatarImagePath + '.' + imFormat + + if not avatarImageFilename: return None if (not os.path.isfile(avatarImageFilename) or force) and allowDownloads: