2014-04-19 15:09:22 +00:00
|
|
|
|
using System;
|
2015-03-14 10:13:35 +00:00
|
|
|
|
using System.Collections.Generic;
|
2014-04-19 15:09:22 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2015-03-14 10:13:35 +00:00
|
|
|
|
using FluentValidation.Results;
|
|
|
|
|
using NLog;
|
2014-04-19 15:09:22 +00:00
|
|
|
|
using NzbDrone.Common.Disk;
|
2014-12-02 06:26:25 +00:00
|
|
|
|
using NzbDrone.Common.Extensions;
|
2014-04-19 15:09:22 +00:00
|
|
|
|
using NzbDrone.Common.Http;
|
2014-05-27 21:04:13 +00:00
|
|
|
|
using NzbDrone.Core.Configuration;
|
2015-03-14 10:13:35 +00:00
|
|
|
|
using NzbDrone.Core.MediaFiles;
|
2014-04-19 15:09:22 +00:00
|
|
|
|
using NzbDrone.Core.Organizer;
|
|
|
|
|
using NzbDrone.Core.Parser.Model;
|
2014-09-11 20:24:00 +00:00
|
|
|
|
using NzbDrone.Core.RemotePathMappings;
|
2014-04-19 15:09:22 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Download.Clients.UsenetBlackhole
|
|
|
|
|
{
|
2015-03-14 10:13:35 +00:00
|
|
|
|
public class UsenetBlackhole : UsenetClientBase<UsenetBlackholeSettings>
|
2014-04-19 15:09:22 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly IDiskScanService _diskScanService;
|
|
|
|
|
|
2014-07-04 20:27:21 +00:00
|
|
|
|
public UsenetBlackhole(IDiskScanService diskScanService,
|
2014-09-11 23:49:41 +00:00
|
|
|
|
IHttpClient httpClient,
|
2014-05-27 21:04:13 +00:00
|
|
|
|
IConfigService configService,
|
2014-07-04 20:27:21 +00:00
|
|
|
|
IDiskProvider diskProvider,
|
2014-09-11 20:24:00 +00:00
|
|
|
|
IRemotePathMappingService remotePathMappingService,
|
2014-04-19 15:09:22 +00:00
|
|
|
|
Logger logger)
|
2015-03-14 10:13:35 +00:00
|
|
|
|
: base(httpClient, configService, diskProvider, remotePathMappingService, logger)
|
2014-04-19 15:09:22 +00:00
|
|
|
|
{
|
|
|
|
|
_diskScanService = diskScanService;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-14 10:13:35 +00:00
|
|
|
|
protected override string AddFromNzbFile(RemoteEpisode remoteEpisode, string filename, byte[] fileContent)
|
2014-04-19 15:09:22 +00:00
|
|
|
|
{
|
|
|
|
|
var title = remoteEpisode.Release.Title;
|
|
|
|
|
|
2014-07-28 21:14:34 +00:00
|
|
|
|
title = FileNameBuilder.CleanFileName(title);
|
2014-04-19 15:09:22 +00:00
|
|
|
|
|
2015-03-14 10:13:35 +00:00
|
|
|
|
var filepath = Path.Combine(Settings.NzbFolder, title + ".nzb");
|
|
|
|
|
|
|
|
|
|
using (var stream = _diskProvider.OpenWriteStream(filepath))
|
|
|
|
|
{
|
|
|
|
|
stream.Write(fileContent, 0, fileContent.Length);
|
|
|
|
|
}
|
2014-04-19 15:09:22 +00:00
|
|
|
|
|
2015-03-14 10:13:35 +00:00
|
|
|
|
_logger.Debug("NZB Download succeeded, saved to: {0}", filepath);
|
2014-04-19 15:09:22 +00:00
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-25 16:22:53 +00:00
|
|
|
|
public override string Name
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return "Usenet Blackhole";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-19 15:09:22 +00:00
|
|
|
|
public override IEnumerable<DownloadClientItem> GetItems()
|
|
|
|
|
{
|
|
|
|
|
foreach (var folder in _diskProvider.GetDirectories(Settings.WatchFolder))
|
|
|
|
|
{
|
2014-07-28 21:14:34 +00:00
|
|
|
|
var title = FileNameBuilder.CleanFileName(Path.GetFileName(folder));
|
2014-04-19 15:09:22 +00:00
|
|
|
|
|
|
|
|
|
var files = _diskProvider.GetFiles(folder, SearchOption.AllDirectories);
|
|
|
|
|
|
|
|
|
|
var historyItem = new DownloadClientItem
|
|
|
|
|
{
|
|
|
|
|
DownloadClient = Definition.Name,
|
2014-12-19 00:26:42 +00:00
|
|
|
|
DownloadId = Definition.Name + "_" + Path.GetFileName(folder) + "_" + _diskProvider.FolderGetCreationTime(folder).Ticks,
|
2015-03-21 11:05:06 +00:00
|
|
|
|
Category = "sonarr",
|
2014-04-19 15:09:22 +00:00
|
|
|
|
Title = title,
|
|
|
|
|
|
|
|
|
|
TotalSize = files.Select(_diskProvider.GetFileSize).Sum(),
|
|
|
|
|
|
2014-10-12 22:00:03 +00:00
|
|
|
|
OutputPath = new OsPath(folder)
|
2014-04-19 15:09:22 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (files.Any(_diskProvider.IsFileLocked))
|
|
|
|
|
{
|
|
|
|
|
historyItem.Status = DownloadItemStatus.Downloading;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
historyItem.Status = DownloadItemStatus.Completed;
|
2014-05-24 10:48:55 +00:00
|
|
|
|
|
|
|
|
|
historyItem.RemainingTime = TimeSpan.Zero;
|
2014-04-19 15:09:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yield return historyItem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var videoFile in _diskScanService.GetVideoFiles(Settings.WatchFolder, false))
|
|
|
|
|
{
|
2014-07-28 21:14:34 +00:00
|
|
|
|
var title = FileNameBuilder.CleanFileName(Path.GetFileName(videoFile));
|
2014-04-19 15:09:22 +00:00
|
|
|
|
|
|
|
|
|
var historyItem = new DownloadClientItem
|
|
|
|
|
{
|
|
|
|
|
DownloadClient = Definition.Name,
|
2014-12-19 00:26:42 +00:00
|
|
|
|
DownloadId = Definition.Name + "_" + Path.GetFileName(videoFile) + "_" + _diskProvider.FileGetLastWrite(videoFile).Ticks,
|
2015-03-21 11:05:06 +00:00
|
|
|
|
Category = "sonarr",
|
2014-04-19 15:09:22 +00:00
|
|
|
|
Title = title,
|
|
|
|
|
|
|
|
|
|
TotalSize = _diskProvider.GetFileSize(videoFile),
|
|
|
|
|
|
2014-10-12 22:00:03 +00:00
|
|
|
|
OutputPath = new OsPath(videoFile)
|
2014-04-19 15:09:22 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (_diskProvider.IsFileLocked(videoFile))
|
|
|
|
|
{
|
|
|
|
|
historyItem.Status = DownloadItemStatus.Downloading;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
historyItem.Status = DownloadItemStatus.Completed;
|
2014-09-07 11:37:50 +00:00
|
|
|
|
historyItem.RemainingTime = TimeSpan.Zero;
|
2014-04-19 15:09:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yield return historyItem;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-23 01:17:48 +00:00
|
|
|
|
public override void RemoveItem(string downloadId, bool deleteData)
|
2014-04-19 15:09:22 +00:00
|
|
|
|
{
|
2015-01-19 22:25:04 +00:00
|
|
|
|
var downloadItem = GetItems().FirstOrDefault(v => v.DownloadId == downloadId);
|
|
|
|
|
|
|
|
|
|
if (downloadItem == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException(string.Format("Cannot remove DownloadItem {0} because it was not found.", downloadId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!deleteData)
|
|
|
|
|
{
|
|
|
|
|
throw new NotSupportedException("Blackhole cannot remove DownloadItem without deleting the data as well, ignoring.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var outputPath = downloadItem.OutputPath.FullPath;
|
|
|
|
|
|
|
|
|
|
if (_diskProvider.FileExists(outputPath))
|
|
|
|
|
{
|
|
|
|
|
_diskProvider.DeleteFile(outputPath);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_diskProvider.DeleteFolder(outputPath, true);
|
|
|
|
|
}
|
2014-04-19 15:09:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-06 05:55:38 +00:00
|
|
|
|
public override DownloadClientStatus GetStatus()
|
|
|
|
|
{
|
|
|
|
|
return new DownloadClientStatus
|
|
|
|
|
{
|
|
|
|
|
IsLocalhost = true,
|
2014-10-12 22:00:03 +00:00
|
|
|
|
OutputRootFolders = new List<OsPath> { new OsPath(Settings.WatchFolder) }
|
2014-06-06 05:55:38 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-09 22:11:57 +00:00
|
|
|
|
protected override void Test(List<ValidationFailure> failures)
|
2014-06-12 21:46:08 +00:00
|
|
|
|
{
|
2014-07-04 20:27:21 +00:00
|
|
|
|
failures.AddIfNotNull(TestFolder(Settings.NzbFolder, "NzbFolder"));
|
|
|
|
|
failures.AddIfNotNull(TestFolder(Settings.WatchFolder, "WatchFolder"));
|
2014-04-19 15:09:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|