Merge branch 'feature/cache-by-absoulte-path' into develop

This commit is contained in:
mandlm 2018-11-10 16:36:29 +01:00
commit f51f324a30
3 changed files with 8 additions and 7 deletions

View File

@ -1,3 +1,4 @@
imgdir = "." imgdir = "."
refresh = 5 refresh = 5
cachedir = ".webslider" cachedir = "/tmp/webslider"
resolution = (1920, 1080)

View File

@ -1,2 +1,3 @@
Flask==1.0.2 Flask==1.0.2
Pillow==5.3.0 Pillow==5.3.0
Click==7.0

View File

@ -13,7 +13,8 @@ import config
app = Flask(__name__) app = Flask(__name__)
cache_resolution = (1920, 1080) imgdir = Path(config.imgdir).expanduser().resolve()
cache_resolution = config.resolution
cache_dir = Path(config.cachedir) / ("%sx%s" % cache_resolution) cache_dir = Path(config.cachedir) / ("%sx%s" % cache_resolution)
@ -25,7 +26,6 @@ def random():
@app.route("/random_image/") @app.route("/random_image/")
def random_image(): def random_image():
img_glob = "*jpg" img_glob = "*jpg"
imgdir = Path(config.imgdir)
last_modified_time, last_modified_file = max( last_modified_time, last_modified_file = max(
(f.stat().st_mtime, f) for f in imgdir.glob(img_glob) (f.stat().st_mtime, f) for f in imgdir.glob(img_glob)
) )
@ -36,7 +36,7 @@ def random_image():
images = list(imgdir.glob(img_glob)) images = list(imgdir.glob(img_glob))
selected_image = choice(images).relative_to(imgdir) selected_image = choice(images).relative_to(imgdir)
return redirect(url_for("image", filename=selected_image)) return redirect(url_for("image", filename=selected_image) + "?hash=%s" % get_cache_filename(selected_image))
@app.route("/img/<path:filename>") @app.route("/img/<path:filename>")
@ -64,7 +64,7 @@ def create_cache_file(filename):
if not (cache_dir / cache_file).exists(): if not (cache_dir / cache_file).exists():
print("Creating cache file", filename) print("Creating cache file", filename)
img = Image.open(Path(config.imgdir) / filename) img = Image.open(imgdir / filename)
img.thumbnail(cache_resolution) img.thumbnail(cache_resolution)
img.save(cache_dir / cache_file, "JPEG") img.save(cache_dir / cache_file, "JPEG")
@ -72,12 +72,11 @@ def create_cache_file(filename):
def get_cache_filename(filename): def get_cache_filename(filename):
original_file_path = Path(config.imgdir) / filename original_file_path = imgdir / filename
return sha256(str(original_file_path.resolve()).encode("utf-8")).hexdigest() return sha256(str(original_file_path.resolve()).encode("utf-8")).hexdigest()
def pre_cache_images(): def pre_cache_images():
imgdir = Path(config.imgdir)
for image_file in sorted(imgdir.glob("*.jpg")): for image_file in sorted(imgdir.glob("*.jpg")):
create_cache_file(image_file.relative_to(imgdir)) create_cache_file(image_file.relative_to(imgdir))