From 972727cc8bed676e73f0d1e779854575095c6395 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Tue, 4 Feb 2020 19:34:52 +0000 Subject: [PATCH] Clearing avatar images from cache before storing updated ones --- daemon.py | 13 ++----------- inbox.py | 7 ++----- utils.py | 10 ++++++++++ webinterface.py | 3 ++- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/daemon.py b/daemon.py index 47b8f4047..a0f686397 100644 --- a/daemon.py +++ b/daemon.py @@ -147,6 +147,7 @@ from shares import outboxUndoShareUpload from shares import addShare from shares import removeShare from shares import expireShares +from utils import removeAvatarFromCache from utils import locatePost from utils import getCachedPostFilename from utils import removePostFromCache @@ -4308,17 +4309,7 @@ class PubServer(BaseHTTPRequestHandler): actorJson['id'],actorJson, \ self.server.personCache) # clear any cached images for this actor - avatarCacheFilename= \ - self.server.baseDir+'/cache/avatars/'+ \ - actorJson['id'].replace('/','#') - if os.path.isfile(avatarCacheFilename+'.png'): - os.remove(avatarCacheFilename+'.png') - if os.path.isfile(avatarCacheFilename+'.jpg'): - os.remove(avatarCacheFilename+'.jpg') - if os.path.isfile(avatarCacheFilename+'.gif'): - os.remove(avatarCacheFilename+'.gif') - if os.path.isfile(avatarCacheFilename+'.webp'): - os.remove(avatarCacheFilename+'.webp') + removeAvatarFromCache(self.server.baseDir,actorJson['id'].replace('/','-')) # save the actor to the cache actorCacheFilename= \ self.server.baseDir+'/cache/actors/'+ \ diff --git a/inbox.py b/inbox.py index 6eb2a8972..886f96b00 100644 --- a/inbox.py +++ b/inbox.py @@ -12,6 +12,7 @@ import datetime import time import json from shutil import copyfile +from utils import removeAvatarFromCache from utils import isPublicPost from utils import getCachedPostFilename from utils import removePostFromCache @@ -775,11 +776,7 @@ def personReceiveUpdate(baseDir: str, \ # remove avatar if it exists so that it will be refreshed later # when a timeline is constructed actorStr=personJson['id'].replace('/','-') - avatarFilenameExtensions=('png','jpg','gif','webp') - for extension in avatarFilenameExtensions: - avatarFilename=baseDir+'/cache/avatars/'+actorStr+'.'+extension - if os.path.isfile(avatarFilename): - os.remove(avatarFilename) + removeAvatarFromCache(baseDir,actorStr) return True def receiveUpdateToQuestion(recentPostsCache: {},messageJson: {}, \ diff --git a/utils.py b/utils.py index e0d757ace..7b4ccc6fe 100644 --- a/utils.py +++ b/utils.py @@ -12,6 +12,16 @@ import shutil import datetime import json +def removeAvatarFromCache(baseDir: str,actorStr: str) -> None: + """Removes any existing avatar entries from the cache + This avoids duplicate entries with differing extensions + """ + avatarFilenameExtensions=('png','jpg','gif','webp') + for extension in avatarFilenameExtensions: + avatarFilename=baseDir+'/cache/avatars/'+actorStr+'.'+extension + if os.path.isfile(avatarFilename): + os.remove(avatarFilename) + def saveJson(jsonObject: {},filename: str) -> bool: """Saves json to a file """ diff --git a/webinterface.py b/webinterface.py index dc0d81342..373040bbf 100644 --- a/webinterface.py +++ b/webinterface.py @@ -68,7 +68,8 @@ def updateAvatarImageCache(session,baseDir: str,httpPrefix: str,actor: str,avata """ if not avatarUrl: return None - avatarImagePath=baseDir+'/cache/avatars/'+actor.replace('/','-') + actorStr=actor.replace('/','-') + avatarImagePath=baseDir+'/cache/avatars/'+actorStr if avatarUrl.endswith('.png') or '.png?' in avatarUrl: sessionHeaders = {'Accept': 'image/png'} avatarImageFilename=avatarImagePath+'.png'