Move icon display to its own method

main
Bob Mottram 2020-08-31 19:15:53 +01:00
parent eb40fc63c2
commit 4dbe934d3b
1 changed files with 39 additions and 31 deletions

View File

@ -3893,6 +3893,42 @@ class PubServer(BaseHTTPRequestHandler):
return return
self._404() self._404()
def _showIcon(self, callingDomain: str, path: str,
baseDir: str,
GETstartTime, GETtimings: {}):
"""Shows an icon
"""
if path.endswith('.png'):
mediaStr = path.split('/icons/')[1]
mediaFilename = baseDir + '/img/icons/' + mediaStr
if self._etag_exists(mediaFilename):
# The file has not changed
self._304()
return
if self.server.iconsCache.get(mediaStr):
mediaBinary = self.server.iconsCache[mediaStr]
self._set_headers_etag(mediaFilename,
'image/png',
mediaBinary, None,
callingDomain)
self._write(mediaBinary)
return
else:
if os.path.isfile(mediaFilename):
with open(mediaFilename, 'rb') as avFile:
mediaBinary = avFile.read()
self._set_headers_etag(mediaFilename,
'image/png',
mediaBinary, None,
callingDomain)
self._write(mediaBinary)
self.server.iconsCache[mediaStr] = mediaBinary
self._benchmarkGETtimings(GETstartTime, GETtimings,
'show files done',
'icon shown')
return
self._404()
def do_GET(self): def do_GET(self):
callingDomain = self.server.domainFull callingDomain = self.server.domainFull
if self.headers.get('Host'): if self.headers.get('Host'):
@ -4782,37 +4818,9 @@ class PubServer(BaseHTTPRequestHandler):
# icon images # icon images
# Note that this comes before the busy flag to avoid conflicts # Note that this comes before the busy flag to avoid conflicts
if self.path.startswith('/icons/'): if self.path.startswith('/icons/'):
if self.path.endswith('.png'): self._showIcon(callingDomain, self.path,
mediaStr = self.path.split('/icons/')[1] self.server.baseDir,
mediaFilename = \ GETstartTime, GETtimings)
self.server.baseDir + '/img/icons/' + mediaStr
if self._etag_exists(mediaFilename):
# The file has not changed
self._304()
return
if self.server.iconsCache.get(mediaStr):
mediaBinary = self.server.iconsCache[mediaStr]
self._set_headers_etag(mediaFilename,
'image/png',
mediaBinary, cookie,
callingDomain)
self._write(mediaBinary)
return
else:
if os.path.isfile(mediaFilename):
with open(mediaFilename, 'rb') as avFile:
mediaBinary = avFile.read()
self._set_headers_etag(mediaFilename,
'image/png',
mediaBinary, cookie,
callingDomain)
self._write(mediaBinary)
self.server.iconsCache[mediaStr] = mediaBinary
self._benchmarkGETtimings(GETstartTime, GETtimings,
'show files done',
'icon shown')
return
self._404()
return return
self._benchmarkGETtimings(GETstartTime, GETtimings, self._benchmarkGETtimings(GETstartTime, GETtimings,