2013-11-26 08:30:19 +00:00
|
|
|
"""Apache's specific responses."""
|
2024-08-05 08:51:17 +00:00
|
|
|
|
2013-11-26 08:30:19 +00:00
|
|
|
import os.path
|
|
|
|
|
|
2020-01-07 14:10:42 +00:00
|
|
|
from django_downloadview.response import ProxiedDownloadResponse, content_disposition
|
2013-11-26 08:30:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
):
|
2013-11-26 08:30:19 +00:00
|
|
|
"""Return a HttpResponse with headers for Apache X-Sendfile."""
|
2024-07-31 15:12:03 +00:00
|
|
|
# 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)
|
2024-07-31 15:12:03 +00:00
|
|
|
super().__init__(content_type=content_type, headers=headers)
|
2013-11-26 08:30:19 +00:00
|
|
|
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
|