uploads: Add an endpoint which forces a download.

This is most useful for images hosted in S3, which are otherwise
always displayed in the browser.
This commit is contained in:
Alex Vandiver
2022-03-21 23:38:18 -04:00
committed by Tim Abbott
parent d7b59c86ce
commit abed174b12
4 changed files with 55 additions and 9 deletions

View File

@@ -385,7 +385,7 @@ def get_file_info(request: HttpRequest, user_file: File) -> Tuple[str, int, Opti
return uploaded_file_name, uploaded_file_size, content_type
def get_signed_upload_url(path: str) -> str:
def get_signed_upload_url(path: str, download: bool = False) -> str:
client = boto3.client(
"s3",
aws_access_key_id=settings.S3_KEY,
@@ -393,9 +393,16 @@ def get_signed_upload_url(path: str) -> str:
region_name=settings.S3_REGION,
endpoint_url=settings.S3_ENDPOINT_URL,
)
params = {
"Bucket": settings.S3_AUTH_UPLOADS_BUCKET,
"Key": path,
}
if download:
params["ResponseContentDisposition"] = "attachment"
return client.generate_presigned_url(
ClientMethod="get_object",
Params={"Bucket": settings.S3_AUTH_UPLOADS_BUCKET, "Key": path},
Params=params,
ExpiresIn=SIGNED_UPLOAD_URL_DURATION,
HttpMethod="GET",
)