mediacms/uploader/views.py

76 lines
2.6 KiB
Python
Raw Normal View History

2020-12-15 22:33:43 +01:00
# -*- coding: utf-8 -*-
import os
import shutil
from django.conf import settings
from django.core.exceptions import PermissionDenied
2021-05-26 17:35:21 +02:00
from django.core.files import File
from django.http import JsonResponse
from django.views import generic
2020-12-15 22:33:43 +01:00
from cms.permissions import user_allowed_to_upload
from files.helpers import rm_file
2021-05-26 17:35:21 +02:00
from files.models import Media
2020-12-15 22:33:43 +01:00
from .fineuploader import ChunkedFineUploader
2021-05-26 17:35:21 +02:00
from .forms import FineUploaderUploadForm, FineUploaderUploadSuccessForm
2020-12-15 22:33:43 +01:00
class FineUploaderView(generic.FormView):
http_method_names = ("post",)
form_class_upload = FineUploaderUploadForm
form_class_upload_success = FineUploaderUploadSuccessForm
@property
def concurrent(self):
return settings.CONCURRENT_UPLOADS
@property
def chunks_done(self):
return self.chunks_done_param_name in self.request.GET
@property
def chunks_done_param_name(self):
return settings.CHUNKS_DONE_PARAM_NAME
def make_response(self, data, **kwargs):
return JsonResponse(data, **kwargs)
def get_form(self, form_class=None):
if self.chunks_done:
form_class = self.form_class_upload_success
else:
form_class = self.form_class_upload
return form_class(**self.get_form_kwargs())
def dispatch(self, request, *args, **kwargs):
if not user_allowed_to_upload(request):
raise PermissionDenied # HTTP 403
return super(FineUploaderView, self).dispatch(request, *args, **kwargs)
def form_valid(self, form):
self.upload = ChunkedFineUploader(form.cleaned_data, self.concurrent)
if self.upload.concurrent and self.chunks_done:
try:
self.upload.combine_chunks()
except FileNotFoundError:
data = {"success": False, "error": "Error with File Uploading"}
return self.make_response(data, status=400)
elif self.upload.total_parts == 1:
self.upload.save()
else:
self.upload.save()
return self.make_response({"success": True})
# create media!
media_file = os.path.join(settings.MEDIA_ROOT, self.upload.real_path)
with open(media_file, "rb") as f:
myfile = File(f)
new = Media.objects.create(media_file=myfile, user=self.request.user)
rm_file(media_file)
shutil.rmtree(os.path.join(settings.MEDIA_ROOT, self.upload.file_path))
2021-05-26 17:35:21 +02:00
return self.make_response({"success": True, "media_url": new.get_absolute_url()})
2020-12-15 22:33:43 +01:00
def form_invalid(self, form):
data = {"success": False, "error": "%s" % repr(form.errors)}
return self.make_response(data, status=400)