Shorter cached favicon filename

main
Bob Mottram 2021-12-17 12:01:54 +00:00
parent adedd62251
commit c6516b55c3
4 changed files with 63 additions and 36 deletions

View File

@ -7412,12 +7412,12 @@ class PubServer(BaseHTTPRequestHandler):
"""Shows a favicon image obtained from the cache
"""
favFile = path.replace('/favicons/', '')
mediaFilename = baseDir + urllib.parse.unquote_plus(path)
print('showCachedFavicon: ' + mediaFilename)
favFilename = baseDir + urllib.parse.unquote_plus(path)
print('showCachedFavicon: ' + favFilename)
if self.server.faviconsCache.get(favFile):
mediaBinary = self.server.faviconsCache[favFile]
mimeType = mediaFileMimeType(mediaFilename)
self._set_headers_etag(mediaFilename,
mimeType = mediaFileMimeType(favFilename)
self._set_headers_etag(favFilename,
mimeType,
mediaBinary, None,
refererDomain,
@ -7427,25 +7427,22 @@ class PubServer(BaseHTTPRequestHandler):
'_GET', '_showCachedFavicon2',
self.server.debug)
return
if not os.path.isfile(mediaFilename):
originalMediaFilename = mediaFilename
mediaFilename = originalMediaFilename.replace('.ico', '.png')
if not os.path.isfile(mediaFilename):
self._404()
return
if self._etag_exists(mediaFilename):
if not os.path.isfile(favFilename):
self._404()
return
if self._etag_exists(favFilename):
# The file has not changed
self._304()
return
mediaBinary = None
try:
with open(mediaFilename, 'rb') as avFile:
with open(favFilename, 'rb') as avFile:
mediaBinary = avFile.read()
except OSError:
print('EX: unable to read cached favicon ' + mediaFilename)
print('EX: unable to read cached favicon ' + favFilename)
if mediaBinary:
mimeType = mediaFileMimeType(mediaFilename)
self._set_headers_etag(mediaFilename,
mimeType = mediaFileMimeType(favFilename)
self._set_headers_etag(favFilename,
mimeType,
mediaBinary, None,
refererDomain,

View File

@ -18,6 +18,7 @@ from datetime import timezone
from collections import OrderedDict
from utils import validPostDate
from categories import setHashtagCategory
from utils import getFavFilenameFromUrl
from utils import getBaseContentFromPost
from utils import hasObjectDict
from utils import firstParagraphFromString
@ -155,23 +156,35 @@ def _downloadNewswireFeedFavicon(session, baseDir: str,
downloadImageAnyMimeType(session, favUrl, timeoutSec, debug)
if not imageData or not mimeType:
return False
if 'image/png' in mimeType:
favUrl = favUrl.replace('.ico', '.png')
elif 'image/webp' in mimeType:
favUrl = favUrl.replace('.ico', '.webp')
elif 'image/gif' in mimeType:
favUrl = favUrl.replace('.ico', '.gif')
# update the favicon url
extensionsToMime = {
'ico': 'x-icon',
'png': 'png',
'jpg': 'jpeg',
'gif': 'gif',
'avif': 'avif',
'svg': 'svg+xml',
'webp': 'webp'
}
for ext, mimeExt in extensionsToMime.items():
if 'image/' + mimeExt in mimeType:
favUrl = favUrl.replace('.ico', '.' + mimeExt)
break
# create cached favicons directory if needed
if not os.path.isdir(baseDir + '/favicons'):
os.mkdir(baseDir + '/favicons')
linkFilename = favUrl.replace('/', '-')
imageFilename = baseDir + '/favicons/' + linkFilename
if os.path.isfile(imageFilename):
# save to the cache
favFilename = getFavFilenameFromUrl(baseDir, favUrl)
if os.path.isfile(favFilename):
return True
try:
with open(imageFilename, 'wb+') as fp:
with open(favFilename, 'wb+') as fp:
fp.write(imageData)
except OSError:
print('EX: failed writing favicon ' + favUrl)
print('EX: failed writing favicon ' + favFilename)
return False
return True

View File

@ -346,7 +346,7 @@ def getAudioExtensions() -> []:
def getImageExtensions() -> []:
"""Returns a list of the possible image file extensions
"""
return ('png', 'jpg', 'jpeg', 'gif', 'webp', 'avif', 'svg')
return ('png', 'jpg', 'jpeg', 'gif', 'webp', 'avif', 'svg', 'ico')
def getImageMimeType(imageFilename: str) -> str:
@ -358,7 +358,8 @@ def getImageMimeType(imageFilename: str) -> str:
'gif': 'gif',
'avif': 'avif',
'svg': 'svg+xml',
'webp': 'webp'
'webp': 'webp',
'ico': 'x-icon'
}
for ext, mimeExt in extensionsToMime.items():
if imageFilename.endswith('.' + ext):
@ -375,7 +376,8 @@ def getImageExtensionFromMimeType(contentType: str) -> str:
'gif': 'gif',
'svg+xml': 'svg',
'webp': 'webp',
'avif': 'avif'
'avif': 'avif',
'x-icon': 'ico'
}
for mimeExt, ext in imageMedia.items():
if contentType.endswith(mimeExt):
@ -3220,3 +3222,11 @@ def getNewPostEndpoints() -> []:
'newreminder', 'newreport', 'newquestion', 'newshare', 'newwanted',
'editblogpost'
)
def getFavFilenameFromUrl(baseDir: str, faviconUrl: str) -> str:
"""Returns the cached filename for a favicon based upon its url
"""
if '://' in faviconUrl:
faviconUrl = faviconUrl.split('://')[1]
return baseDir + '/favicons/' + faviconUrl.replace('/', '-')

View File

@ -11,6 +11,7 @@ import os
from datetime import datetime
from content import removeLongWords
from content import limitRepeatedWords
from utils import getFavFilenameFromUrl
from utils import getBaseContentFromPost
from utils import removeHtml
from utils import locatePost
@ -240,15 +241,21 @@ def _htmlNewswire(baseDir: str, newswire: {}, nickname: str, moderator: bool,
faviconUrl = getNewswireFaviconUrl(url)
faviconLink = ''
if faviconUrl:
favBase = '/favicons/' + faviconUrl.replace('/', '-')
cachedFaviconFilename = baseDir + favBase
cachedFaviconFilename = getFavFilenameFromUrl(baseDir, faviconUrl)
if os.path.isfile(cachedFaviconFilename):
faviconUrl = favBase
faviconUrl = \
cachedFaviconFilename.replace(baseDir + '/favicons', '')
else:
favBase = favBase.replace('.ico', '.png')
cachedFaviconFilename = baseDir + favBase
if os.path.isfile(cachedFaviconFilename):
faviconUrl = favBase
extensions = ('png', 'jpg', 'gif', 'avif', 'svg', 'webp')
for ext in extensions:
cachedFaviconFilename = \
getFavFilenameFromUrl(baseDir, faviconUrl)
cachedFaviconFilename = \
cachedFaviconFilename.replace('.ico', '.' + ext)
if os.path.isfile(cachedFaviconFilename):
faviconUrl = \
cachedFaviconFilename.replace(baseDir +
'/favicons', '')
faviconLink = \
'<img loading="lazy" src="' + faviconUrl + '" ' + \