Fix a bug with locally serving images, fix issue #118

This commit is contained in:
Lucas Verney 2018-02-23 16:57:00 +01:00
parent 4ff4510ab7
commit 0b89f27a43

View File

@ -104,22 +104,24 @@ class ImageCache(MemoryCache):
if len(self.map.keys()) > self.max_items: if len(self.map.keys()) > self.max_items:
self.map.popitem(last=False) self.map.popitem(last=False)
filepath = os.path.join( # Try to load from local folder
self.storage_dir, if self.storage_dir:
self.compute_filename(url) filepath = os.path.join(
) self.storage_dir,
if os.path.isfile(filepath): self.compute_filename(url)
image = PIL.Image.open(filepath) )
else: if os.path.isfile(filepath):
req = requests.get(url) return PIL.Image.open(filepath)
try: # Otherwise, fetch it
req.raise_for_status() req = requests.get(url)
image = PIL.Image.open(BytesIO(req.content)) try:
if self.storage_dir: req.raise_for_status()
image.save(filepath, format=image.format) image = PIL.Image.open(BytesIO(req.content))
except (requests.HTTPError, IOError): if self.storage_dir:
return None image.save(filepath, format=image.format)
return image return image
except (requests.HTTPError, IOError):
return None
def __init__(self, max_items=200, storage_dir=None): def __init__(self, max_items=200, storage_dir=None):
""" """