From 0e2f8a55ce872f24b2439a16410b5956874b3594 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Sun, 15 Sep 2024 21:41:06 +0200 Subject: [PATCH] View to list all images in collection (#3) * Cache generation instead of views This way, query params are kept cached. * Add view to list all images --- templates/list.html | 47 +++++++++++++++++++++++++++++++++++++++++++++ wsgi.py | 26 +++++++++++++++---------- 2 files changed, 63 insertions(+), 10 deletions(-) create mode 100644 templates/list.html diff --git a/templates/list.html b/templates/list.html new file mode 100644 index 0000000..e6d1267 --- /dev/null +++ b/templates/list.html @@ -0,0 +1,47 @@ + + + + + + placeany + + + + +
+

placeany

+

The following images are used on this placeany instance. + You may request a specific image by adding ?image=n to your request + (where n is the number of the image you want), or filter the options + by adding multiple values like this: ?image=n1x&image=n2&image=n3.

+
+ {% for n in range(count) %} +
+ +
+ {% endfor %} +
+
+
+ + + + \ No newline at end of file diff --git a/wsgi.py b/wsgi.py index fe6ef40..e836574 100644 --- a/wsgi.py +++ b/wsgi.py @@ -10,6 +10,7 @@ app = Flask(__name__) GREY = "G" COLOR = "RGB" +IMAGE_DIR = "./images" cache = Cache(config={"CACHE_TYPE": "SimpleCache"}) @@ -17,16 +18,17 @@ app = Flask(__name__) cache.init_app(app) -def get_cropped_image(x, y, grey=False, retries=0): +@cache.memoize(1) +def get_cropped_image(x, y, s, grey=False, retries=0): """crops a random image from collection""" if retries > 10: return None - options = os.listdir("./images") + options = os.listdir(IMAGE_DIR) try: selection = list( filter( lambda i: i in range(0, len(options)), - map(int, request.args.getlist("image")), + map(int, s), ) ) except ValueError: @@ -38,7 +40,7 @@ def get_cropped_image(x, y, grey=False, retries=0): im_src = options[selection[0]] case _: im_src = options[random.choice(selection)] - im = Image.open(f"images/{im_src}") + im = Image.open(f"{IMAGE_DIR}/{im_src}") out = BytesIO() max_x, max_y = im.size if x > max_x and y > max_y: @@ -51,8 +53,8 @@ def get_cropped_image(x, y, grey=False, retries=0): return out -def make_response(x, y, color_mode=COLOR): - im = get_cropped_image(x, y, color_mode == GREY) +def make_response(x, y, s, color_mode=COLOR): + im = get_cropped_image(x, y, s, color_mode == GREY) if not im: return Response(status=401) return send_file(im, mimetype="image/webp") @@ -70,16 +72,20 @@ def bookmarklet(): return render_template("bookmarklet.html", url=u) +@app.route("/images") +def collection(): + c = len(os.listdir(IMAGE_DIR)) + return render_template("list.html", count=c) + + @app.route("//") -@cache.cached(1) def generate(x, y): - return make_response(x, y, COLOR) + return make_response(x, y, request.args.getlist("image"), COLOR) @app.route("/g//") -@cache.cached(1) def generate_grey(x, y): - return make_response(x, y, GREY) + return make_response(x, y, request.args.getlist("image"), GREY) if __name__ == "__main__":