django-downloadview/django_downloadview/apache/response.py

23 lines
870 B
Python
Raw Normal View History

"""Apache's specific responses."""
2024-08-05 08:51:17 +00:00
import os.path
2020-01-07 14:10:42 +00:00
from django_downloadview.response import ProxiedDownloadResponse, content_disposition
class XSendfileResponse(ProxiedDownloadResponse):
"Delegates serving file to Apache via X-Sendfile header."
2020-01-07 14:10:42 +00:00
2024-08-05 08:51:17 +00:00
def __init__(
self, file_path, content_type, basename=None, attachment=True, headers=None
):
"""Return a HttpResponse with headers for Apache X-Sendfile."""
# content-type must be provided only as keyword argument to response
if headers and content_type:
2024-08-05 08:51:17 +00:00
headers.pop("Content-Type", None)
super().__init__(content_type=content_type, headers=headers)
if attachment:
self.basename = basename or os.path.basename(file_path)
2020-01-07 14:10:42 +00:00
self["Content-Disposition"] = content_disposition(self.basename)
self["X-Sendfile"] = file_path