From c6516b55c3109cc23dc0bf5a0ceeaf03ce69ed5e Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Fri, 17 Dec 2021 12:01:54 +0000 Subject: [PATCH] Shorter cached favicon filename --- daemon.py | 27 ++++++++++++--------------- newswire.py | 35 ++++++++++++++++++++++++----------- utils.py | 16 +++++++++++++--- webapp_column_right.py | 21 ++++++++++++++------- 4 files changed, 63 insertions(+), 36 deletions(-) diff --git a/daemon.py b/daemon.py index 2c725975c..02b8cdf58 100644 --- a/daemon.py +++ b/daemon.py @@ -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, diff --git a/newswire.py b/newswire.py index 48e85c5c8..03e766a91 100644 --- a/newswire.py +++ b/newswire.py @@ -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 diff --git a/utils.py b/utils.py index 06544bdb0..6a0e888e5 100644 --- a/utils.py +++ b/utils.py @@ -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('/', '-') diff --git a/webapp_column_right.py b/webapp_column_right.py index 2ca5bc12a..3cd69a86c 100644 --- a/webapp_column_right.py +++ b/webapp_column_right.py @@ -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 = \ '