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)
|
print('showCachedFavicon: ' + mediaFilename)
|
||||||
if self.server.faviconsCache.get(favFile):
|
if self.server.faviconsCache.get(favFile):
|
||||||
mediaBinary = self.server.faviconsCache[favFile]
|
mediaBinary = self.server.faviconsCache[favFile]
|
||||||
|
mimeType = mediaFileMimeType(mediaFilename)
|
||||||
self._set_headers_etag(mediaFilename,
|
self._set_headers_etag(mediaFilename,
|
||||||
'image/x-icon',
|
mimeType,
|
||||||
mediaBinary, None,
|
mediaBinary, None,
|
||||||
refererDomain,
|
refererDomain,
|
||||||
False, None)
|
False, None)
|
||||||
|
@ -7427,8 +7428,11 @@ class PubServer(BaseHTTPRequestHandler):
|
||||||
self.server.debug)
|
self.server.debug)
|
||||||
return
|
return
|
||||||
if not os.path.isfile(mediaFilename):
|
if not os.path.isfile(mediaFilename):
|
||||||
self._404()
|
originalMediaFilename = mediaFilename
|
||||||
return
|
mediaFilename = originalMediaFilename.replace('.ico', '.png')
|
||||||
|
if not os.path.isfile(mediaFilename):
|
||||||
|
self._404()
|
||||||
|
return
|
||||||
if self._etag_exists(mediaFilename):
|
if self._etag_exists(mediaFilename):
|
||||||
# The file has not changed
|
# The file has not changed
|
||||||
self._304()
|
self._304()
|
||||||
|
@ -7440,8 +7444,9 @@ class PubServer(BaseHTTPRequestHandler):
|
||||||
except OSError:
|
except OSError:
|
||||||
print('EX: unable to read cached favicon ' + mediaFilename)
|
print('EX: unable to read cached favicon ' + mediaFilename)
|
||||||
if mediaBinary:
|
if mediaBinary:
|
||||||
|
mimeType = mediaFileMimeType(mediaFilename)
|
||||||
self._set_headers_etag(mediaFilename,
|
self._set_headers_etag(mediaFilename,
|
||||||
'image/x-icon',
|
mimeType,
|
||||||
mediaBinary, None,
|
mediaBinary, None,
|
||||||
refererDomain,
|
refererDomain,
|
||||||
False, None)
|
False, None)
|
||||||
|
|
|
@ -151,7 +151,14 @@ def _downloadNewswireFeedFavicon(session, baseDir: str,
|
||||||
if '://' not in link:
|
if '://' not in link:
|
||||||
return False
|
return False
|
||||||
timeoutSec = 10
|
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:
|
if not imageData:
|
||||||
return False
|
return False
|
||||||
if not os.path.isdir(baseDir + '/favicons'):
|
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:
|
if result.status_code != 200:
|
||||||
print('WARN: getImageFromUrl: ' + url +
|
print('WARN: getImageFromUrl: ' + url +
|
||||||
' failed with error code ' + str(result.status_code))
|
' 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:
|
except requests.exceptions.RequestException as e:
|
||||||
if debug:
|
if debug:
|
||||||
print('ERROR: getImageFromUrl failed: ' + str(url) + ', ' +
|
print('ERROR: getImageFromUrl failed: ' + str(url) + ', ' +
|
||||||
|
@ -475,4 +484,4 @@ def getImageBinaryFromUrl(session, url: str, timeoutSec: int, debug: bool):
|
||||||
if e.errno == errno.ECONNRESET:
|
if e.errno == errno.ECONNRESET:
|
||||||
print('WARN: getImageFromUrl failed, ' +
|
print('WARN: getImageFromUrl failed, ' +
|
||||||
'connection was reset ' + str(e))
|
'connection was reset ' + str(e))
|
||||||
return None
|
return None, None
|
||||||
|
|
Loading…
Reference in New Issue