2014-02-14 05:31:49 +00:00
using System ;
2014-07-05 14:21:44 +00:00
using System.IO ;
using System.Linq ;
2014-02-14 05:31:49 +00:00
using System.Collections.Generic ;
2014-07-05 14:21:44 +00:00
using NzbDrone.Common ;
2014-07-04 20:27:21 +00:00
using NzbDrone.Common.Disk ;
2014-04-19 15:09:22 +00:00
using NzbDrone.Core.Indexers ;
using NzbDrone.Core.Parser ;
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-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 ;
protected readonly IParsingService _parsingService ;
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-07-04 20:27:21 +00:00
protected DownloadClientBase ( IConfigService configService , IDiskProvider diskProvider , IParsingService parsingService , 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-04-19 15:09:22 +00:00
_parsingService = parsingService ;
_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 ( ) ;
public abstract void RemoveItem ( string id ) ;
2014-07-19 17:37:06 +00:00
public abstract String RetryDownload ( string id ) ;
2014-06-06 05:55:38 +00:00
public abstract DownloadClientStatus GetStatus ( ) ;
2014-04-19 15:09:22 +00:00
protected RemoteEpisode GetRemoteEpisode ( String title )
{
var parsedEpisodeInfo = Parser . Parser . ParseTitle ( title ) ;
if ( parsedEpisodeInfo = = null ) return null ;
var remoteEpisode = _parsingService . Map ( parsedEpisodeInfo , 0 ) ;
if ( remoteEpisode . Series = = null ) return null ;
return remoteEpisode ;
}
2014-07-04 20:27:21 +00:00
2014-07-05 14:21:44 +00:00
protected void RemapStorage ( DownloadClientItem downloadClientItem , String remotePath , String localPath )
{
if ( downloadClientItem . OutputPath . IsNullOrWhiteSpace ( ) | | localPath . IsNullOrWhiteSpace ( ) )
{
return ;
}
remotePath = remotePath . TrimEnd ( '/' , '\\' ) ;
localPath = localPath . TrimEnd ( '/' , '\\' ) ;
if ( downloadClientItem . OutputPath . StartsWith ( remotePath ) )
{
downloadClientItem . OutputPath = localPath + downloadClientItem . OutputPath . Substring ( remotePath . Length ) ;
downloadClientItem . OutputPath = downloadClientItem . OutputPath . Replace ( Path . AltDirectorySeparatorChar , Path . DirectorySeparatorChar ) ;
}
}
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
}
}