2018-03-14 20:41:36 +00:00
using System.Collections.Generic ;
2013-11-16 09:22:31 +00:00
using System.Linq ;
2013-08-26 05:14:55 +00:00
using FluentValidation ;
2013-11-15 08:53:12 +00:00
using FluentValidation.Results ;
2019-12-22 22:08:53 +00:00
using Nancy.ModelBinding ;
2014-12-02 06:26:25 +00:00
using NzbDrone.Common.Extensions ;
2013-07-24 00:35:35 +00:00
using NzbDrone.Core.Organizer ;
2018-11-23 07:03:32 +00:00
using Radarr.Http ;
2013-07-24 00:35:35 +00:00
namespace NzbDrone.Api.Config
{
2018-11-23 07:03:32 +00:00
public class NamingConfigModule : RadarrRestModule < NamingConfigResource >
2013-07-24 00:35:35 +00:00
{
private readonly INamingConfigService _namingConfigService ;
2013-11-16 05:03:42 +00:00
private readonly IFilenameSampleService _filenameSampleService ;
private readonly IFilenameValidationService _filenameValidationService ;
2013-11-22 07:44:43 +00:00
private readonly IBuildFileNames _filenameBuilder ;
2013-07-24 00:35:35 +00:00
2014-02-16 09:56:12 +00:00
public NamingConfigModule ( INamingConfigService namingConfigService ,
2013-11-16 05:03:42 +00:00
IFilenameSampleService filenameSampleService ,
2013-11-22 07:44:43 +00:00
IFilenameValidationService filenameValidationService ,
IBuildFileNames filenameBuilder )
2013-07-24 00:35:35 +00:00
: base ( "config/naming" )
{
_namingConfigService = namingConfigService ;
2013-11-16 05:03:42 +00:00
_filenameSampleService = filenameSampleService ;
_filenameValidationService = filenameValidationService ;
2013-11-22 07:44:43 +00:00
_filenameBuilder = filenameBuilder ;
2013-07-24 00:35:35 +00:00
GetResourceSingle = GetNamingConfig ;
2013-08-28 00:16:24 +00:00
GetResourceById = GetNamingConfig ;
2013-07-24 00:35:35 +00:00
UpdateResource = UpdateNamingConfig ;
2019-12-22 22:08:53 +00:00
Get ( "/samples" , x = > GetExamples ( this . Bind < NamingConfigResource > ( ) ) ) ;
2013-08-26 05:14:55 +00:00
2015-01-01 06:40:37 +00:00
SharedValidator . RuleFor ( c = > c . MultiEpisodeStyle ) . InclusiveBetween ( 0 , 5 ) ;
2017-01-07 00:32:19 +00:00
SharedValidator . RuleFor ( c = > c . StandardMovieFormat ) . ValidMovieFormat ( ) ;
SharedValidator . RuleFor ( c = > c . MovieFolderFormat ) . ValidMovieFolderFormat ( ) ;
2013-07-24 00:35:35 +00:00
}
2013-08-26 05:14:55 +00:00
private void UpdateNamingConfig ( NamingConfigResource resource )
2013-07-24 00:35:35 +00:00
{
2016-03-25 00:56:29 +00:00
var nameSpec = resource . ToModel ( ) ;
2013-11-15 08:53:12 +00:00
ValidateFormatResult ( nameSpec ) ;
_namingConfigService . Save ( nameSpec ) ;
2013-07-24 00:35:35 +00:00
}
private NamingConfigResource GetNamingConfig ( )
{
2013-11-22 07:44:43 +00:00
var nameSpec = _namingConfigService . GetConfig ( ) ;
2016-03-25 00:56:29 +00:00
var resource = nameSpec . ToResource ( ) ;
2013-11-22 16:59:03 +00:00
2017-01-07 00:32:19 +00:00
if ( resource . StandardMovieFormat . IsNotNullOrWhiteSpace ( ) )
2013-11-22 16:59:03 +00:00
{
2016-03-25 00:56:29 +00:00
var basicConfig = _filenameBuilder . GetBasicNamingConfig ( nameSpec ) ;
basicConfig . AddToResource ( resource ) ;
2013-11-22 16:59:03 +00:00
}
2013-11-22 07:44:43 +00:00
return resource ;
2013-08-26 05:14:55 +00:00
}
2013-08-28 00:16:24 +00:00
private NamingConfigResource GetNamingConfig ( int id )
{
return GetNamingConfig ( ) ;
}
2019-08-28 21:43:55 +00:00
private object GetExamples ( NamingConfigResource config )
2013-08-26 05:14:55 +00:00
{
2016-03-25 00:56:29 +00:00
var nameSpec = config . ToModel ( ) ;
2013-08-26 05:14:55 +00:00
var sampleResource = new NamingSampleResource ( ) ;
2017-01-07 03:51:15 +00:00
var movieSampleResult = _filenameSampleService . GetMovieSample ( nameSpec ) ;
2017-01-07 09:42:21 +00:00
sampleResource . MovieExample = nameSpec . StandardMovieFormat . IsNullOrWhiteSpace ( )
2017-01-07 03:51:15 +00:00
? "Invalid Format"
: movieSampleResult . FileName ;
sampleResource . MovieFolderExample = nameSpec . MovieFolderFormat . IsNullOrWhiteSpace ( )
2014-04-17 18:26:48 +00:00
? "Invalid format"
2017-01-07 03:51:15 +00:00
: _filenameSampleService . GetMovieFolderSample ( nameSpec ) ;
2014-04-15 14:16:49 +00:00
2019-08-28 21:43:55 +00:00
return sampleResource ;
2013-07-24 00:35:35 +00:00
}
2013-11-10 04:20:45 +00:00
2013-11-15 08:53:12 +00:00
private void ValidateFormatResult ( NamingConfig nameSpec )
{
2017-01-07 03:51:15 +00:00
var movieSampleResult = _filenameSampleService . GetMovieSample ( nameSpec ) ;
2017-01-07 00:32:19 +00:00
2017-01-07 09:42:21 +00:00
//var standardMovieValidationResult = _filenameValidationService.ValidateMovieFilename(movieSampleResult); For now, let's hope the user is not stupid enough :/
2013-11-16 05:03:42 +00:00
var validationFailures = new List < ValidationFailure > ( ) ;
2013-11-15 08:53:12 +00:00
2017-01-07 09:42:21 +00:00
//validationFailures.AddIfNotNull(standardMovieValidationResult);
2013-11-16 09:22:31 +00:00
if ( validationFailures . Any ( ) )
{
2014-11-29 19:17:20 +00:00
throw new ValidationException ( validationFailures . DistinctBy ( v = > v . PropertyName ) . ToArray ( ) ) ;
2013-11-16 09:22:31 +00:00
}
2013-11-15 08:53:12 +00:00
}
2013-07-24 00:35:35 +00:00
}
2013-11-16 05:03:42 +00:00
}