From 0e278bc8c42551f27d858c2f7e371c8e28e8fb9b Mon Sep 17 00:00:00 2001 From: Laurent DEFERT Date: Wed, 28 Dec 2022 10:34:34 +0100 Subject: [PATCH 1/2] fix relative media_file path FileField should store a relative path, to make their "url" attribute work --- .../migrations/0013_fix_elative_media_file.py | 25 +++++++++++++++++++ tubesync/sync/models.py | 8 ++++-- tubesync/sync/tasks.py | 2 +- 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 tubesync/sync/migrations/0013_fix_elative_media_file.py diff --git a/tubesync/sync/migrations/0013_fix_elative_media_file.py b/tubesync/sync/migrations/0013_fix_elative_media_file.py new file mode 100644 index 0000000..c9eee22 --- /dev/null +++ b/tubesync/sync/migrations/0013_fix_elative_media_file.py @@ -0,0 +1,25 @@ +# Generated by Django 3.2.12 on 2022-04-06 06:19 + +from django.conf import settings +from django.db import migrations, models + + +def fix_media_file(apps, schema_editor): + Media = apps.get_model('sync', 'Media') + for media in Media.objects.filter(downloaded=True): + download_dir = str(settings.DOWNLOAD_ROOT) + + if media.media_file.name.startswith(download_dir): + media.media_file.name = media.media_file.name[len(download_dir) + 1:] + media.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('sync', '0012_alter_media_downloaded_format'), + ] + + operations = [ + migrations.RunPython(fix_media_file) + ] diff --git a/tubesync/sync/models.py b/tubesync/sync/models.py index 15437f6..4e42086 100644 --- a/tubesync/sync/models.py +++ b/tubesync/sync/models.py @@ -392,10 +392,14 @@ class Source(models.Model): @property def directory_path(self): download_dir = Path(media_file_storage.location) + return download_dir / self.type_directory_path + + @property + def type_directory_path(self): if self.source_resolution == self.SOURCE_RESOLUTION_AUDIO: - return download_dir / settings.DOWNLOAD_AUDIO_DIR / self.directory + return Path(settings.DOWNLOAD_AUDIO_DIR) / self.directory else: - return download_dir / settings.DOWNLOAD_VIDEO_DIR / self.directory + return Path(settings.DOWNLOAD_VIDEO_DIR) / self.directory def make_directory(self): return os.makedirs(self.directory_path, exist_ok=True) diff --git a/tubesync/sync/tasks.py b/tubesync/sync/tasks.py index 34f1381..a597d11 100644 --- a/tubesync/sync/tasks.py +++ b/tubesync/sync/tasks.py @@ -341,7 +341,7 @@ def download_media(media_id): log.info(f'Successfully downloaded media: {media} (UUID: {media.pk}) to: ' f'"{filepath}"') # Link the media file to the object and update info about the download - media.media_file.name = str(filepath) + media.media_file.name = str(media.source.type_directory_path / media.filename) media.downloaded = True media.download_date = timezone.now() media.downloaded_filesize = os.path.getsize(filepath) From bf99241ad283fde8db3c19cf266e66af34b3050f Mon Sep 17 00:00:00 2001 From: Laurent DEFERT Date: Tue, 27 Dec 2022 23:51:03 +0100 Subject: [PATCH 2/2] embedded video player, video downloads --- config/root/etc/nginx/nginx.conf | 5 +++++ tubesync/sync/models.py | 2 +- tubesync/sync/templates/sync/media-item.html | 6 ++++++ tubesync/sync/urls.py | 6 +++++- tubesync/sync/views.py | 19 +++++++++++++++++++ 5 files changed, 36 insertions(+), 2 deletions(-) diff --git a/config/root/etc/nginx/nginx.conf b/config/root/etc/nginx/nginx.conf index 4cddd7b..14c5aea 100644 --- a/config/root/etc/nginx/nginx.conf +++ b/config/root/etc/nginx/nginx.conf @@ -79,6 +79,11 @@ http { proxy_connect_timeout 10; } + # File dwnload and streaming + location /media-data/ { + internal; + alias /downloads/; + } } } diff --git a/tubesync/sync/models.py b/tubesync/sync/models.py index 4e42086..59ce225 100644 --- a/tubesync/sync/models.py +++ b/tubesync/sync/models.py @@ -21,7 +21,7 @@ from .matching import (get_best_combined_format, get_best_audio_format, from .mediaservers import PlexMediaServer -media_file_storage = FileSystemStorage(location=str(settings.DOWNLOAD_ROOT)) +media_file_storage = FileSystemStorage(location=str(settings.DOWNLOAD_ROOT), base_url='/media-data/') class Source(models.Model): diff --git a/tubesync/sync/templates/sync/media-item.html b/tubesync/sync/templates/sync/media-item.html index 62f6f35..868b1c1 100644 --- a/tubesync/sync/templates/sync/media-item.html +++ b/tubesync/sync/templates/sync/media-item.html @@ -9,6 +9,12 @@ {% if media.title %}

{{ media.title }}

{% endif %}

{{ media.url }}

Downloading to: {{ media.source.directory_path }}

+ {% if download_state == 'downloaded' %} + +

Download

+ {% endif %} {% if not media.can_download %}{% include 'errorbox.html' with message='Media cannot be downloaded because it has no formats which match the source requirements.' %}{% endif %} diff --git a/tubesync/sync/urls.py b/tubesync/sync/urls.py index 9632c3c..bfe73b7 100644 --- a/tubesync/sync/urls.py +++ b/tubesync/sync/urls.py @@ -2,7 +2,7 @@ from django.urls import path from .views import (DashboardView, SourcesView, ValidateSourceView, AddSourceView, SourceView, UpdateSourceView, DeleteSourceView, MediaView, MediaThumbView, MediaItemView, MediaRedownloadView, MediaSkipView, - MediaEnableView, TasksView, CompletedTasksView, ResetTasks, + MediaEnableView, MediaContent, TasksView, CompletedTasksView, ResetTasks, MediaServersView, AddMediaServerView, MediaServerView, DeleteMediaServerView, UpdateMediaServerView) @@ -70,6 +70,10 @@ urlpatterns = [ MediaEnableView.as_view(), name='enable-media'), + path('media-content/', + MediaContent.as_view(), + name='media-content'), + # Task URLs path('tasks', diff --git a/tubesync/sync/views.py b/tubesync/sync/views.py index ce09fe1..0da9df8 100644 --- a/tubesync/sync/views.py +++ b/tubesync/sync/views.py @@ -686,6 +686,25 @@ class MediaEnableView(FormView, SingleObjectMixin): return append_uri_params(url, {'message': 'enabled'}) +class MediaContent(DetailView): + ''' + Redirect to nginx to download the file + ''' + model = Media + + def __init__(self, *args, **kwargs): + self.object = None + super().__init__(*args, **kwargs) + + def dispatch(self, request, *args, **kwargs): + self.object = self.get_object() + + headers = { + 'X-Accel-Redirect': self.object.media_file.url, + } + return HttpResponse(headers=headers) + + class TasksView(ListView): ''' A list of tasks queued to be completed. This is, for example, scraping for new