mirror of
https://github.com/FAUSheppy/python-flask-picture-factory
synced 2025-12-06 07:01:37 +01:00
fix internal caching and set a header
This commit is contained in:
14
server.py
14
server.py
@@ -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>")
|
||||||
@@ -89,7 +94,7 @@ def sendPicture(path):
|
|||||||
|
|
||||||
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)
|
||||||
@@ -97,7 +102,8 @@ def sendPicture(path):
|
|||||||
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")
|
||||||
|
|||||||
Reference in New Issue
Block a user