From c8d8b36b8e7b20a710ca70f5e5d711ba1ff30f6d Mon Sep 17 00:00:00 2001 From: Evan Blaudy Date: Wed, 20 Sep 2023 14:24:29 +0200 Subject: [PATCH] [files] Add default cache-control header to every static files + allow to set the value of max_age in an environment variable --- zou/app/blueprints/comments/resources.py | 2 ++ zou/app/blueprints/files/resources.py | 5 ++++- zou/app/blueprints/previews/resources.py | 2 +- zou/app/config.py | 2 ++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/zou/app/blueprints/comments/resources.py b/zou/app/blueprints/comments/resources.py index 03ddb8aac8..2a1dcf66b4 100644 --- a/zou/app/blueprints/comments/resources.py +++ b/zou/app/blueprints/comments/resources.py @@ -14,6 +14,7 @@ tasks_service, user_service, ) +from zou.app import config class DownloadAttachmentResource(Resource): @@ -64,6 +65,7 @@ def get(self, attachment_file_id, file_name): mimetype=attachment_file["mimetype"], as_attachment=False, download_name=attachment_file["name"], + max_age=config.CLIENT_CACHE_MAX_AGE, ) except Exception: abort(404) diff --git a/zou/app/blueprints/files/resources.py b/zou/app/blueprints/files/resources.py index 22cb359b22..ad057827e1 100644 --- a/zou/app/blueprints/files/resources.py +++ b/zou/app/blueprints/files/resources.py @@ -32,7 +32,9 @@ ) -def send_storage_file(working_file_id, as_attachment=False): +def send_storage_file( + working_file_id, as_attachment=False, max_age=config.CLIENT_CACHE_MAX_AGE +): """ Send file from storage. If it's not a local storage, cache the file in a temporary folder before sending it. It accepts conditional headers. @@ -58,6 +60,7 @@ def send_storage_file(working_file_id, as_attachment=False): mimetype=mimetype, as_attachment=as_attachment, download_name=download_name, + max_age=max_age, ) except IOError as e: current_app.logger.error(e) diff --git a/zou/app/blueprints/previews/resources.py b/zou/app/blueprints/previews/resources.py index b104b474f9..5aef00aecd 100644 --- a/zou/app/blueprints/previews/resources.py +++ b/zou/app/blueprints/previews/resources.py @@ -127,7 +127,7 @@ def send_storage_file( extension, mimetype="application/octet-stream", as_attachment=False, - max_age=60 * 60 * 24 * 7 + max_age=config.CLIENT_CACHE_MAX_AGE, ): """ Send file from storage. If it's not a local storage, cache the file in diff --git a/zou/app/config.py b/zou/app/config.py index e2ed3b31c3..d818dea4ef 100644 --- a/zou/app/config.py +++ b/zou/app/config.py @@ -36,6 +36,8 @@ JWT_COOKIE_SAMESITE = "Lax" JWT_IDENTITY_CLAIM = "sub" +CLIENT_CACHE_MAX_AGE = int(os.getenv("CLIENT_CACHE_MAX_AGE", 604800)) + DATABASE = { "drivername": os.getenv("DB_DRIVER", "postgresql+psycopg"), "host": os.getenv("DB_HOST", "localhost"),