use HttpResponse in /media endpoint
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
j3d1 2023-11-01 02:42:34 +01:00
parent 41b6bffa61
commit 90d1149c07
3 changed files with 10 additions and 8 deletions

1
.gitignore vendored
View file

@ -129,3 +129,4 @@ dmypy.json
.pyre/ .pyre/
staticfiles/ staticfiles/
testdata.py

View file

@ -1,3 +1,4 @@
from django.http import HttpResponse
from django.urls import path from django.urls import path
from drf_yasg.utils import swagger_auto_schema from drf_yasg.utils import swagger_auto_schema
from rest_framework import status from rest_framework import status
@ -13,12 +14,12 @@ from files.models import File
def media_urls(request, id, format=None): def media_urls(request, id, format=None):
try: try:
file = File.objects.get(file=id) file = File.objects.get(file=id)
return Response(status=status.HTTP_200_OK, return HttpResponse(status=status.HTTP_200_OK,
content_type=file.mime_type, content_type=file.mime_type,
headers={ headers={
'X-Accel-Redirect': f'/redirect_media/{id}', 'X-Accel-Redirect': f'/redirect_media/{id}',
'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Origin': '*',
}) # TODO Expires and Cache-Control }) # TODO Expires and Cache-Control
except File.DoesNotExist: except File.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND) return Response(status=status.HTTP_404_NOT_FOUND)

View file

@ -118,14 +118,14 @@ class MediaUrlTestCase(FilesTestMixin, UserTestMixin, ToolshedTestCase):
self.assertEqual(reply.status_code, 200) self.assertEqual(reply.status_code, 200)
self.assertEqual(reply.headers['X-Accel-Redirect'], 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:]}") 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( reply = client.get(
f"/media/{self.f['hash2'][:2]}/{self.f['hash2'][2:4]}/{self.f['hash2'][4:6]}/{self.f['hash2'][6:]}", 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.f['local_user1'])
self.assertEqual(reply.status_code, 200) self.assertEqual(reply.status_code, 200)
self.assertEqual(reply.headers['X-Accel-Redirect'], 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:]}") 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): def test_file_url_fail(self):
reply = client.get('/media/{}/'.format('nonexistent'), self.f['local_user1']) reply = client.get('/media/{}/'.format('nonexistent'), self.f['local_user1'])