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;
|
|
|
|
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;
|
2018-03-14 20:41:36 +00:00
|
|
|
using NzbDrone.Core.Movies;
|
2019-12-22 22:08:53 +00:00
|
|
|
using NzbDrone.Core.Parser.Model;
|
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>,
|
2020-09-06 02:24:00 +00:00
|
|
|
IHandle<MovieScannedEvent>,
|
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;
|
|
|
|
|
|
|
|
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,
|
2021-10-21 20:04:26 +00:00
|
|
|
IEnumerable<IManageExtraFiles> extraFileManagers,
|
2015-12-25 09:22:00 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-09-06 02:24:00 +00:00
|
|
|
CreateAfterMovieImport(localMovie.Movie, movieFile);
|
2018-04-01 19:48:22 +00:00
|
|
|
}
|
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
|
|
|
|
2022-02-27 20:37:23 +00:00
|
|
|
var folderSearchOption = localMovie.FolderMovieInfo == null
|
|
|
|
? SearchOption.TopDirectoryOnly
|
|
|
|
: SearchOption.AllDirectories;
|
2015-12-25 09:22:00 +00:00
|
|
|
|
|
|
|
var wantedExtensions = _configService.ExtraFileExtensions.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
|
2022-02-27 20:37:23 +00:00
|
|
|
.Select(e => e.Trim(' ', '.')
|
|
|
|
.Insert(0, "."))
|
2015-12-25 09:22:00 +00:00
|
|
|
.ToList();
|
|
|
|
|
2022-02-27 20:37:23 +00:00
|
|
|
var sourceFolder = _diskProvider.GetParentFolder(localMovie.Path);
|
|
|
|
var files = _diskProvider.GetFiles(sourceFolder, folderSearchOption);
|
|
|
|
var managedFiles = _extraFileManagers.Select((i) => new List<string>()).ToArray();
|
2015-12-25 09:22:00 +00:00
|
|
|
|
2022-02-27 20:37:23 +00:00
|
|
|
foreach (var file in files)
|
2019-07-01 01:50:01 +00:00
|
|
|
{
|
2022-02-27 20:37:23 +00:00
|
|
|
var extension = Path.GetExtension(file);
|
|
|
|
var matchingExtension = wantedExtensions.FirstOrDefault(e => e.Equals(extension));
|
2015-12-25 09:22:00 +00:00
|
|
|
|
|
|
|
if (matchingExtension == null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-05-23 10:52:39 +00:00
|
|
|
for (var i = 0; i < _extraFileManagers.Count; i++)
|
2015-12-25 09:22:00 +00:00
|
|
|
{
|
2022-02-27 20:37:23 +00:00
|
|
|
if (_extraFileManagers[i].CanImportFile(localMovie, movieFile, file, extension, isReadOnly))
|
2015-12-25 09:22:00 +00:00
|
|
|
{
|
2022-02-27 20:37:23 +00:00
|
|
|
managedFiles[i].Add(file);
|
|
|
|
break;
|
2015-12-25 09:22:00 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-27 20:37:23 +00:00
|
|
|
}
|
|
|
|
|
2023-05-23 10:52:39 +00:00
|
|
|
for (var i = 0; i < _extraFileManagers.Count; i++)
|
2022-02-27 20:37:23 +00:00
|
|
|
{
|
|
|
|
_extraFileManagers[i].ImportFiles(localMovie, movieFile, managedFiles[i], isReadOnly);
|
2015-12-25 09:22:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-06 02:24:00 +00:00
|
|
|
private void CreateAfterMovieImport(Movie movie, MovieFile movieFile)
|
2018-04-01 19:48:22 +00:00
|
|
|
{
|
|
|
|
foreach (var extraFileManager in _extraFileManagers)
|
|
|
|
{
|
|
|
|
extraFileManager.CreateAfterMovieImport(movie, movieFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-25 09:22:00 +00:00
|
|
|
public void Handle(MediaCoversUpdatedEvent message)
|
2020-09-06 02:24:00 +00:00
|
|
|
{
|
|
|
|
if (message.Updated)
|
|
|
|
{
|
|
|
|
var movie = message.Movie;
|
|
|
|
|
|
|
|
foreach (var extraFileManager in _extraFileManagers)
|
|
|
|
{
|
|
|
|
extraFileManager.CreateAfterMediaCoverUpdate(movie);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Handle(MovieScannedEvent 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.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)
|
|
|
|
{
|
2020-09-06 02:24:00 +00:00
|
|
|
extraFileManager.CreateAfterMovieFolder(movie, message.MovieFolder);
|
2018-04-01 19:48:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 21:56:41 +00:00
|
|
|
movieFile.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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|