Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overwrite FileResponse Content-Type with application/octet-stream for all files in /store #1064

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions asu/fastapi/staticfiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os

from fastapi.responses import FileResponse, Response
from fastapi.staticfiles import StaticFiles as FastApiStaticFiles

from starlette.staticfiles import PathLike
from starlette.types import Scope


class StaticFiles(FastApiStaticFiles):
def __init__(
self,
*,
directory: PathLike | None = None,
packages: list[str | tuple[str, str]] | None = None,
html: bool = False,
check_dir: bool = True,
follow_symlink: bool = False,
) -> None:
super().__init__(
directory=directory,
packages=packages,
html=html,
check_dir=check_dir,
follow_symlink=follow_symlink,
)

def file_response(
self,
full_path: PathLike,
stat_result: os.stat_result,
scope: Scope,
status_code: int = 200,
) -> Response:
response = super().file_response(full_path, stat_result, scope, status_code)
if isinstance(response, FileResponse):
response.headers["Content-Type"] = "application/octet-stream"
return response
5 changes: 4 additions & 1 deletion asu/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from asu.config import settings
from asu.routers import api
from asu.util import get_redis_client, parse_feeds_conf, parse_packages_file
from asu.fastapi.staticfiles import StaticFiles as AsuStaticFiles

logging.basicConfig(encoding="utf-8", level=settings.log_level)

Expand All @@ -36,7 +37,9 @@ async def lifespan(app: FastAPI):
(settings.public_path / "json").mkdir(parents=True, exist_ok=True)
(settings.public_path / "store").mkdir(parents=True, exist_ok=True)

app.mount("/store", StaticFiles(directory=settings.public_path / "store"), name="store")
app.mount(
"/store", AsuStaticFiles(directory=settings.public_path / "store"), name="store"
)
app.mount("/static", StaticFiles(directory=base_path / "static"), name="static")

templates = Jinja2Templates(directory=base_path / "templates")
Expand Down
36 changes: 36 additions & 0 deletions tests/test_store.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from asu.config import settings
from asu.fastapi.staticfiles import FileResponse

store_path = settings.public_path / "store"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ugly.

But conftest.py overwrites settings.public_path, unfortunately the main.py/app has already been constructed, with the previous old public_path.
Thus the conftest.py app()-setting of public_path to a temp-path doesn't work here.


def test_store_content_type_img(client):
store_path.mkdir(parents=True, exist_ok=True)
with open(store_path / "test_store_content_type.img", "w+b"):
pass
response = client.head("/store/test_store_content_type.img")

assert response.status_code == 200

headers = response.headers
assert headers["Content-Type"] == "application/octet-stream"


def test_store_content_type_imggz(client):
store_path.mkdir(parents=True, exist_ok=True)
with open(store_path / "test_store_content_type.img.gz", "w+b"):
pass
response = client.head("/store/test_store_content_type.img.gz")

assert response.status_code == 200

headers = response.headers
assert headers["Content-Type"] == "application/octet-stream"


def test_store_file_missing(client):
response = client.head("/store/test_store_file_missing.bin")

assert response.status_code == 404

headers = response.headers
assert headers["Content-Type"] != "application/octet-stream"