meringue.protected.views ¤
get_content_disposition ¤
get_signed_url ¤
Returns a temporary signed URL of a file in a django-storages S3 storage.
The URL makes S3 return the file with the same Content-Disposition and Content-Type as
the view does.
Attributes:
-
file–Protected file.
-
disp–Content disposition (
inlineorattachment).
Returns:
-
str | None–Signed URL or
Noneif the storage is not S3 or does not sign URLs -
str | None–(
querystring_auth = False: the URL would be public and permanent).
Source code in meringue/protected/views.py
def get_signed_url(file: FieldFile, disp: str) -> str | None:
"""
Returns a temporary signed URL of a file in a django-storages S3 storage.
The URL makes S3 return the file with the same `Content-Disposition` and `Content-Type` as
the view does.
Attributes:
file: Protected file.
disp: Content disposition (`inline` or `attachment`).
Returns:
Signed URL or `None` if the storage is not S3 or does not sign URLs
(`querystring_auth = False`: the URL would be public and permanent).
"""
storage = file.storage
if not is_s3_storage_class(type(storage)) or not getattr(storage, "querystring_auth", False):
return None
file_name = PurePosixPath(file.name).name
parameters = {"ResponseContentDisposition": get_content_disposition(file_name, disp)}
content_type = mimetypes.guess_type(file_name)[0]
if content_type:
parameters["ResponseContentType"] = content_type
return storage.url(file.name, parameters=parameters)
x_accel_redirect_view ¤
The view checks the user's access to view and serves the file.
If you enable the parameter PROTECTED_SERVE_WITH_NGINX, the file will not be served; instead, the response will contain the X-Accel-Redirect header pointing to the original file. It is expected that nginx will return the file using the internal parameter.
Otherwise:
- a file in a django-storages S3 storage with signed URLs (
querystring_auth, the default) — redirect to a temporary signed URL of the file, so the file is downloaded from S3 directly; - a file in any other storage — the file is read through the storage and served by Django.
Source code in meringue/protected/views.py
def x_accel_redirect_view(request, cid, field, pk, disp="inline"):
"""
The view checks the user's access to view and serves the file.
If you enable the parameter
[PROTECTED_SERVE_WITH_NGINX][meringue.conf.default_settings.PROTECTED_SERVE_WITH_NGINX], the
file will not be served; instead, the response will contain the X-Accel-Redirect header
pointing to the original file. It is expected that nginx will return the file using the
[internal](https://nginx.org/en/docs/http/ngx_http_core_module.html#internal) parameter.
Otherwise:
* a file in a django-storages S3 storage with signed URLs (`querystring_auth`, the default) —
redirect to a temporary signed URL of the file, so the file is downloaded from S3 directly;
* a file in any other storage — the file is read through the storage and served by Django.
"""
contenttype = ContentType.objects.get(id=cid)
if not request.user.has_perm(f"{contenttype.app_label}.view_{contenttype.model}"):
raise PermissionDenied()
model = contenttype.model_class().objects.get(pk=pk)
file = getattr(model, field, None)
if not isinstance(file, FieldFile) or not file:
raise Http404()
file_name = PurePosixPath(file.name).name
if m_settings.PROTECTED_SERVE_WITH_NGINX:
response = HttpResponse()
# the file name and not `file.path`: remote storages (S3) have no local paths
response["Content-Type"] = mimetypes.guess_type(file_name)[0]
response["Content-Disposition"] = get_content_disposition(file_name, disp)
if isinstance(file, ProtectedFieldFile | ProtectedImageFieldFile):
redirect_url = file.redirect_url
else:
redirect_url = file.url
response["X-Accel-Redirect"] = redirect_url
return response
signed_url = get_signed_url(file, disp)
if signed_url:
return HttpResponseRedirect(signed_url)
return FileResponse(
file.storage.open(file.name, "rb"),
as_attachment=disp == "attachment",
filename=file_name,
)