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/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..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): @@ -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) 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