Again fixed caching, also documentation

This commit is contained in:
Ozbolt Menegatti 2019-09-18 12:23:42 +00:00
parent 44915ea014
commit 52cc584c44

View File

@ -68,14 +68,22 @@ def plugin_file(plugin, path):
return "File not found", 404 return "File not found", 404
mt, _encoding = mimetypes.guess_type(path) mt, _encoding = mimetypes.guess_type(path)
# if in cache, load etag and content, set status code to 304
if check_cache(full_path): if check_cache(full_path):
result = redis.get(full_path + ":content") result = redis.get(full_path + ":content")
etag = str(redis.get(full_path + ":etag"))
status_code = 304 status_code = 304
# else, make search-and-replace and save to redis (+etag)
else: else:
replace_with = "{}/{}".format(URL, plugin) replace_with = "{}/{}".format(URL, plugin)
status_code = 200 status_code = 200
etag = generate_etag()
with open(full_path, 'rb') as fp: with open(full_path, 'rb') as fp:
content_bytes = fp.read() content_bytes = fp.read()
try: try:
content = content_bytes.decode('UTF-8') content = content_bytes.decode('UTF-8')
result = content.replace(REPLACE_STRING, replace_with) result = content.replace(REPLACE_STRING, replace_with)
@ -84,15 +92,21 @@ def plugin_file(plugin, path):
result = content_bytes result = content_bytes
redis.set(full_path + ":content", result) redis.set(full_path + ":content", result)
redis.set(full_path + ":etag", etag)
resp = Response(result, mimetype=mt) resp = Response(result, mimetype=mt)
resp.headers.add('Access-Control-Allow-Origin', '*') resp.headers.add('Access-Control-Allow-Origin', '*')
resp.headers.add('ETag', generate_etag()) resp.headers.add('ETag', etag)
resp.status_code = status_code
# return everything if cached response is not allowed # if browser does not have a cached etag -> 200
if 'If-None-Match' not in request.headers: if 'If-None-Match' not in request.headers:
resp.status_code = 200 resp.status_code = 200
# then check if it is the latest etag, else -> 200
elif request.headers['If-None-Match'] != etag:
resp.status_code = 200
# else just set whatever was predetermined
else:
resp.status_code = status_code
return resp return resp