2014-02-14 05:31:49 +00:00
using System ;
2014-07-05 14:21:44 +00:00
using System.IO ;
2014-02-14 05:31:49 +00:00
using System.Collections.Generic ;
2014-07-04 20:27:21 +00:00
using NzbDrone.Common.Disk ;
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 ;
using NzbDrone.Core.ThingiProvider ;
2014-05-27 21:04:13 +00:00
using NzbDrone.Core.Configuration ;
2014-04-19 15:09:22 +00:00
using NLog ;
2014-07-05 14:21:44 +00:00
using FluentValidation.Results ;
2014-07-09 22:11:57 +00:00
using NzbDrone.Core.Validation ;
2014-09-11 20:24:00 +00:00
using NzbDrone.Core.RemotePathMappings ;
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 ;
2014-02-14 05:31:49 +00:00
public Type ConfigContract
{
get
{
return typeof ( TSettings ) ;
}
}
public IEnumerable < ProviderDefinition > DefaultDefinitions
{
get
{
return new List < ProviderDefinition > ( ) ;
}
}
public ProviderDefinition Definition { get ; set ; }
protected TSettings Settings
{
get
{
return ( TSettings ) Definition . Settings ;
}
}
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 ;
}
2014-07-19 17:37:06 +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
2014-07-09 22:11:57 +00:00
public ValidationResult Test ( )
{
var failures = new List < ValidationFailure > ( ) ;
try
{
Test ( failures ) ;
}
catch ( Exception ex )
{
_logger . ErrorException ( "Test aborted due to exception" , ex ) ;
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 ) ;
2014-07-04 20:27:21 +00:00
protected ValidationFailure TestFolder ( String folder , String propertyName , Boolean mustBeWritable = true )
{
if ( ! _diskProvider . FolderExists ( folder ) )
{
2014-07-09 22:11:57 +00:00
return new NzbDroneValidationFailure ( propertyName , "Folder does not exist" )
{
DetailedDescription = "The folder you specified does not exist or is inaccessible. Please verify the folder permissions for the user account that is used to execute NzbDrone."
} ;
2014-07-04 20:27:21 +00:00
}
if ( mustBeWritable )
{
try
{
var testPath = Path . Combine ( folder , "drone_test.txt" ) ;
_diskProvider . WriteAllText ( testPath , DateTime . Now . ToString ( ) ) ;
_diskProvider . DeleteFile ( testPath ) ;
}
catch ( Exception ex )
{
_logger . ErrorException ( ex . Message , ex ) ;
2014-07-09 22:11:57 +00:00
return new NzbDroneValidationFailure ( propertyName , "Unable to write to folder" )
{
DetailedDescription = "The folder you specified is not writable. Please verify the folder permissions for the user account that is used to execute NzbDrone."
} ;
2014-07-04 20:27:21 +00:00
}
}
return null ;
}
2014-02-14 05:31:49 +00:00
}
}