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-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; }
|
2014-07-04 08:09:48 +00:00
|
|
|
|
public abstract ValidationResult Test();
|
2014-02-14 05:31:49 +00:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract string Download(RemoteEpisode remoteEpisode);
|
|
|
|
|
public abstract IEnumerable<DownloadClientItem> GetItems();
|
|
|
|
|
public abstract void RemoveItem(string id);
|
2014-04-01 20:07:41 +00:00
|
|
|
|
public abstract void 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-04 20:27:21 +00:00
|
|
|
|
protected ValidationFailure TestFolder(String folder, String propertyName, Boolean mustBeWritable = true)
|
|
|
|
|
{
|
|
|
|
|
if (!_diskProvider.FolderExists(folder))
|
|
|
|
|
{
|
|
|
|
|
return new ValidationFailure(propertyName, "Folder does not exist");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
return new ValidationFailure(propertyName, "Unable to write to folder");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2014-02-14 05:31:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|