2019-10-23 18:23:25 +02:00
|
|
|
# Useful reading is:
|
|
|
|
# https://zulip.readthedocs.io/en/latest/subsystems/html-css.html#front-end-build-process
|
2013-10-10 21:37:26 +02:00
|
|
|
|
2016-07-04 13:33:16 +02:00
|
|
|
import os
|
2019-07-03 02:18:44 +02:00
|
|
|
from typing import Optional
|
2016-07-04 13:33:16 +02:00
|
|
|
|
2013-06-12 00:01:13 +02:00
|
|
|
from django.conf import settings
|
2017-02-06 22:12:03 +01:00
|
|
|
from django.contrib.staticfiles.storage import ManifestStaticFilesStorage
|
2022-07-19 16:13:22 +02:00
|
|
|
from django.core.files.base import File
|
2022-07-27 00:41:04 +02:00
|
|
|
from django.core.files.storage import FileSystemStorage
|
2023-10-12 19:43:45 +02:00
|
|
|
from typing_extensions import override
|
2013-06-12 00:01:13 +02:00
|
|
|
|
2019-07-18 11:36:56 +02:00
|
|
|
if settings.DEBUG:
|
2019-07-17 02:29:08 +02:00
|
|
|
from django.contrib.staticfiles.finders import find
|
|
|
|
|
|
|
|
def static_path(path: str) -> str:
|
|
|
|
return find(path) or "/nonexistent"
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-07-17 02:29:08 +02:00
|
|
|
else:
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-07-17 02:29:08 +02:00
|
|
|
def static_path(path: str) -> str:
|
|
|
|
return os.path.join(settings.STATIC_ROOT, path)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-06-02 01:59:50 +02:00
|
|
|
class IgnoreBundlesManifestStaticFilesStorage(ManifestStaticFilesStorage):
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2021-02-12 08:19:30 +01:00
|
|
|
def hashed_name(
|
2022-07-19 16:13:22 +02:00
|
|
|
self, name: str, content: Optional["File[bytes]"] = None, filename: Optional[str] = None
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> str:
|
2018-07-24 18:17:05 +02:00
|
|
|
ext = os.path.splitext(name)[1]
|
2019-07-03 02:22:28 +02:00
|
|
|
if name.startswith("webpack-bundles"):
|
2018-06-02 01:59:50 +02:00
|
|
|
# Hack to avoid renaming already-hashnamed webpack bundles
|
|
|
|
# when minifying; this was causing every bundle to have
|
|
|
|
# two hashes appended to its name, one by webpack and one
|
|
|
|
# here. We can't just skip processing of these bundles,
|
|
|
|
# since we do need the Django storage to add these to the
|
|
|
|
# manifest for django_webpack_loader to work. So, we just
|
|
|
|
# use a no-op hash function for these already-hashed
|
|
|
|
# assets.
|
2018-07-24 18:17:05 +02:00
|
|
|
return name
|
2023-02-07 21:14:52 +01:00
|
|
|
if name == "generated/emoji/emoji_api.json":
|
|
|
|
# Unlike most .json files, we do want to hash this file;
|
|
|
|
# its hashed URL is returned as part of the API. See
|
|
|
|
# data_url() in zerver/lib/emoji.py.
|
|
|
|
return super().hashed_name(name, content, filename)
|
2021-02-12 08:20:45 +01:00
|
|
|
if ext in [".png", ".gif", ".jpg", ".svg"]:
|
2018-07-24 18:17:05 +02:00
|
|
|
# Similarly, don't hash-rename image files; we only serve
|
|
|
|
# the original file paths (not the hashed file paths), and
|
|
|
|
# so the only effect of hash-renaming these is to increase
|
2022-02-08 00:13:33 +01:00
|
|
|
# the size of release tarballs with duplicate copies of these.
|
2018-07-24 18:17:05 +02:00
|
|
|
#
|
|
|
|
# One could imagine a future world in which we instead
|
|
|
|
# used the hashed paths for these; in that case, though,
|
|
|
|
# we should instead be removing the non-hashed paths.
|
|
|
|
return name
|
2023-02-07 23:20:29 +01:00
|
|
|
if ext in [".json", ".po", ".mo", ".mp3", ".ogg", ".html", ".md"]:
|
2018-07-24 18:17:05 +02:00
|
|
|
# And same story for translation files, sound files, etc.
|
2018-06-02 01:59:50 +02:00
|
|
|
return name
|
|
|
|
return super().hashed_name(name, content, filename)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-07-18 11:36:56 +02:00
|
|
|
class ZulipStorage(IgnoreBundlesManifestStaticFilesStorage):
|
2017-02-06 22:12:03 +01:00
|
|
|
# This is a hack to use staticfiles.json from within the
|
|
|
|
# deployment, rather than a directory under STATIC_ROOT. By doing
|
|
|
|
# so, we can use a different copy of staticfiles.json for each
|
|
|
|
# deployment, which ensures that we always use the correct static
|
|
|
|
# assets for each deployment.
|
2022-07-27 00:41:04 +02:00
|
|
|
def __init__(self) -> None:
|
2023-02-23 01:17:24 +01:00
|
|
|
super().__init__(manifest_storage=FileSystemStorage(location=settings.DEPLOY_ROOT))
|