2018-11-23 07:03:32 +00:00
|
|
|
using System.Collections.Generic;
|
2020-10-07 20:45:04 +00:00
|
|
|
using System.IO;
|
2020-05-26 01:55:10 +00:00
|
|
|
using System.Linq;
|
2020-10-07 20:45:04 +00:00
|
|
|
using System.Threading.Tasks;
|
2018-11-23 07:03:32 +00:00
|
|
|
using FluentValidation;
|
2019-12-22 22:08:53 +00:00
|
|
|
using Nancy;
|
2018-11-23 07:03:32 +00:00
|
|
|
using NzbDrone.Common.Extensions;
|
2020-05-26 01:55:10 +00:00
|
|
|
using NzbDrone.Core.Configuration;
|
2018-11-23 07:03:32 +00:00
|
|
|
using NzbDrone.Core.Datastore.Events;
|
2019-12-22 19:15:32 +00:00
|
|
|
using NzbDrone.Core.DecisionEngine.Specifications;
|
2020-05-26 01:55:10 +00:00
|
|
|
using NzbDrone.Core.Languages;
|
2018-11-23 07:03:32 +00:00
|
|
|
using NzbDrone.Core.MediaCover;
|
|
|
|
using NzbDrone.Core.MediaFiles;
|
|
|
|
using NzbDrone.Core.MediaFiles.Events;
|
2020-01-12 03:13:28 +00:00
|
|
|
using NzbDrone.Core.Messaging.Commands;
|
2018-11-23 07:03:32 +00:00
|
|
|
using NzbDrone.Core.Messaging.Events;
|
|
|
|
using NzbDrone.Core.Movies;
|
2020-01-12 03:13:28 +00:00
|
|
|
using NzbDrone.Core.Movies.Commands;
|
2018-11-23 07:03:32 +00:00
|
|
|
using NzbDrone.Core.Movies.Events;
|
2020-05-26 01:55:10 +00:00
|
|
|
using NzbDrone.Core.Movies.Translations;
|
2018-11-23 07:03:32 +00:00
|
|
|
using NzbDrone.Core.Validation;
|
2019-12-22 22:08:53 +00:00
|
|
|
using NzbDrone.Core.Validation.Paths;
|
2018-11-23 07:03:32 +00:00
|
|
|
using NzbDrone.SignalR;
|
|
|
|
using Radarr.Http;
|
2019-12-22 22:08:53 +00:00
|
|
|
using Radarr.Http.Extensions;
|
2018-11-23 07:03:32 +00:00
|
|
|
|
2019-12-03 01:36:18 +00:00
|
|
|
namespace Radarr.Api.V3.Movies
|
2018-11-23 07:03:32 +00:00
|
|
|
{
|
2019-12-22 21:24:11 +00:00
|
|
|
public class MovieModule : RadarrRestModuleWithSignalR<MovieResource, Movie>,
|
2018-11-23 07:03:32 +00:00
|
|
|
IHandle<MovieImportedEvent>,
|
|
|
|
IHandle<MovieFileDeletedEvent>,
|
2019-12-22 21:24:11 +00:00
|
|
|
IHandle<MovieUpdatedEvent>,
|
|
|
|
IHandle<MovieEditedEvent>,
|
2020-06-05 03:35:02 +00:00
|
|
|
IHandle<MoviesDeletedEvent>,
|
2018-11-23 07:03:32 +00:00
|
|
|
IHandle<MovieRenamedEvent>,
|
|
|
|
IHandle<MediaCoversUpdatedEvent>
|
|
|
|
{
|
2020-05-14 02:19:22 +00:00
|
|
|
private readonly IMovieService _moviesService;
|
2020-05-26 01:55:10 +00:00
|
|
|
private readonly IMovieTranslationService _movieTranslationService;
|
2020-05-14 02:19:22 +00:00
|
|
|
private readonly IAddMovieService _addMovieService;
|
2018-11-23 07:03:32 +00:00
|
|
|
private readonly IMapCoversToLocal _coverMapper;
|
2020-01-12 03:13:28 +00:00
|
|
|
private readonly IManageCommandQueue _commandQueueManager;
|
2019-12-22 19:15:32 +00:00
|
|
|
private readonly IUpgradableSpecification _qualityUpgradableSpecification;
|
2020-05-26 01:55:10 +00:00
|
|
|
private readonly IConfigService _configService;
|
2018-11-23 07:03:32 +00:00
|
|
|
|
|
|
|
public MovieModule(IBroadcastSignalRMessage signalRBroadcaster,
|
2020-10-07 20:45:04 +00:00
|
|
|
IMovieService moviesService,
|
|
|
|
IMovieTranslationService movieTranslationService,
|
|
|
|
IAddMovieService addMovieService,
|
|
|
|
IMapCoversToLocal coverMapper,
|
|
|
|
IManageCommandQueue commandQueueManager,
|
|
|
|
IUpgradableSpecification qualityUpgradableSpecification,
|
|
|
|
IConfigService configService,
|
|
|
|
RootFolderValidator rootFolderValidator,
|
|
|
|
MappedNetworkDriveValidator mappedNetworkDriveValidator,
|
|
|
|
MoviePathValidator moviesPathValidator,
|
|
|
|
MovieExistsValidator moviesExistsValidator,
|
|
|
|
MovieAncestorValidator moviesAncestorValidator,
|
|
|
|
SystemFolderValidator systemFolderValidator,
|
|
|
|
ProfileExistsValidator profileExistsValidator,
|
|
|
|
MovieFolderAsRootFolderValidator movieFolderAsRootFolderValidator)
|
2018-11-23 07:03:32 +00:00
|
|
|
: base(signalRBroadcaster)
|
|
|
|
{
|
|
|
|
_moviesService = moviesService;
|
2020-05-26 01:55:10 +00:00
|
|
|
_movieTranslationService = movieTranslationService;
|
2020-05-14 02:19:22 +00:00
|
|
|
_addMovieService = addMovieService;
|
2019-12-22 19:15:32 +00:00
|
|
|
_qualityUpgradableSpecification = qualityUpgradableSpecification;
|
2020-05-26 01:55:10 +00:00
|
|
|
_configService = configService;
|
2018-11-23 07:03:32 +00:00
|
|
|
_coverMapper = coverMapper;
|
2020-01-12 03:13:28 +00:00
|
|
|
_commandQueueManager = commandQueueManager;
|
2018-11-23 07:03:32 +00:00
|
|
|
|
|
|
|
GetResourceAll = AllMovie;
|
|
|
|
GetResourceById = GetMovie;
|
|
|
|
CreateResource = AddMovie;
|
|
|
|
UpdateResource = UpdateMovie;
|
|
|
|
DeleteResource = DeleteMovie;
|
|
|
|
|
|
|
|
SharedValidator.RuleFor(s => s.QualityProfileId).ValidId();
|
|
|
|
|
|
|
|
SharedValidator.RuleFor(s => s.Path)
|
|
|
|
.Cascade(CascadeMode.StopOnFirstFailure)
|
|
|
|
.IsValidPath()
|
|
|
|
.SetValidator(rootFolderValidator)
|
2020-03-09 02:49:00 +00:00
|
|
|
.SetValidator(mappedNetworkDriveValidator)
|
2018-11-23 07:03:32 +00:00
|
|
|
.SetValidator(moviesPathValidator)
|
|
|
|
.SetValidator(moviesAncestorValidator)
|
2020-03-09 02:49:00 +00:00
|
|
|
.SetValidator(systemFolderValidator)
|
2018-11-23 07:03:32 +00:00
|
|
|
.When(s => !s.Path.IsNullOrWhiteSpace());
|
|
|
|
|
|
|
|
SharedValidator.RuleFor(s => s.QualityProfileId).SetValidator(profileExistsValidator);
|
|
|
|
|
|
|
|
PostValidator.RuleFor(s => s.Path).IsValidPath().When(s => s.RootFolderPath.IsNullOrWhiteSpace());
|
2019-09-29 20:27:30 +00:00
|
|
|
PostValidator.RuleFor(s => s.RootFolderPath)
|
|
|
|
.IsValidPath()
|
|
|
|
.SetValidator(movieFolderAsRootFolderValidator)
|
|
|
|
.When(s => s.Path.IsNullOrWhiteSpace());
|
2020-08-29 03:56:13 +00:00
|
|
|
PostValidator.RuleFor(s => s.Title).NotEmpty().When(s => s.TmdbId <= 0);
|
2018-11-23 07:03:32 +00:00
|
|
|
PostValidator.RuleFor(s => s.TmdbId).NotNull().NotEmpty().SetValidator(moviesExistsValidator);
|
|
|
|
|
|
|
|
PutValidator.RuleFor(s => s.Path).IsValidPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<MovieResource> AllMovie()
|
|
|
|
{
|
2020-04-11 03:45:26 +00:00
|
|
|
var tmdbId = Request.GetIntegerQueryParameter("tmdbId");
|
|
|
|
var moviesResources = new List<MovieResource>();
|
2020-10-06 11:48:58 +00:00
|
|
|
var configLanguage = (Language)_configService.MovieInfoLanguage;
|
2020-04-11 03:45:26 +00:00
|
|
|
|
2020-10-07 20:45:04 +00:00
|
|
|
Dictionary<string, FileInfo> coverFileInfos = null;
|
|
|
|
|
2020-04-11 03:45:26 +00:00
|
|
|
if (tmdbId > 0)
|
|
|
|
{
|
2020-05-26 01:55:10 +00:00
|
|
|
var movie = _moviesService.FindByTmdbId(tmdbId);
|
|
|
|
|
2020-08-24 01:10:25 +00:00
|
|
|
if (movie != null)
|
|
|
|
{
|
2020-09-09 03:13:42 +00:00
|
|
|
var translations = _movieTranslationService.GetAllTranslationsForMovie(movie.Id);
|
2020-10-06 11:48:58 +00:00
|
|
|
var translation = GetMovieTranslation(translations, movie, configLanguage);
|
2020-08-24 01:10:25 +00:00
|
|
|
moviesResources.AddIfNotNull(movie.ToResource(_qualityUpgradableSpecification, translation));
|
|
|
|
}
|
2020-04-11 03:45:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-07 20:45:04 +00:00
|
|
|
var movieTask = Task.Run(() => _moviesService.GetAllMovies());
|
|
|
|
|
|
|
|
var translations = _movieTranslationService
|
|
|
|
.GetAllTranslationsForLanguage(configLanguage)
|
|
|
|
.ToDictionary(x => x.MovieId);
|
|
|
|
|
|
|
|
coverFileInfos = _coverMapper.GetCoverFileInfos();
|
|
|
|
|
|
|
|
var movies = movieTask.GetAwaiter().GetResult();
|
|
|
|
|
|
|
|
moviesResources = new List<MovieResource>(movies.Count);
|
2020-05-26 01:55:10 +00:00
|
|
|
|
|
|
|
foreach (var movie in movies)
|
|
|
|
{
|
2020-10-07 20:45:04 +00:00
|
|
|
var translation = GetTranslationFromDict(translations, movie, configLanguage);
|
|
|
|
var resource = movie.ToResource(_qualityUpgradableSpecification, translation);
|
|
|
|
_coverMapper.ConvertToLocalUrls(resource.Id, resource.Images, coverFileInfos);
|
|
|
|
moviesResources.Add(resource);
|
2020-05-26 01:55:10 +00:00
|
|
|
}
|
2020-04-11 03:45:26 +00:00
|
|
|
}
|
2018-11-23 07:03:32 +00:00
|
|
|
|
|
|
|
return moviesResources;
|
|
|
|
}
|
|
|
|
|
|
|
|
private MovieResource GetMovie(int id)
|
|
|
|
{
|
2020-05-26 01:55:10 +00:00
|
|
|
var movie = _moviesService.GetMovie(id);
|
|
|
|
return MapToResource(movie);
|
2018-11-23 07:03:32 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 01:55:10 +00:00
|
|
|
protected MovieResource MapToResource(Movie movie)
|
2018-11-23 07:03:32 +00:00
|
|
|
{
|
2020-05-26 01:55:10 +00:00
|
|
|
if (movie == null)
|
2019-12-22 22:08:53 +00:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2018-11-23 07:03:32 +00:00
|
|
|
|
2020-09-09 03:13:42 +00:00
|
|
|
var translations = _movieTranslationService.GetAllTranslationsForMovie(movie.Id);
|
2020-10-06 11:48:58 +00:00
|
|
|
var translation = GetMovieTranslation(translations, movie, (Language)_configService.MovieInfoLanguage);
|
2020-05-26 01:55:10 +00:00
|
|
|
|
|
|
|
var resource = movie.ToResource(_qualityUpgradableSpecification, translation);
|
2018-11-23 07:03:32 +00:00
|
|
|
MapCoversToLocal(resource);
|
|
|
|
|
|
|
|
return resource;
|
|
|
|
}
|
|
|
|
|
2020-10-06 11:48:58 +00:00
|
|
|
private MovieTranslation GetMovieTranslation(List<MovieTranslation> translations, Movie movie, Language configLanguage)
|
2020-09-09 03:13:42 +00:00
|
|
|
{
|
2020-10-06 11:48:58 +00:00
|
|
|
if (configLanguage == Language.Original)
|
2020-09-09 03:13:42 +00:00
|
|
|
{
|
|
|
|
return new MovieTranslation
|
|
|
|
{
|
|
|
|
Title = movie.OriginalTitle,
|
|
|
|
Overview = movie.Overview
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-06 11:48:58 +00:00
|
|
|
return translations.FirstOrDefault(t => t.Language == configLanguage && t.MovieId == movie.Id);
|
2020-09-09 03:13:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 20:45:04 +00:00
|
|
|
private MovieTranslation GetTranslationFromDict(Dictionary<int, MovieTranslation> translations, Movie movie, Language configLanguage)
|
|
|
|
{
|
|
|
|
if (configLanguage == Language.Original)
|
|
|
|
{
|
|
|
|
return new MovieTranslation
|
|
|
|
{
|
|
|
|
Title = movie.OriginalTitle,
|
|
|
|
Overview = movie.Overview
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
translations.TryGetValue(movie.Id, out var translation);
|
|
|
|
return translation;
|
|
|
|
}
|
|
|
|
|
2018-11-23 07:03:32 +00:00
|
|
|
private int AddMovie(MovieResource moviesResource)
|
|
|
|
{
|
2020-05-14 02:19:22 +00:00
|
|
|
var movie = _addMovieService.AddMovie(moviesResource.ToModel());
|
2018-11-23 07:03:32 +00:00
|
|
|
|
|
|
|
return movie.Id;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void UpdateMovie(MovieResource moviesResource)
|
|
|
|
{
|
2020-01-12 03:13:28 +00:00
|
|
|
var moveFiles = Request.GetBooleanQueryParameter("moveFiles");
|
|
|
|
var movie = _moviesService.GetMovie(moviesResource.Id);
|
|
|
|
|
|
|
|
if (moveFiles)
|
|
|
|
{
|
|
|
|
var sourcePath = movie.Path;
|
|
|
|
var destinationPath = moviesResource.Path;
|
|
|
|
|
|
|
|
_commandQueueManager.Push(new MoveMovieCommand
|
|
|
|
{
|
|
|
|
MovieId = movie.Id,
|
|
|
|
SourcePath = sourcePath,
|
|
|
|
DestinationPath = destinationPath,
|
|
|
|
Trigger = CommandTrigger.Manual
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var model = moviesResource.ToModel(movie);
|
2018-11-23 07:03:32 +00:00
|
|
|
|
2020-05-26 01:55:10 +00:00
|
|
|
var updatedMovie = _moviesService.UpdateMovie(model);
|
2020-09-09 03:13:42 +00:00
|
|
|
var translations = _movieTranslationService.GetAllTranslationsForMovie(movie.Id);
|
2020-10-06 11:48:58 +00:00
|
|
|
var translation = GetMovieTranslation(translations, movie, (Language)_configService.MovieInfoLanguage);
|
2018-11-23 07:03:32 +00:00
|
|
|
|
2020-05-26 01:55:10 +00:00
|
|
|
BroadcastResourceChange(ModelAction.Updated, updatedMovie.ToResource(_qualityUpgradableSpecification, translation));
|
2018-11-23 07:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void DeleteMovie(int id)
|
|
|
|
{
|
2020-09-04 02:50:56 +00:00
|
|
|
var addExclusion = Request.GetBooleanQueryParameter("addImportExclusion");
|
2018-11-23 07:03:32 +00:00
|
|
|
var deleteFiles = Request.GetBooleanQueryParameter("deleteFiles");
|
|
|
|
|
|
|
|
_moviesService.DeleteMovie(id, deleteFiles, addExclusion);
|
|
|
|
}
|
|
|
|
|
2020-10-07 20:45:04 +00:00
|
|
|
private void MapCoversToLocal(MovieResource movie)
|
2018-11-23 07:03:32 +00:00
|
|
|
{
|
2020-10-07 20:45:04 +00:00
|
|
|
_coverMapper.ConvertToLocalUrls(movie.Id, movie.Images);
|
2018-11-23 07:03:32 +00:00
|
|
|
}
|
2019-12-22 21:24:11 +00:00
|
|
|
|
2018-11-23 07:03:32 +00:00
|
|
|
public void Handle(MovieImportedEvent message)
|
|
|
|
{
|
2020-09-09 03:13:42 +00:00
|
|
|
var translations = _movieTranslationService.GetAllTranslationsForMovie(message.ImportedMovie.Movie.Id);
|
2020-10-06 11:48:58 +00:00
|
|
|
var translation = GetMovieTranslation(translations, message.ImportedMovie.Movie, (Language)_configService.MovieInfoLanguage);
|
2020-05-26 01:55:10 +00:00
|
|
|
BroadcastResourceChange(ModelAction.Updated, message.ImportedMovie.Movie.ToResource(_qualityUpgradableSpecification, translation));
|
2018-11-23 07:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Handle(MovieFileDeletedEvent message)
|
|
|
|
{
|
2019-12-22 22:08:53 +00:00
|
|
|
if (message.Reason == DeleteMediaFileReason.Upgrade)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2018-11-23 07:03:32 +00:00
|
|
|
|
|
|
|
BroadcastResourceChange(ModelAction.Updated, message.MovieFile.MovieId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Handle(MovieUpdatedEvent message)
|
|
|
|
{
|
2020-09-09 03:13:42 +00:00
|
|
|
var translations = _movieTranslationService.GetAllTranslationsForMovie(message.Movie.Id);
|
2020-10-06 11:48:58 +00:00
|
|
|
var translation = GetMovieTranslation(translations, message.Movie, (Language)_configService.MovieInfoLanguage);
|
2020-05-26 01:55:10 +00:00
|
|
|
BroadcastResourceChange(ModelAction.Updated, message.Movie.ToResource(_qualityUpgradableSpecification, translation));
|
2018-11-23 07:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Handle(MovieEditedEvent message)
|
|
|
|
{
|
2020-09-09 03:13:42 +00:00
|
|
|
var translations = _movieTranslationService.GetAllTranslationsForMovie(message.Movie.Id);
|
2020-10-06 11:48:58 +00:00
|
|
|
var translation = GetMovieTranslation(translations, message.Movie, (Language)_configService.MovieInfoLanguage);
|
2020-05-26 01:55:10 +00:00
|
|
|
BroadcastResourceChange(ModelAction.Updated, message.Movie.ToResource(_qualityUpgradableSpecification, translation));
|
2018-11-23 07:03:32 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 03:35:02 +00:00
|
|
|
public void Handle(MoviesDeletedEvent message)
|
2018-11-23 07:03:32 +00:00
|
|
|
{
|
2020-06-05 03:35:02 +00:00
|
|
|
foreach (var movie in message.Movies)
|
|
|
|
{
|
|
|
|
BroadcastResourceChange(ModelAction.Deleted, movie.Id);
|
|
|
|
}
|
2018-11-23 07:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Handle(MovieRenamedEvent message)
|
|
|
|
{
|
2020-09-09 03:13:42 +00:00
|
|
|
var translations = _movieTranslationService.GetAllTranslationsForMovie(message.Movie.Id);
|
2020-10-06 11:48:58 +00:00
|
|
|
var translation = GetMovieTranslation(translations, message.Movie, (Language)_configService.MovieInfoLanguage);
|
2020-05-26 01:55:10 +00:00
|
|
|
BroadcastResourceChange(ModelAction.Updated, message.Movie.ToResource(_qualityUpgradableSpecification, translation));
|
2018-11-23 07:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Handle(MediaCoversUpdatedEvent message)
|
|
|
|
{
|
2020-03-12 02:09:08 +00:00
|
|
|
if (message.Updated)
|
|
|
|
{
|
|
|
|
BroadcastResourceChange(ModelAction.Updated, message.Movie.Id);
|
|
|
|
}
|
2018-11-23 07:03:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|