]> git.scottworley.com Git - paperdoorknob/blobdiff - images.py
Handle Unicode characters ≈ and ◁
[paperdoorknob] / images.py
index 178f7085f5a10a4feeae780b04707aacc92b07ae..7932091668d20a47c53c71e5ebcc49267818ee24 100644 (file)
--- a/images.py
+++ b/images.py
@@ -4,17 +4,25 @@
 # under the terms of the GNU General Public License as published by the
 # Free Software Foundation, version 3.
 
+from abc import ABC, abstractmethod
 import os
 import os.path
 
 from fetch import Fetcher
 
 
+class ImageStore(ABC):
+
+    @abstractmethod
+    def get_image(self, url: str) -> str:
+        raise NotImplementedError()
+
+
 class ImageStoreError(Exception):
     pass
 
 
-class ImageStore:
+class DiskImageStore(ImageStore):
 
     def __init__(self, root_path: str, fetcher: Fetcher) -> None:
         self._root_path = root_path
@@ -24,7 +32,7 @@ class ImageStore:
 
     def _filename(self, url: str) -> str:
         assert url not in self._images
-        base = os.path.basename(url)
+        base = os.path.basename(url).replace('%', '').split('?')[0]
         if base not in self._filenames:
             return base
         stem, ext = os.path.splitext(base)
@@ -46,3 +54,9 @@ class ImageStore:
             self._filenames.add(name)
             self._images[url] = path
         return self._images[url]
+
+
+class FakeImageStore(ImageStore):
+
+    def get_image(self, url: str) -> str:
+        return 'stored:' + url