Detect favicon mime type

main
Bob Mottram 2021-12-16 23:32:13 +00:00
parent 791bbcf423
commit 6da17599ca
3 changed files with 28 additions and 7 deletions

View File

@ -7416,8 +7416,9 @@ class PubServer(BaseHTTPRequestHandler):
print('showCachedFavicon: ' + mediaFilename)
if self.server.faviconsCache.get(favFile):
mediaBinary = self.server.faviconsCache[favFile]
mimeType = mediaFileMimeType(mediaFilename)
self._set_headers_etag(mediaFilename,
'image/x-icon',
mimeType,
mediaBinary, None,
refererDomain,
False, None)
@ -7427,8 +7428,11 @@ class PubServer(BaseHTTPRequestHandler):
self.server.debug)
return
if not os.path.isfile(mediaFilename):
self._404()
return
originalMediaFilename = mediaFilename
mediaFilename = originalMediaFilename.replace('.ico', '.png')
if not os.path.isfile(mediaFilename):
self._404()
return
if self._etag_exists(mediaFilename):
# The file has not changed
self._304()
@ -7440,8 +7444,9 @@ class PubServer(BaseHTTPRequestHandler):
except OSError:
print('EX: unable to read cached favicon ' + mediaFilename)
if mediaBinary:
mimeType = mediaFileMimeType(mediaFilename)
self._set_headers_etag(mediaFilename,
'image/x-icon',
mimeType,
mediaBinary, None,
refererDomain,
False, None)

View File

@ -151,7 +151,14 @@ def _downloadNewswireFeedFavicon(session, baseDir: str,
if '://' not in link:
return False
timeoutSec = 10
imageData = getImageBinaryFromUrl(session, favUrl, timeoutSec, debug)
imageData, mimeType = \
getImageBinaryFromUrl(session, favUrl, timeoutSec, debug)
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')
if not imageData:
return False
if not os.path.isdir(baseDir + '/favicons'):

View File

@ -462,7 +462,16 @@ def getImageBinaryFromUrl(session, url: str, timeoutSec: int, debug: bool):
if result.status_code != 200:
print('WARN: getImageFromUrl: ' + url +
' failed with error code ' + str(result.status_code))
return result.content
mimeType = 'image/png'
if 'image/x-icon' in result.headers['content-length']:
mimeType = 'image/x-icon'
elif 'image/webp' in result.headers['content-length']:
mimeType = 'image/webp'
elif 'image/jpeg' in result.headers['content-length']:
mimeType = 'image/jpeg'
elif 'image/gif' in result.headers['content-length']:
mimeType = 'image/gif'
return result.content, mimeType
except requests.exceptions.RequestException as e:
if debug:
print('ERROR: getImageFromUrl failed: ' + str(url) + ', ' +
@ -475,4 +484,4 @@ def getImageBinaryFromUrl(session, url: str, timeoutSec: int, debug: bool):
if e.errno == errno.ECONNRESET:
print('WARN: getImageFromUrl failed, ' +
'connection was reset ' + str(e))
return None
return None, None