fix internal caching and set a header

This commit is contained in:
Yannik Schmidt
2021-07-07 16:31:45 +02:00
parent 4528022a37
commit 9f60d97140

View File

@@ -30,7 +30,7 @@ def generatePicture(pathToOrig, scaleX, scaleY, encoding, crop):
try: try:
image = PIL.Image.open(os.path.join(PICTURE_DIR, pathToOrig)) image = PIL.Image.open(os.path.join(PICTURE_DIR, pathToOrig))
except FileNotFoundError: except FileNotFoundError:
return None return (None, False)
# ensure sizes are valid # # ensure sizes are valid #
x, y = image.size x, y = image.size
@@ -46,6 +46,11 @@ def generatePicture(pathToOrig, scaleX, scaleY, encoding, crop):
newFile = "x-{x}-y-{y}-{fname}.{ext}".format(x=scaleX, y=scaleY, fname=filename, ext=encoding) newFile = "x-{x}-y-{y}-{fname}.{ext}".format(x=scaleX, y=scaleY, fname=filename, ext=encoding)
newPath = os.path.join(CACHE_DIR, newFile) newPath = os.path.join(CACHE_DIR, newFile)
# check for cache
print(newPath)
if os.path.isfile(newPath):
return (newPath, True)
# save image with new size and encoding # # save image with new size and encoding #
if image.mode in ("RGBA", "P") and encoding in ("jpeg", "webp"): if image.mode in ("RGBA", "P") and encoding in ("jpeg", "webp"):
image = image.convert("RGB") image = image.convert("RGB")
@@ -61,7 +66,7 @@ def generatePicture(pathToOrig, scaleX, scaleY, encoding, crop):
# strip the STATIC_DIR because we will use send_from_directory for safety # # strip the STATIC_DIR because we will use send_from_directory for safety #
REPLACE_ONCE = 1 REPLACE_ONCE = 1
return newPath.replace(PICTURE_DIR, "", REPLACE_ONCE) return (newPath.replace(PICTURE_DIR, "", REPLACE_ONCE), False)
@app.route("/media/<path:path>") @app.route("/media/<path:path>")
@app.route("/picture/<path:path>") @app.route("/picture/<path:path>")
@@ -86,18 +91,19 @@ def sendPicture(path):
scaleX = round(float(x1)) scaleX = round(float(x1))
elif x2: elif x2:
scaleX = round(float(x2)) scaleX = round(float(x2))
pathDebug = path pathDebug = path
encoding = flask.request.args.get("encoding") encoding = flask.request.args.get("encoding")
path = generatePicture(path, scaleX, scaleY, encoding, path, cacheHit = generatePicture(path, scaleX, scaleY, encoding,
bool(flask.request.args.get("crop"))) bool(flask.request.args.get("crop")))
if not path: if not path:
return ("File not found: {}".format(os.path.join(PICTURE_DIR, pathDebug)), 404) return ("File not found: {}".format(os.path.join(PICTURE_DIR, pathDebug)), 404)
raw = flask.send_from_directory(".", path, cache_timeout=cache_timeout) raw = flask.send_from_directory(".", path, cache_timeout=cache_timeout)
response = flask.make_response(raw) response = flask.make_response(raw)
response.headers['X-ATHQ-INTERNAL-FID'] = path response.headers['X-PICTURE-FACTORY-INTERNAL-FID'] = path
response.headers['X-PICTURE-FACTORY-INTERNAL-CACHE-HIT'] = cacheHit
# check for a cacheTimeout # # check for a cacheTimeout #
cacheTimeout = flask.request.args.get("cache-timeout") cacheTimeout = flask.request.args.get("cache-timeout")