Skip to content

Commit

Permalink
New: Custom import scripts can communicate information back
Browse files Browse the repository at this point in the history
(cherry picked from commit b4ac495983d61819d9ab84f49c880957ba57418b)
  • Loading branch information
JeWe37 authored and mynameisbogdan committed Jan 14, 2024
1 parent dca9d69 commit d72f78d
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 48 deletions.
Expand Up @@ -156,7 +156,7 @@ public void should_not_scan_extras_subfolder()
Subject.Scan(_movie);

Mocker.GetMock<IDiskProvider>()
.Verify(v => v.GetFiles(It.IsAny<string>(), It.IsAny<bool>()), Times.Once());
.Verify(v => v.GetFiles(It.IsAny<string>(), It.IsAny<bool>()), Times.Exactly(2));

Mocker.GetMock<IMakeImportDecision>()
.Verify(v => v.GetImportDecisions(It.Is<List<string>>(l => l.Count == 1), _movie, false), Times.Once());
Expand Down
@@ -1,4 +1,5 @@
using System.IO;
using System.Collections.Generic;
using System.IO;
using FizzWare.NBuilder;
using FluentAssertions;
using Moq;
Expand Down Expand Up @@ -71,7 +72,7 @@ public void should_skip_up_to_date_media_info()
GivenFileExists();
GivenSuccessfulScan();

Subject.Handle(new MovieScannedEvent(_movie));
Subject.Handle(new MovieScannedEvent(_movie, new List<string>()));

Mocker.GetMock<IVideoFileInfoReader>()
.Verify(v => v.GetMediaInfo(Path.Combine(_movie.Path, "media.mkv")), Times.Exactly(2));
Expand All @@ -97,7 +98,7 @@ public void should_skip_not_yet_date_media_info()
GivenFileExists();
GivenSuccessfulScan();

Subject.Handle(new MovieScannedEvent(_movie));
Subject.Handle(new MovieScannedEvent(_movie, new List<string>()));

Mocker.GetMock<IVideoFileInfoReader>()
.Verify(v => v.GetMediaInfo(Path.Combine(_movie.Path, "media.mkv")), Times.Exactly(2));
Expand All @@ -123,7 +124,7 @@ public void should_update_outdated_media_info()
GivenFileExists();
GivenSuccessfulScan();

Subject.Handle(new MovieScannedEvent(_movie));
Subject.Handle(new MovieScannedEvent(_movie, new List<string>()));

Mocker.GetMock<IVideoFileInfoReader>()
.Verify(v => v.GetMediaInfo(Path.Combine(_movie.Path, "media.mkv")), Times.Exactly(3));
Expand All @@ -146,7 +147,7 @@ public void should_ignore_missing_files()

GivenSuccessfulScan();

Subject.Handle(new MovieScannedEvent(_movie));
Subject.Handle(new MovieScannedEvent(_movie, new List<string>()));

Mocker.GetMock<IVideoFileInfoReader>()
.Verify(v => v.GetMediaInfo("media.mkv"), Times.Never());
Expand All @@ -173,7 +174,7 @@ public void should_continue_after_failure()
GivenSuccessfulScan();
GivenFailedScan(Path.Combine(_movie.Path, "media2.mkv"));

Subject.Handle(new MovieScannedEvent(_movie));
Subject.Handle(new MovieScannedEvent(_movie, new List<string>()));

Mocker.GetMock<IVideoFileInfoReader>()
.Verify(v => v.GetMediaInfo(Path.Combine(_movie.Path, "media.mkv")), Times.Exactly(1));
Expand Down Expand Up @@ -203,7 +204,7 @@ public void should_not_update_files_if_media_info_disabled()
GivenFileExists();
GivenSuccessfulScan();

Subject.Handle(new MovieScannedEvent(_movie));
Subject.Handle(new MovieScannedEvent(_movie, new List<string>()));

Mocker.GetMock<IVideoFileInfoReader>()
.Verify(v => v.GetMediaInfo(It.IsAny<string>()), Times.Never());
Expand Down
39 changes: 18 additions & 21 deletions src/NzbDrone.Core/Extras/ExistingExtraFileService.cs
Expand Up @@ -2,45 +2,33 @@
using System.IO;
using System.Linq;
using NLog;
using NzbDrone.Common.Disk;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.MediaFiles.Events;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Movies;

namespace NzbDrone.Core.Extras
{
public class ExistingExtraFileService : IHandle<MovieScannedEvent>
public interface IExistingExtraFiles
{
List<string> ImportExtraFiles(Movie movie, List<string> possibleExtraFiles);
}

public class ExistingExtraFileService : IExistingExtraFiles, IHandle<MovieScannedEvent>
{
private readonly IDiskProvider _diskProvider;
private readonly IDiskScanService _diskScanService;
private readonly List<IImportExistingExtraFiles> _existingExtraFileImporters;
private readonly Logger _logger;

public ExistingExtraFileService(IDiskProvider diskProvider,
IDiskScanService diskScanService,
IEnumerable<IImportExistingExtraFiles> existingExtraFileImporters,
public ExistingExtraFileService(IEnumerable<IImportExistingExtraFiles> existingExtraFileImporters,
Logger logger)
{
_diskProvider = diskProvider;
_diskScanService = diskScanService;
_existingExtraFileImporters = existingExtraFileImporters.OrderBy(e => e.Order).ToList();
_logger = logger;
}

public void Handle(MovieScannedEvent message)
public List<string> ImportExtraFiles(Movie movie, List<string> possibleExtraFiles)
{
var movie = message.Movie;

if (!_diskProvider.FolderExists(movie.Path))
{
return;
}

_logger.Debug("Looking for existing extra files in {0}", movie.Path);

var filesOnDisk = _diskScanService.GetNonVideoFiles(movie.Path);
var possibleExtraFiles = _diskScanService.FilterPaths(movie.Path, filesOnDisk, false);

var importedFiles = new List<string>();

foreach (var existingExtraFileImporter in _existingExtraFileImporters)
Expand All @@ -50,6 +38,15 @@ public void Handle(MovieScannedEvent message)
importedFiles.AddRange(imported.Select(f => Path.Combine(movie.Path, f.RelativePath)));
}

return importedFiles;
}

public void Handle(MovieScannedEvent message)
{
var movie = message.Movie;
var possibleExtraFiles = message.PossibleExtraFiles;
var importedFiles = ImportExtraFiles(movie, possibleExtraFiles);

_logger.Info("Found {0} possible extra files, imported {1} files.", possibleExtraFiles.Count, importedFiles.Count);
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/NzbDrone.Core/Extras/ExtraService.cs
Expand Up @@ -17,6 +17,7 @@ namespace NzbDrone.Core.Extras
{
public interface IExtraService
{
void MoveFilesAfterRename(Movie movie, MovieFile movieFile);
void ImportMovie(LocalMovie localMovie, MovieFile movieFile, bool isReadOnly);
}

Expand Down Expand Up @@ -139,6 +140,16 @@ public void Handle(MovieFolderCreatedEvent message)
}
}

public void MoveFilesAfterRename(Movie movie, MovieFile movieFile)
{
var movieFiles = new List<MovieFile> { movieFile };

foreach (var extraFileManager in _extraFileManagers)
{
extraFileManager.MoveFilesAfterRename(movie, movieFiles);
}
}

public void Handle(MovieRenamedEvent message)
{
var movie = message.Movie;
Expand Down
11 changes: 7 additions & 4 deletions src/NzbDrone.Core/MediaFiles/DiskScanService.cs
Expand Up @@ -121,7 +121,7 @@ public void Scan(Movie movie)
}

CleanMediaFiles(movie, new List<string>());
CompletedScanning(movie);
CompletedScanning(movie, new List<string>());

return;
}
Expand Down Expand Up @@ -173,8 +173,11 @@ public void Scan(Movie movie)
fileInfoStopwatch.Stop();
_logger.Trace("Reprocessing existing files complete for: {0} [{1}]", movie, decisionsStopwatch.Elapsed);

var filesOnDisk = GetNonVideoFiles(movie.Path);
var possibleExtraFiles = FilterPaths(movie.Path, filesOnDisk);

RemoveEmptyMovieFolder(movie.Path);
CompletedScanning(movie);
CompletedScanning(movie, possibleExtraFiles);
}

private void CleanMediaFiles(Movie movie, List<string> mediaFileList)
Expand All @@ -183,10 +186,10 @@ private void CleanMediaFiles(Movie movie, List<string> mediaFileList)
_mediaFileTableCleanupService.Clean(movie, mediaFileList);
}

private void CompletedScanning(Movie movie)
private void CompletedScanning(Movie movie, List<string> possibleExtraFiles)
{
_logger.Info("Completed scanning disk for {0}", movie.Title);
_eventAggregator.PublishEvent(new MovieScannedEvent(movie));
_eventAggregator.PublishEvent(new MovieScannedEvent(movie, possibleExtraFiles));
}

public string[] GetVideoFiles(string path, bool allDirectories = true)
Expand Down
7 changes: 5 additions & 2 deletions src/NzbDrone.Core/MediaFiles/Events/MovieScannedEvent.cs
@@ -1,15 +1,18 @@
using NzbDrone.Common.Messaging;
using System.Collections.Generic;
using NzbDrone.Common.Messaging;
using NzbDrone.Core.Movies;

namespace NzbDrone.Core.MediaFiles.Events
{
public class MovieScannedEvent : IEvent
{
public Movie Movie { get; private set; }
public List<string> PossibleExtraFiles { get; set; }

public MovieScannedEvent(Movie movie)
public MovieScannedEvent(Movie movie, List<string> possibleExtraFiles)
{
Movie = movie;
PossibleExtraFiles = possibleExtraFiles;
}
}
}
1 change: 1 addition & 0 deletions src/NzbDrone.Core/MediaFiles/MovieFileMovingService.cs
Expand Up @@ -126,6 +126,7 @@ private MovieFile TransferFile(MovieFile movieFile, Movie movie, string destinat
try
{
MoveMovieFile(movieFile, movie);
localMovie.FileRenamedAfterScriptImport = true;
}
catch (SameFilenameException)
{
Expand Down
18 changes: 17 additions & 1 deletion src/NzbDrone.Core/MediaFiles/MovieImport/ImportApprovedMovie.cs
Expand Up @@ -27,6 +27,7 @@ public class ImportApprovedMovie : IImportApprovedMovie
private readonly IUpgradeMediaFiles _movieFileUpgrader;
private readonly IMediaFileService _mediaFileService;
private readonly IExtraService _extraService;
private readonly IExistingExtraFiles _existingExtraFiles;
private readonly IDiskProvider _diskProvider;
private readonly IHistoryService _historyService;
private readonly IEventAggregator _eventAggregator;
Expand All @@ -36,6 +37,7 @@ public class ImportApprovedMovie : IImportApprovedMovie
public ImportApprovedMovie(IUpgradeMediaFiles movieFileUpgrader,
IMediaFileService mediaFileService,
IExtraService extraService,
IExistingExtraFiles existingExtraFiles,
IDiskProvider diskProvider,
IHistoryService historyService,
IEventAggregator eventAggregator,
Expand All @@ -45,6 +47,7 @@ public class ImportApprovedMovie : IImportApprovedMovie
_movieFileUpgrader = movieFileUpgrader;
_mediaFileService = mediaFileService;
_extraService = extraService;
_existingExtraFiles = existingExtraFiles;
_diskProvider = diskProvider;
_historyService = historyService;
_eventAggregator = eventAggregator;
Expand Down Expand Up @@ -146,7 +149,20 @@ public List<ImportResult> Import(List<ImportDecision> decisions, bool newDownloa

if (newDownload)
{
_extraService.ImportMovie(localMovie, movieFile, copyOnly);
if (localMovie.ScriptImported)
{
_existingExtraFiles.ImportExtraFiles(localMovie.Movie, localMovie.PossibleExtraFiles);

if (localMovie.FileRenamedAfterScriptImport)
{
_extraService.MoveFilesAfterRename(localMovie.Movie, movieFile);
}
}

if (!localMovie.ScriptImported || localMovie.ShouldImportExtras)
{
_extraService.ImportMovie(localMovie, movieFile, copyOnly);
}
}

_eventAggregator.PublishEvent(new MovieFileImportedEvent(localMovie, movieFile, oldFiles, newDownload, downloadClientItem));
Expand Down

0 comments on commit d72f78d

Please sign in to comment.