Name cache files by absolute path hash

pull/5/head
mandlm 2018-11-10 15:18:43 +01:00
parent f103fd05fc
commit 8a79fda820
1 changed files with 11 additions and 5 deletions

View File

@ -5,6 +5,7 @@ from random import choice
from pathlib import Path from pathlib import Path
from PIL import Image from PIL import Image
from time import time from time import time
from hashlib import sha256
import config import config
@ -39,14 +40,19 @@ def image(filename):
cache_dir = Path(config.cachedir) / ("%sx%s" % cache_resolution) cache_dir = Path(config.cachedir) / ("%sx%s" % cache_resolution)
if not cache_dir.exists(): if not cache_dir.exists():
print("Creating cache dir", cache_dir)
cache_dir.mkdir(parents=True) cache_dir.mkdir(parents=True)
if not (cache_dir / filename).exists(): original_file_path = Path(config.imgdir) / filename
img = Image.open(Path(config.imgdir) / filename) cache_file = sha256(str(original_file_path.resolve()).encode("utf-8")).hexdigest()
img.thumbnail(cache_resolution)
img.save(cache_dir / filename)
return send_from_directory(cache_dir, filename) if not (cache_dir / cache_file).exists():
print("Creating cache file", filename)
img = Image.open(original_file_path)
img.thumbnail(cache_resolution)
img.save(cache_dir / cache_file, "JPEG")
return send_from_directory(cache_dir, cache_file)
if __name__ == "__main__": if __name__ == "__main__":