Skip to content

Commit

Permalink
[files] Add default cache-control header to every static files + allo…
Browse files Browse the repository at this point in the history
…w to set the value of max_age in an environment variable
  • Loading branch information
EvanBldy committed Sep 20, 2023
1 parent abf9935 commit c8d8b36
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 2 deletions.
2 changes: 2 additions & 0 deletions zou/app/blueprints/comments/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
tasks_service,
user_service,
)
from zou.app import config


class DownloadAttachmentResource(Resource):
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion zou/app/blueprints/files/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion zou/app/blueprints/previews/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions zou/app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit c8d8b36

Please sign in to comment.