mirror of https://gitlab.com/bashrc2/epicyon
Detect favicon mime type
parent
791bbcf423
commit
6da17599ca
13
daemon.py
13
daemon.py
|
@ -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)
|
||||
|
|
|
@ -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'):
|
||||
|
|
13
session.py
13
session.py
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue