wip
This commit is contained in:
parent
0f911589ca
commit
5bdfe313de
65 changed files with 2219 additions and 77 deletions
24
core/files/api_v2.py
Normal file
24
core/files/api_v2.py
Normal file
|
@ -0,0 +1,24 @@
|
|||
from rest_framework import serializers, viewsets, routers
|
||||
|
||||
from files.models import File
|
||||
|
||||
|
||||
class FileSerializer(serializers.ModelSerializer):
|
||||
data = serializers.CharField(max_length=1000000, write_only=True)
|
||||
|
||||
class Meta:
|
||||
model = File
|
||||
fields = ['hash', 'data']
|
||||
read_only_fields = ['hash']
|
||||
|
||||
|
||||
class FileViewSet(viewsets.ModelViewSet):
|
||||
serializer_class = FileSerializer
|
||||
queryset = File.objects.all()
|
||||
lookup_field = 'hash'
|
||||
|
||||
|
||||
router = routers.SimpleRouter()
|
||||
router.register(r'files', FileViewSet, basename='files')
|
||||
|
||||
urlpatterns = router.urls
|
57
core/files/media_v2.py
Normal file
57
core/files/media_v2.py
Normal file
|
@ -0,0 +1,57 @@
|
|||
from coverage.annotate import os
|
||||
from django.http import HttpResponse
|
||||
from django.urls import path
|
||||
from drf_yasg.utils import swagger_auto_schema
|
||||
from rest_framework import status
|
||||
from rest_framework.decorators import api_view
|
||||
from rest_framework.response import Response
|
||||
|
||||
from core.settings import MEDIA_ROOT
|
||||
from files.models import File
|
||||
|
||||
|
||||
@swagger_auto_schema(method='GET', auto_schema=None)
|
||||
@api_view(['GET'])
|
||||
def media_urls(request, hash_path):
|
||||
try:
|
||||
file = File.objects.get(file=hash_path)
|
||||
|
||||
return HttpResponse(status=status.HTTP_200_OK,
|
||||
content_type=file.mime_type,
|
||||
headers={
|
||||
'X-Accel-Redirect': f'/redirect_media/{hash_path}',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
}) # TODO Expires and Cache-Control
|
||||
|
||||
except File.DoesNotExist:
|
||||
return Response(status=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
|
||||
@swagger_auto_schema(method='GET', auto_schema=None)
|
||||
@api_view(['GET'])
|
||||
def thumbnail_urls(request, size, hash_path):
|
||||
if size not in [32, 64, 256]:
|
||||
return Response(status=status.HTTP_404_NOT_FOUND)
|
||||
try:
|
||||
file = File.objects.get(file=hash_path)
|
||||
if not os.path.exists(MEDIA_ROOT + f'/thumbnails/{size}/{hash_path}'):
|
||||
from PIL import Image
|
||||
iamge = Image.open(file.file)
|
||||
iamge.thumbnail((size, size))
|
||||
iamge.save(MEDIA_ROOT + f'/media/thumbnails/{size}/{hash_path}', quality=90)
|
||||
|
||||
return HttpResponse(status=status.HTTP_200_OK,
|
||||
content_type=file.mime_type,
|
||||
headers={
|
||||
'X-Accel-Redirect': f'/redirect_thumbnail/{size}/{hash_path}',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
}) # TODO Expires and Cache-Control
|
||||
|
||||
except File.DoesNotExist:
|
||||
return Response(status=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
path('<int:size>/<path:hash_path>/', thumbnail_urls),
|
||||
path('<path:hash_path>/', media_urls),
|
||||
]
|
0
core/files/tests/__init__.py
Normal file
0
core/files/tests/__init__.py
Normal file
0
core/files/tests/v1/__init__.py
Normal file
0
core/files/tests/v1/__init__.py
Normal file
0
core/files/tests/v2/__init__.py
Normal file
0
core/files/tests/v2/__init__.py
Normal file
49
core/files/tests/v2/test_files.py
Normal file
49
core/files/tests/v2/test_files.py
Normal file
|
@ -0,0 +1,49 @@
|
|||
from django.test import TestCase, Client
|
||||
|
||||
from files.models import File
|
||||
from inventory.models import Event, Container, Item
|
||||
|
||||
client = Client()
|
||||
|
||||
|
||||
class FileTestCase(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.event = Event.objects.create(slug='EVENT', name='Event')
|
||||
self.box = Container.objects.create(name='BOX')
|
||||
|
||||
def test_list_files(self):
|
||||
import base64
|
||||
item = File.objects.create(data="data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8'))
|
||||
response = client.get('/api/2/files/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json()[0]['hash'], item.hash)
|
||||
self.assertEqual(len(response.json()[0]['hash']), 64)
|
||||
|
||||
def test_one_file(self):
|
||||
import base64
|
||||
item = File.objects.create(data="data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8'))
|
||||
response = client.get(f'/api/2/files/{item.hash}/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json()['hash'], item.hash)
|
||||
self.assertEqual(len(response.json()['hash']), 64)
|
||||
|
||||
def test_create_file(self):
|
||||
import base64
|
||||
Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
item = Item.objects.create(container=self.box, event=self.event, description='2')
|
||||
response = client.post('/api/2/files/',
|
||||
{'data': "data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8')},
|
||||
content_type='application/json')
|
||||
self.assertEqual(response.status_code, 201)
|
||||
self.assertEqual(len(response.json()['hash']), 64)
|
||||
|
||||
def test_delete_file(self):
|
||||
import base64
|
||||
item = Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
File.objects.create(item=item, data="data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8'))
|
||||
file = File.objects.create(item=item, data="data:text/plain;base64," + base64.b64encode(b"bar").decode('utf-8'))
|
||||
self.assertEqual(len(File.objects.all()), 2)
|
||||
response = client.delete(f'/api/2/files/{file.hash}/')
|
||||
self.assertEqual(response.status_code, 204)
|
Loading…
Add table
Add a link
Reference in a new issue