You need to sign in or sign up before continuing.
Commit 215b5bb1 authored by Ilya Simonov's avatar Ilya Simonov

upload file by chunks

parent d4d437ab
No preview for this file type
......@@ -4,6 +4,12 @@ from django.forms import ModelForm
from .models import Video
class UploadFileForm(ModelForm):
class Meta:
model = Video
fields = ('local_file',)
class VideoForm(ModelForm):
class Meta:
model = Video
......
# Generated by Django 4.1.7 on 2023-04-02 14:45
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import storages.backends.s3boto3
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('core', '0006_video_s3_file_upload'),
]
operations = [
migrations.AlterField(
model_name='video',
name='creator',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='creator_video', to=settings.AUTH_USER_MODEL, verbose_name='creator'),
),
migrations.AlterField(
model_name='video',
name='s3_file_upload',
field=models.FileField(blank=True, max_length=500, null=True, storage=storages.backends.s3boto3.S3Boto3Storage(bucket_name='clutchpoints-videos'), upload_to='', verbose_name='AWS S3 file upload'),
),
]
......@@ -131,7 +131,8 @@ class Video(BaseModel):
verbose_name=_('creator'),
related_name='creator_video',
on_delete=models.CASCADE,
blank=True
blank=True,
null=True,
)
status = models.CharField(
_('status'),
......@@ -155,7 +156,6 @@ class Video(BaseModel):
tags = GenericRelation(TagToObject)
class Meta:
ordering = ('title',)
verbose_name = 'Video'
verbose_name_plural = 'Videos'
......
{% extends "admin/change_list.html" %}
{% load i18n admin_urls %}
{% block object-tools %}
<div style="margin-bottom: 50px;">
<div style="margin-bottom: 30px;">
<form action="{% url 'upload-file' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<div><!-- DIV needed for valid HTML -->
<p>Upload Video:</p>
<input type="file" name="local_file" value="" id="local_file" required>
<input type="submit" value="Upload video">
</div>
</form>
</div>
</div>
{{ block.super }}
{% endblock %}
......@@ -5,6 +5,7 @@ from . import views
urlpatterns = [
path('video/', views.VideoAPIView.as_view({'get': 'list'})),
path('video/upload/', views.upload_file, name='upload-file'),
path('playlist/<int:pk>/', views.PlaylistRetrieveAPIView.as_view()),
path('playlist/<int:pk>/mrss', views.playlist_mrss_view),
]
......@@ -8,6 +8,8 @@ from moviepy.editor import VideoFileClip
from .models import Video, Playlist, TagToObject, DYNAMIC, PlaylistVideo
from cp_video.utils import asset_upload
log = logging.getLogger('send_video_to_s3')
......@@ -25,6 +27,12 @@ def download_file(url):
return local_filename
def handle_uploaded_file(f, file_name):
with open(file_name, 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
def generate_thumbnail(video_path, video_name):
clip = VideoFileClip(video_path)
thumbnail_name = video_name.split('.')[0]
......
import os
from rest_framework import permissions, viewsets, generics
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.shortcuts import get_object_or_404, redirect, HttpResponseRedirect
from PyRSS2Gen import RSS2, RSSItem, Guid
from . import models, serializers
from . import models, serializers, forms, utils
from cp_video.pagination import LimitPagination
......@@ -56,3 +58,22 @@ def playlist_mrss_view(request, pk):
rss.write_xml(response)
return response
def upload_file(request):
if request.method == 'POST':
form = forms.UploadFileForm(request.POST, request.FILES)
if form.is_valid():
video_file = request.FILES['local_file']
video_name = video_file.name
utils.handle_uploaded_file(video_file, video_name)
form.save()
new_video_id = models.Video.objects.last().id
redirect_url = f'{request.META["HTTP_ORIGIN"]}/admin/core/video/{new_video_id}/'
os.remove(video_name)
return HttpResponseRedirect(redirect_url)
return redirect(request.META['HTTP_REFERER'])
......@@ -69,7 +69,9 @@ ROOT_URLCONF = 'cp_video.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': [
os.path.join(BASE_DIR, 'templates')
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment