2014-02-14 05:31:49 +00:00
using System ;
2015-05-01 22:19:15 +00:00
using System.Linq ;
2014-02-14 05:31:49 +00:00
using System.Collections.Generic ;
2015-05-01 22:19:15 +00:00
using FluentValidation.Results ;
using NLog ;
2014-07-04 20:27:21 +00:00
using NzbDrone.Common.Disk ;
2015-05-01 22:19:15 +00:00
using NzbDrone.Common.Extensions ;
using NzbDrone.Core.Configuration ;
2014-04-19 15:09:22 +00:00
using NzbDrone.Core.Indexers ;
2014-02-14 05:31:49 +00:00
using NzbDrone.Core.Parser.Model ;
2015-05-01 22:19:15 +00:00
using NzbDrone.Core.RemotePathMappings ;
2014-02-14 05:31:49 +00:00
using NzbDrone.Core.ThingiProvider ;
2014-07-09 22:11:57 +00:00
using NzbDrone.Core.Validation ;
2014-02-14 05:31:49 +00:00
namespace NzbDrone.Core.Download
{
2014-04-19 15:09:22 +00:00
public abstract class DownloadClientBase < TSettings > : IDownloadClient
where TSettings : IProviderConfig , new ( )
2014-02-14 05:31:49 +00:00
{
2014-05-27 21:04:13 +00:00
protected readonly IConfigService _configService ;
2014-07-04 20:27:21 +00:00
protected readonly IDiskProvider _diskProvider ;
2014-09-11 20:24:00 +00:00
protected readonly IRemotePathMappingService _remotePathMappingService ;
2014-04-19 15:09:22 +00:00
protected readonly Logger _logger ;
2015-04-25 16:22:53 +00:00
public abstract string Name { get ; }
2015-04-25 16:02:17 +00:00
2016-12-09 06:54:15 +00:00
public Type ConfigContract = > typeof ( TSettings ) ;
2014-02-14 05:31:49 +00:00
2016-12-09 06:54:15 +00:00
public virtual ProviderMessage Message = > null ;
2015-06-28 21:06:03 +00:00
2017-06-06 20:40:44 +00:00
public IEnumerable < ProviderDefinition > GetDefaultDefinitions ( )
{
return new List < ProviderDefinition > ( ) ;
}
2014-02-14 05:31:49 +00:00
public ProviderDefinition Definition { get ; set ; }
2016-08-10 18:45:48 +00:00
public virtual object RequestAction ( string action , IDictionary < string , string > query ) { return null ; }
2015-03-29 05:30:58 +00:00
2016-12-09 06:54:15 +00:00
protected TSettings Settings = > ( TSettings ) Definition . Settings ;
2014-02-14 05:31:49 +00:00
2014-10-13 21:11:35 +00:00
protected DownloadClientBase ( IConfigService configService ,
IDiskProvider diskProvider ,
IRemotePathMappingService remotePathMappingService ,
Logger logger )
2014-04-19 15:09:22 +00:00
{
2014-05-27 21:04:13 +00:00
_configService = configService ;
2014-07-04 20:27:21 +00:00
_diskProvider = diskProvider ;
2014-09-11 20:24:00 +00:00
_remotePathMappingService = remotePathMappingService ;
2014-04-19 15:09:22 +00:00
_logger = logger ;
}
2014-02-14 05:31:49 +00:00
public override string ToString ( )
{
return GetType ( ) . Name ;
}
2014-04-19 15:09:22 +00:00
public abstract DownloadProtocol Protocol
{
get ;
}
2017-01-02 18:20:32 +00:00
2015-10-03 17:45:26 +00:00
public abstract string Download ( RemoteEpisode remoteEpisode ) ;
2014-04-19 15:09:22 +00:00
public abstract IEnumerable < DownloadClientItem > GetItems ( ) ;
2014-12-23 01:17:48 +00:00
public abstract void RemoveItem ( string downloadId , bool deleteData ) ;
2014-06-06 05:55:38 +00:00
public abstract DownloadClientStatus GetStatus ( ) ;
2014-04-19 15:09:22 +00:00
2015-05-01 22:19:15 +00:00
protected virtual void DeleteItemData ( string downloadId )
{
if ( downloadId . IsNullOrWhiteSpace ( ) )
{
return ;
}
var item = GetItems ( ) . FirstOrDefault ( v = > v . DownloadId = = downloadId ) ;
if ( item = = null )
{
_logger . Trace ( "DownloadItem {0} in {1} history not found, skipping delete data." , downloadId , Name ) ;
return ;
}
2015-05-05 15:42:41 +00:00
if ( item . OutputPath . IsEmpty )
2015-05-01 22:19:15 +00:00
{
_logger . Trace ( "[{0}] Doesn't have an outputPath, skipping delete data." , item . Title ) ;
return ;
}
try
{
if ( _diskProvider . FolderExists ( item . OutputPath . FullPath ) )
{
_logger . Debug ( "[{0}] Deleting folder '{1}'." , item . Title , item . OutputPath ) ;
_diskProvider . DeleteFolder ( item . OutputPath . FullPath , true ) ;
}
else if ( _diskProvider . FileExists ( item . OutputPath . FullPath ) )
{
_logger . Debug ( "[{0}] Deleting file '{1}'." , item . Title , item . OutputPath ) ;
_diskProvider . DeleteFile ( item . OutputPath . FullPath ) ;
}
else
{
_logger . Trace ( "[{0}] File or folder '{1}' doesn't exist, skipping cleanup." , item . Title , item . OutputPath ) ;
}
}
catch ( Exception ex )
{
2016-02-11 21:13:42 +00:00
_logger . Warn ( ex , string . Format ( "[{0}] Error occurred while trying to delete data from '{1}'." , item . Title , item . OutputPath ) ) ;
2015-05-01 22:19:15 +00:00
}
}
2014-07-09 22:11:57 +00:00
public ValidationResult Test ( )
{
var failures = new List < ValidationFailure > ( ) ;
2017-04-08 11:36:16 +00:00
2014-07-09 22:11:57 +00:00
try
{
Test ( failures ) ;
}
catch ( Exception ex )
{
2016-02-11 21:13:42 +00:00
_logger . Error ( ex , "Test aborted due to exception" ) ;
2014-07-09 22:11:57 +00:00
failures . Add ( new ValidationFailure ( string . Empty , "Test was aborted due to an error: " + ex . Message ) ) ;
}
return new ValidationResult ( failures ) ;
}
protected abstract void Test ( List < ValidationFailure > failures ) ;
2015-10-03 17:45:26 +00:00
protected ValidationFailure TestFolder ( string folder , string propertyName , bool mustBeWritable = true )
2014-07-04 20:27:21 +00:00
{
if ( ! _diskProvider . FolderExists ( folder ) )
{
2014-07-09 22:11:57 +00:00
return new NzbDroneValidationFailure ( propertyName , "Folder does not exist" )
{
2017-01-03 19:24:55 +00:00
DetailedDescription = string . Format ( "The folder you specified does not exist or is inaccessible. Please verify the folder permissions for the user account '{0}', which is used to execute Radarr." , Environment . UserName )
2014-07-09 22:11:57 +00:00
} ;
2014-07-04 20:27:21 +00:00
}
2015-01-21 22:57:35 +00:00
if ( mustBeWritable & & ! _diskProvider . FolderWritable ( folder ) )
2014-07-04 20:27:21 +00:00
{
2015-01-21 22:57:35 +00:00
_logger . Error ( "Folder '{0}' is not writable." , folder ) ;
return new NzbDroneValidationFailure ( propertyName , "Unable to write to folder" )
2014-07-04 20:27:21 +00:00
{
2017-01-03 19:24:55 +00:00
DetailedDescription = string . Format ( "The folder you specified is not writable. Please verify the folder permissions for the user account '{0}', which is used to execute Radarr." , Environment . UserName )
2015-01-21 22:57:35 +00:00
} ;
2014-07-04 20:27:21 +00:00
}
return null ;
}
2017-01-02 18:20:32 +00:00
public abstract string Download ( RemoteMovie remoteMovie ) ;
2014-02-14 05:31:49 +00:00
}
}