diff --git a/scraper/server/__main__.py b/scraper/server/__main__.py index 75a689c..ce7152a 100644 --- a/scraper/server/__main__.py +++ b/scraper/server/__main__.py @@ -1,5 +1,5 @@ from scraper.config import config -from flask import Flask, Response, request, render_template, url_for +from flask import Flask, Response, request, render_template, url_for, send_file from pathlib import Path import py34 from .block import BLOCK_SIZE, list_blocks, load_blocks, load_block, load_block_stats, save_block, loads as parse_block, enttype2ext @@ -112,12 +112,21 @@ def get_image(post_id: int = None): assert post_id is not None post_id = int(post_id) entry = load_block(post_id).entry(post_id) - return Response( - status = 307, - headers = { - "Location": str(py34.url.ImageURL(entry.dir, entry.image, enttype2ext(entry.type))) - } - ) + if entry.type < 100: + return Response( + "Image not found", + status = 404, + ) + + path = Path.cwd() / Path(f"image/{entry.dir}/{entry.image}.{enttype2ext(entry.type)}") + path.parent.mkdir(parents=True, exist_ok=True) + if path.exists(): + return send_file(path) + else: + post = py34.Post(entry.post, entry.dir, entry.image, entry.tags, entry.thumbnail) + with open(path, "wb") as file: + file.write(post.get_image_data()) + return send_file(path) # Run application