Commit 65394771 authored by Ilya Simonov's avatar Ilya Simonov

fix save files to media folder

parent 4bcb6997
...@@ -100,7 +100,7 @@ class Video(BaseModel): ...@@ -100,7 +100,7 @@ class Video(BaseModel):
description = models.TextField(_('description'), blank=True, null=True) description = models.TextField(_('description'), blank=True, null=True)
local_file = models.FileField( local_file = models.FileField(
_('local file'), _('local file'),
upload_to='videos', upload_to=asset_upload,
blank=True, blank=True,
null=True, null=True,
) )
......
...@@ -12,7 +12,7 @@ def generate_default_thumbnail_for_video(sender, instance, **kwargs): ...@@ -12,7 +12,7 @@ def generate_default_thumbnail_for_video(sender, instance, **kwargs):
if not instance.thumbnail and instance.local_file: if not instance.thumbnail and instance.local_file:
if os.path.isfile(instance.local_file.path): if os.path.isfile(instance.local_file.path):
thumbnail_name = utils.generate_thumbnail(instance.local_file) thumbnail_name = utils.generate_thumbnail(instance.local_file)
thumbnail_path = f'{settings.BASE_DIR}/videos/{thumbnail_name}' thumbnail_path = f'{settings.MEDIA_ROOT}videos/{instance.id}/{thumbnail_name}'
content = utils.get_thumbnail_content(thumbnail_path) content = utils.get_thumbnail_content(thumbnail_path)
key = f'videos/{instance.id}/{thumbnail_name}' key = f'videos/{instance.id}/{thumbnail_name}'
......
import io import io
from django.conf import settings
from PIL import Image as PImage from PIL import Image as PImage
from moviepy.editor import VideoFileClip from moviepy.editor import VideoFileClip
...@@ -11,7 +13,7 @@ def asset_upload(instance, filename): ...@@ -11,7 +13,7 @@ def asset_upload(instance, filename):
def generate_thumbnail(video): def generate_thumbnail(video):
clip = VideoFileClip(video.path) clip = VideoFileClip(video.path)
thumbnail_name = video.name.split('.')[0] thumbnail_name = video.name.split('.')[0]
thumbnail_name = f'{thumbnail_name}.jpg' thumbnail_name = f'{settings.MEDIA_ROOT}{thumbnail_name}.jpg'
clip.save_frame(thumbnail_name, t=1.00) clip.save_frame(thumbnail_name, t=1.00)
thumbnail_name = thumbnail_name.split('/')[-1] thumbnail_name = thumbnail_name.split('/')[-1]
......
...@@ -142,23 +142,27 @@ USE_TZ = True ...@@ -142,23 +142,27 @@ USE_TZ = True
# Static files (CSS, JavaScript, Images) # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/' STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static') STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
# Celery # Celery
# https://docs.celeryq.dev/en/stable/django/first-steps-with-django.html#using-celery-with-django
REDIS_URL = env('REDIS_URL', default=f'redis://127.0.0.1:6379') REDIS_URL = env('REDIS_URL', default=f'redis://127.0.0.1:6379')
CELERY_BROKER_URL = env('CELERY_BROKER_URL', default=f'{REDIS_URL}') CELERY_BROKER_URL = env('CELERY_BROKER_URL', default=f'{REDIS_URL}')
CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler' CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'
# AWS # AWS
AWS_STORAGE_BUCKET_NAME = env('AWS_STORAGE_BUCKET_NAME') path = f'{BASE_DIR}/cp_video/local.py'
AWS_ACCESS_KEY_ID = env('AWS_ACCESS_KEY_ID') if os.path.exists(path):
AWS_SECRET_ACCESS_KEY = env('AWS_SECRET_ACCESS_KEY') from .local import *
AWS_REGION_NAME = env('AWS_REGION_NAME') else:
# CloudFront AWS_STORAGE_BUCKET_NAME = env('AWS_STORAGE_BUCKET_NAME')
AWS_S3_CUSTOM_DOMAIN = env('AWS_S3_CUSTOM_DOMAIN') AWS_ACCESS_KEY_ID = env('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = env('AWS_SECRET_ACCESS_KEY')
AWS_REGION_NAME = env('AWS_REGION_NAME')
# CloudFront
AWS_S3_CUSTOM_DOMAIN = env('AWS_S3_CUSTOM_DOMAIN')
...@@ -14,8 +14,10 @@ Including another URLconf ...@@ -14,8 +14,10 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
""" """
import debug_toolbar import debug_toolbar
from django.conf import settings
from django.contrib import admin from django.contrib import admin
from django.urls import path, include from django.urls import path, include
from django.conf.urls.static import static
urlpatterns = [ urlpatterns = [
path('__debug__/', include(debug_toolbar.urls)), path('__debug__/', include(debug_toolbar.urls)),
...@@ -23,3 +25,8 @@ urlpatterns = [ ...@@ -23,3 +25,8 @@ urlpatterns = [
path('api/', include('apps.core.urls')), path('api/', include('apps.core.urls')),
] ]
urlpatterns += static(
settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT,
)
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