views.py 4.63 KB
from rest_framework import status
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.parsers import MultiPartParser, FormParser
from rest_framework.response import Response
from rest_framework import viewsets
import os
from django.http.request import QueryDict
from django.http import Http404
from django.http import JsonResponse
from django.shortcuts import get_object_or_404, render
from api.models import Video, VideoFile
from api.serializers import VideoSerializer, VideoFileSerializer
from api import  file_upload_path
import subprocess
import shlex

# Create your views here.

def index(request):
    return render(request, template_name='index.html')

class VideoViewSet(viewsets.ModelViewSet):
    queryset = Video.objects.all()
    serializer_class = VideoSerializer

class VideoFileViewSet(viewsets.ModelViewSet):
    queryset = VideoFile.objects.all()
    serializer_class = VideoFileSerializer


class VideoFileUploadView(APIView):
    parser_classes = (MultiPartParser, FormParser)

    def get(self, request, format=None):
        videoFiles = VideoFile.objects.all()
        serializer = VideoFileSerializer(videoFiles, many=True)
        return Response(serializer.data)

    def post(self, req, *args, **kwargs):
        # 동영상 길이
        runTime = 126
       # 요청된 데이터를 꺼냄( QueryDict)         
        new_data = req.data.dict()
        
        # 요청된 파일 객체
        file_name = req.data['file']
        
        # 저장될 파일의 풀path를 생성        
        new_file_full_name = file_upload_path(file_name.name)
        print(new_file_full_name)
        # 새롭게 생성된 파일의 경로
        file_path = '-'.join(new_file_full_name.split('-')[0:-1])        

        new_data['file_path'] = file_path
        new_data['file_origin_name'] = req.data['file'].name
        new_data['file_save_name'] = req.data['file']

        new_query_dict = QueryDict('', mutable=True)
        new_query_dict.update(new_data)
        file_serializer = VideoFileSerializer(data = new_query_dict)
        
        if file_serializer.is_valid():
            file_serializer.save()       
            print(file_serializer.data)
            
            process = subprocess.Popen(['./runMediaPipe.sh %s %s' %(file_serializer.data['file_save_name'],runTime,)], shell = True)
            process.wait()

            return Response(True, status=status.HTTP_201_CREATED)
        else:
            return Response(file_serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    
class VideoFileList(APIView):

    def get_object(self, pk):
        try:
            return VideoFile.objects.get(pk=pk)
        except VideoFile.DoesNotExist:
            raise Http404

    def get(self, request, pk, format=None):
        video = self.get_object(pk)
        serializer = VideoFileSerializer(video)
        return Response(serializer.data)

    def put(self, request, pk, format=None):
        video = self.get_object(pk)
        serializer = VideoFileSerializer(video, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def delete(self, request, pk, format=None):
        video = self.get_object(pk)
        video.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)


class FileListView(APIView):
    def get(self, request):
        data = {
            "search": '',
            "limit": 10,
            "skip": 0,
            "order": "time",
            "fileList": [
                {
                    "name": "1.png",
                    "created": "2020-04-30",
                    "size": 10234,
                    "isFolder": False,
                    "deletedDate": "",
                },
                {
                    "name": "2.png",
                    "created": "2020-04-30",
                    "size": 3145,
                    "isFolder": False,
                    "deletedDate": "",
                },
                {
                    "name": "3.png",
                    "created": "2020-05-01",
                    "size": 5653,
                    "isFolder": False,
                    "deletedDate": "",
                },
            ]
        }
        return Response(data)
    def post(self, request, format=None):
        data = {
            "isSuccess": True,
            "File": {
                "name": "test.jpg",
                "created": "2020-05-02",
                "deletedDate": "",
                "size": 2312,
                "isFolder": False
            }
        }
        return Response(data)