Tidying to reduce file reads

merge-requests/8/head
Bob Mottram 2020-08-29 20:54:30 +01:00
parent 7ef0a275f0
commit 3179a37975
4 changed files with 21 additions and 25 deletions

View File

@ -45,8 +45,9 @@ def getPersonFromCache(baseDir: str, personUrl: str, personCache: {},
# does the person exist as a cached file?
cacheFilename = baseDir + '/cache/actors/' + \
personUrl.replace('/', '#')+'.json'
if os.path.isfile(getFileCaseInsensitive(cacheFilename)):
personJson = loadJson(getFileCaseInsensitive(cacheFilename))
actorFilename = getFileCaseInsensitive(cacheFilename)
if actorFilename:
personJson = loadJson(actorFilename)
if personJson:
storePersonInCache(baseDir, personUrl, personJson,
personCache, False)

View File

@ -7516,7 +7516,8 @@ class PubServer(BaseHTTPRequestHandler):
if fields.get('removeTwitter'):
if fields['removeTwitter'] == 'on':
removeTwitterActive = True
with open(removeTwitterFilename, 'w+') as rFile:
with open(removeTwitterFilename,
'w+') as rFile:
rFile.write('\n')
if not removeTwitterActive:
if os.path.isfile(removeTwitterFilename):

View File

@ -907,21 +907,19 @@ def searchBoxPosts(baseDir: str, nickname: str, domain: str,
def getFileCaseInsensitive(path: str) -> str:
"""Returns a case specific filename given a case insensitive version of it
"""
# does the given file exist? If so then we don't need
# to do a directory search
if os.path.isfile(path):
return path
if path != path.lower():
if os.path.isfile(path.lower()):
return path.lower()
directory, filename = os.path.split(path)
directory, filename = (directory or '.'), filename.lower()
for f in os.listdir(directory):
if f.lower() == filename:
newpath = os.path.join(directory, f)
if os.path.isfile(newpath):
return newpath
return path
# directory, filename = os.path.split(path)
# directory, filename = (directory or '.'), filename.lower()
# for f in os.listdir(directory):
# if f.lower() == filename:
# newpath = os.path.join(directory, f)
# if os.path.isfile(newpath):
# return newpath
return None
def undoLikesCollectionEntry(recentPostsCache: {},

View File

@ -27,7 +27,6 @@ from matrix import getMatrixAddress
from donate import getDonationUrl
from utils import removeIdEnding
from utils import getProtocolPrefixes
from utils import getFileCaseInsensitive
from utils import searchBoxPosts
from utils import isEventPost
from utils import isBlogPost
@ -304,16 +303,13 @@ def getPersonAvatarUrl(baseDir: str, personUrl: str, personCache: {},
# get from locally stored image
actorStr = personJson['id'].replace('/', '-')
avatarImagePath = baseDir + '/cache/avatars/' + actorStr
if os.path.isfile(getFileCaseInsensitive(avatarImagePath + '.png')):
return '/avatars/' + actorStr + '.png'
elif os.path.isfile(getFileCaseInsensitive(avatarImagePath + '.jpg')):
return '/avatars/' + actorStr + '.jpg'
elif os.path.isfile(getFileCaseInsensitive(avatarImagePath + '.gif')):
return '/avatars/' + actorStr + '.gif'
elif os.path.isfile(getFileCaseInsensitive(avatarImagePath + '.webp')):
return '/avatars/' + actorStr + '.webp'
elif os.path.isfile(getFileCaseInsensitive(avatarImagePath)):
return '/avatars/' + actorStr
imageExtension = ('png', 'jpg', 'jpeg', 'gif', 'webp')
for ext in imageExtension:
if os.path.isfile(avatarImagePath + '.' + ext):
return '/avatars/' + actorStr + '.' + ext
elif os.path.isfile(avatarImagePath.lower() + '.' + ext):
return '/avatars/' + actorStr.lower() + '.' + ext
if personJson.get('icon'):
if personJson['icon'].get('url'):
@ -6649,7 +6645,7 @@ def htmlCalendar(translate: {},
for weekOfMonth in range(1, 7):
if dayOfMonth == daysInMonth:
continue
calendarStr += ' <tr>\n'
calendarStr += ' <tr>\n'
for dayNumber in range(1, 8):
if (weekOfMonth > 1 and dayOfMonth < daysInMonth) or \
(weekOfMonth == 1 and dayNumber >= dow):