From 90d1149c07e57bb9e89689266c238b1cd2ad0044 Mon Sep 17 00:00:00 2001 From: jedi Date: Wed, 1 Nov 2023 02:42:34 +0100 Subject: [PATCH] use HttpResponse in /media endpoint --- .gitignore | 1 + backend/files/media_urls.py | 13 +++++++------ backend/files/tests.py | 4 ++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 9ed0f77..e2b6211 100644 --- a/.gitignore +++ b/.gitignore @@ -129,3 +129,4 @@ dmypy.json .pyre/ staticfiles/ +testdata.py \ No newline at end of file diff --git a/backend/files/media_urls.py b/backend/files/media_urls.py index 7184173..ec2c24e 100644 --- a/backend/files/media_urls.py +++ b/backend/files/media_urls.py @@ -1,3 +1,4 @@ +from django.http import HttpResponse from django.urls import path from drf_yasg.utils import swagger_auto_schema from rest_framework import status @@ -13,12 +14,12 @@ from files.models import File def media_urls(request, id, format=None): try: file = File.objects.get(file=id) - return Response(status=status.HTTP_200_OK, - content_type=file.mime_type, - headers={ - 'X-Accel-Redirect': f'/redirect_media/{id}', - 'Access-Control-Allow-Origin': '*', - }) # TODO Expires and Cache-Control + return HttpResponse(status=status.HTTP_200_OK, + content_type=file.mime_type, + headers={ + 'X-Accel-Redirect': f'/redirect_media/{id}', + 'Access-Control-Allow-Origin': '*', + }) # TODO Expires and Cache-Control except File.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) diff --git a/backend/files/tests.py b/backend/files/tests.py index a27c39e..a9dc525 100644 --- a/backend/files/tests.py +++ b/backend/files/tests.py @@ -118,14 +118,14 @@ class MediaUrlTestCase(FilesTestMixin, UserTestMixin, ToolshedTestCase): self.assertEqual(reply.status_code, 200) self.assertEqual(reply.headers['X-Accel-Redirect'], f"/redirect_media/{self.f['hash1'][:2]}/{self.f['hash1'][2:4]}/{self.f['hash1'][4:6]}/{self.f['hash1'][6:]}") - self.assertEqual(reply.content_type, self.f['test_file1'].mime_type) + self.assertEqual(reply.headers['Content-Type'], self.f['test_file1'].mime_type) reply = client.get( f"/media/{self.f['hash2'][:2]}/{self.f['hash2'][2:4]}/{self.f['hash2'][4:6]}/{self.f['hash2'][6:]}", self.f['local_user1']) self.assertEqual(reply.status_code, 200) self.assertEqual(reply.headers['X-Accel-Redirect'], f"/redirect_media/{self.f['hash2'][:2]}/{self.f['hash2'][2:4]}/{self.f['hash2'][4:6]}/{self.f['hash2'][6:]}") - self.assertEqual(reply.content_type, self.f['test_file2'].mime_type) + self.assertEqual(reply.headers['Content-Type'], self.f['test_file2'].mime_type) def test_file_url_fail(self): reply = client.get('/media/{}/'.format('nonexistent'), self.f['local_user1'])