55 lines
1.5 KiB
Python
55 lines
1.5 KiB
Python
# retoor <retoor@molodetz.nl>
|
|
|
|
from pathlib import Path
|
|
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
INLINE_MEDIA_EXTENSIONS = {
|
|
".jpg",
|
|
".jpeg",
|
|
".png",
|
|
".gif",
|
|
".webp",
|
|
".bmp",
|
|
".tiff",
|
|
".mp4",
|
|
".webm",
|
|
".ogv",
|
|
".mov",
|
|
".m4v",
|
|
".mp3",
|
|
}
|
|
|
|
|
|
class UploadStaticFiles(StaticFiles):
|
|
async def get_response(self, path, scope):
|
|
response = await super().get_response(path, scope)
|
|
disposition = (
|
|
"inline"
|
|
if Path(path).suffix.lower() in INLINE_MEDIA_EXTENSIONS
|
|
else "attachment"
|
|
)
|
|
response.headers["Content-Disposition"] = disposition
|
|
response.headers["Cache-Control"] = "public, max-age=604800"
|
|
return response
|
|
|
|
|
|
class CachedStaticFiles(StaticFiles):
|
|
async def get_response(self, path, scope):
|
|
response = await super().get_response(path, scope)
|
|
if Path(path).name == "service-worker.js":
|
|
response.headers["Cache-Control"] = "no-cache"
|
|
else:
|
|
response.headers["Cache-Control"] = "public, max-age=31536000, immutable"
|
|
return response
|
|
|
|
|
|
class FallbackStaticFiles(StaticFiles):
|
|
async def get_response(self, path, scope):
|
|
response = await super().get_response(path, scope)
|
|
if Path(path).name == "service-worker.js":
|
|
response.headers["Cache-Control"] = "no-cache"
|
|
else:
|
|
response.headers["Cache-Control"] = "public, max-age=3600"
|
|
return response
|