2018-02-15 12:39:01 +00:00
|
|
|
using System;
|
2015-12-25 09:22:00 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2019-12-18 02:17:47 +00:00
|
|
|
using Marr.Data;
|
2015-12-25 09:22:00 +00:00
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common.Disk;
|
|
|
|
using NzbDrone.Core.Configuration;
|
|
|
|
using NzbDrone.Core.Extras.Files;
|
|
|
|
using NzbDrone.Core.MediaCover;
|
|
|
|
using NzbDrone.Core.MediaFiles;
|
|
|
|
using NzbDrone.Core.MediaFiles.Events;
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
|
|
|
using NzbDrone.Core.Parser.Model;
|
2018-03-14 20:41:36 +00:00
|
|
|
using NzbDrone.Core.Movies;
|
2015-12-25 09:22:00 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Extras
|
|
|
|
{
|
|
|
|
public interface IExtraService
|
|
|
|
{
|
2018-04-01 19:48:22 +00:00
|
|
|
void ImportMovie(LocalMovie localMovie, MovieFile movieFile, bool isReadOnly);
|
2015-12-25 09:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class ExtraService : IExtraService,
|
|
|
|
IHandle<MediaCoversUpdatedEvent>,
|
2018-04-01 19:48:22 +00:00
|
|
|
IHandle<MovieFolderCreatedEvent>,
|
2018-02-15 12:39:01 +00:00
|
|
|
IHandle<MovieRenamedEvent>
|
2015-12-25 09:22:00 +00:00
|
|
|
{
|
|
|
|
private readonly IMediaFileService _mediaFileService;
|
2018-02-15 12:39:01 +00:00
|
|
|
private readonly IMovieService _movieService;
|
2015-12-25 09:22:00 +00:00
|
|
|
private readonly IDiskProvider _diskProvider;
|
|
|
|
private readonly IConfigService _configService;
|
|
|
|
private readonly List<IManageExtraFiles> _extraFileManagers;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
public ExtraService(IMediaFileService mediaFileService,
|
2018-02-15 12:39:01 +00:00
|
|
|
IMovieService movieService,
|
2015-12-25 09:22:00 +00:00
|
|
|
IDiskProvider diskProvider,
|
|
|
|
IConfigService configService,
|
|
|
|
List<IManageExtraFiles> extraFileManagers,
|
|
|
|
Logger logger)
|
|
|
|
{
|
|
|
|
_mediaFileService = mediaFileService;
|
2018-02-15 12:39:01 +00:00
|
|
|
_movieService = movieService;
|
2015-12-25 09:22:00 +00:00
|
|
|
_diskProvider = diskProvider;
|
|
|
|
_configService = configService;
|
|
|
|
_extraFileManagers = extraFileManagers.OrderBy(e => e.Order).ToList();
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2018-04-01 19:48:22 +00:00
|
|
|
public void ImportMovie(LocalMovie localMovie, MovieFile movieFile, bool isReadOnly)
|
2015-12-25 09:22:00 +00:00
|
|
|
{
|
2018-04-01 19:48:22 +00:00
|
|
|
ImportExtraFiles(localMovie, movieFile, isReadOnly);
|
2015-12-25 09:22:00 +00:00
|
|
|
|
2018-04-01 19:48:22 +00:00
|
|
|
CreateAfterImport(localMovie.Movie, movieFile);
|
|
|
|
}
|
2015-12-25 09:22:00 +00:00
|
|
|
|
2018-04-01 19:48:22 +00:00
|
|
|
public void ImportExtraFiles(LocalMovie localMovie, MovieFile movieFile, bool isReadOnly)
|
|
|
|
{
|
2018-02-15 12:39:01 +00:00
|
|
|
if (!_configService.ImportExtraFiles)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-09-19 05:19:08 +00:00
|
|
|
|
2018-02-15 12:39:01 +00:00
|
|
|
var sourcePath = localMovie.Path;
|
2015-12-25 09:22:00 +00:00
|
|
|
var sourceFolder = _diskProvider.GetParentFolder(sourcePath);
|
|
|
|
var sourceFileName = Path.GetFileNameWithoutExtension(sourcePath);
|
|
|
|
var files = _diskProvider.GetFiles(sourceFolder, SearchOption.TopDirectoryOnly);
|
|
|
|
|
|
|
|
var wantedExtensions = _configService.ExtraFileExtensions.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
|
|
|
|
.Select(e => e.Trim(' ', '.'))
|
|
|
|
.ToList();
|
|
|
|
|
2019-07-01 01:50:01 +00:00
|
|
|
var matchingFilenames = files.Where(f => Path.GetFileNameWithoutExtension(f).StartsWith(sourceFileName, StringComparison.InvariantCultureIgnoreCase)).ToList();
|
|
|
|
var filteredFilenames = new List<string>();
|
|
|
|
var hasNfo = false;
|
2015-12-25 09:22:00 +00:00
|
|
|
|
|
|
|
foreach (var matchingFilename in matchingFilenames)
|
2019-07-01 01:50:01 +00:00
|
|
|
{
|
|
|
|
// Filter out duplicate NFO files
|
|
|
|
|
|
|
|
if (matchingFilename.EndsWith(".nfo", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
{
|
|
|
|
if (hasNfo)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
hasNfo = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
filteredFilenames.Add(matchingFilename);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var matchingFilename in filteredFilenames)
|
2015-12-25 09:22:00 +00:00
|
|
|
{
|
|
|
|
var matchingExtension = wantedExtensions.FirstOrDefault(e => matchingFilename.EndsWith(e));
|
|
|
|
|
|
|
|
if (matchingExtension == null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
foreach (var extraFileManager in _extraFileManagers)
|
|
|
|
{
|
2018-02-15 12:39:01 +00:00
|
|
|
var extension = Path.GetExtension(matchingFilename);
|
2018-04-01 19:48:22 +00:00
|
|
|
var extraFile = extraFileManager.Import(localMovie.Movie, movieFile, matchingFilename, extension, isReadOnly);
|
2015-12-25 09:22:00 +00:00
|
|
|
|
|
|
|
if (extraFile != null)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.Warn(ex, "Failed to import extra file: {0}", matchingFilename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-01 19:48:22 +00:00
|
|
|
private void CreateAfterImport(Movie movie, MovieFile movieFile)
|
|
|
|
{
|
|
|
|
foreach (var extraFileManager in _extraFileManagers)
|
|
|
|
{
|
|
|
|
extraFileManager.CreateAfterMovieImport(movie, movieFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-25 09:22:00 +00:00
|
|
|
public void Handle(MediaCoversUpdatedEvent message)
|
|
|
|
{
|
2018-02-15 12:39:01 +00:00
|
|
|
var movie = message.Movie;
|
|
|
|
var movieFiles = GetMovieFiles(movie.Id);
|
2015-12-25 09:22:00 +00:00
|
|
|
|
|
|
|
foreach (var extraFileManager in _extraFileManagers)
|
|
|
|
{
|
2018-02-15 12:39:01 +00:00
|
|
|
extraFileManager.CreateAfterMovieScan(movie, movieFiles);
|
2015-12-25 09:22:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-01 19:48:22 +00:00
|
|
|
public void Handle(MovieFolderCreatedEvent message)
|
|
|
|
{
|
|
|
|
var movie = message.Movie;
|
|
|
|
|
|
|
|
foreach (var extraFileManager in _extraFileManagers)
|
|
|
|
{
|
|
|
|
extraFileManager.CreateAfterMovieImport(movie, message.MovieFolder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 12:39:01 +00:00
|
|
|
public void Handle(MovieRenamedEvent message)
|
2015-12-25 09:22:00 +00:00
|
|
|
{
|
2018-02-15 12:39:01 +00:00
|
|
|
var movie = message.Movie;
|
|
|
|
var movieFiles = GetMovieFiles(movie.Id);
|
2015-12-25 09:22:00 +00:00
|
|
|
|
|
|
|
foreach (var extraFileManager in _extraFileManagers)
|
|
|
|
{
|
2018-02-15 12:39:01 +00:00
|
|
|
extraFileManager.MoveFilesAfterRename(movie, movieFiles);
|
2015-12-25 09:22:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 12:39:01 +00:00
|
|
|
private List<MovieFile> GetMovieFiles(int movieId)
|
2015-12-25 09:22:00 +00:00
|
|
|
{
|
2018-02-15 12:39:01 +00:00
|
|
|
var movieFiles = _mediaFileService.GetFilesByMovie(movieId);
|
2015-12-25 09:22:00 +00:00
|
|
|
|
2018-02-15 12:39:01 +00:00
|
|
|
foreach (var movieFile in movieFiles)
|
2015-12-25 09:22:00 +00:00
|
|
|
{
|
2019-12-18 02:17:47 +00:00
|
|
|
movieFile.Movie = new LazyLoaded<Movie>(_movieService.GetMovie(movieId));
|
2015-12-25 09:22:00 +00:00
|
|
|
}
|
|
|
|
|
2018-02-15 12:39:01 +00:00
|
|
|
return movieFiles;
|
2015-12-25 09:22:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|