-
-
Notifications
You must be signed in to change notification settings - Fork 242
/
Aria2.cs
264 lines (220 loc) · 9.26 KB
/
Aria2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using FluentValidation.Results;
using NLog;
using NzbDrone.Common.Disk;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http;
using NzbDrone.Core.Blocklisting;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.MediaFiles.TorrentInfo;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.RemotePathMappings;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Download.Clients.Aria2
{
public class Aria2 : TorrentClientBase<Aria2Settings>
{
private readonly IAria2Proxy _proxy;
public override string Name => "Aria2";
public Aria2(IAria2Proxy proxy,
ITorrentFileInfoReader torrentFileInfoReader,
IHttpClient httpClient,
IConfigService configService,
IDiskProvider diskProvider,
IRemotePathMappingService remotePathMappingService,
IBlocklistService blocklistService,
Logger logger)
: base(torrentFileInfoReader, httpClient, configService, diskProvider, remotePathMappingService, blocklistService, logger)
{
_proxy = proxy;
}
protected override string AddFromMagnetLink(RemoteAlbum remoteAlbum, string hash, string magnetLink)
{
var gid = _proxy.AddMagnet(Settings, magnetLink);
var tries = 10;
var retryDelay = 500;
// Wait a bit for the magnet to be resolved.
if (!WaitForTorrent(gid, hash, tries, retryDelay))
{
_logger.Warn($"Aria2 could not add magnent within {tries * retryDelay / 1000} seconds, download may remain stuck: {magnetLink}.");
return hash;
}
_logger.Debug($"Aria2 AddFromMagnetLink '{hash}' -> '{gid}'");
return hash;
}
protected override string AddFromTorrentFile(RemoteAlbum remoteAlbum, string hash, string filename, byte[] fileContent)
{
var gid = _proxy.AddTorrent(Settings, fileContent);
var tries = 10;
var retryDelay = 500;
// Wait a bit for the magnet to be resolved.
if (!WaitForTorrent(gid, hash, tries, retryDelay))
{
_logger.Warn($"Aria2 could not add torrent within {tries * retryDelay / 1000} seconds, download may remain stuck: {filename}.");
return hash;
}
return hash;
}
public override IEnumerable<DownloadClientItem> GetItems()
{
var torrents = _proxy.GetTorrents(Settings);
foreach (var torrent in torrents)
{
var firstFile = torrent.Files?.FirstOrDefault();
// skip metadata download
if (firstFile?.Path?.Contains("[METADATA]") == true)
{
continue;
}
var completedLength = long.Parse(torrent.CompletedLength);
var totalLength = long.Parse(torrent.TotalLength);
var uploadedLength = long.Parse(torrent.UploadLength);
var downloadSpeed = long.Parse(torrent.DownloadSpeed);
var status = DownloadItemStatus.Failed;
var title = torrent.Bittorrent?.Name ?? "";
switch (torrent.Status)
{
case "active":
if (completedLength == totalLength)
{
status = DownloadItemStatus.Completed;
}
else
{
status = DownloadItemStatus.Downloading;
}
break;
case "waiting":
status = DownloadItemStatus.Queued;
break;
case "paused":
status = DownloadItemStatus.Paused;
break;
case "error":
status = DownloadItemStatus.Failed;
break;
case "complete":
status = DownloadItemStatus.Completed;
break;
case "removed":
status = DownloadItemStatus.Failed;
break;
}
_logger.Trace($"- aria2 getstatus hash:'{torrent.InfoHash}' gid:'{torrent.Gid}' status:'{status}' total:{totalLength} completed:'{completedLength}'");
var outputPath = _remotePathMappingService.RemapRemoteToLocal(Settings.Host, new OsPath(GetOutputPath(torrent)));
yield return new DownloadClientItem
{
CanMoveFiles = false,
CanBeRemoved = torrent.Status == "complete",
Category = null,
DownloadClientInfo = DownloadClientItemClientInfo.FromDownloadClient(this),
DownloadId = torrent.InfoHash?.ToUpper(),
IsEncrypted = false,
Message = torrent.ErrorMessage,
OutputPath = outputPath,
RemainingSize = totalLength - completedLength,
RemainingTime = downloadSpeed == 0 ? (TimeSpan?)null : new TimeSpan(0, 0, (int)((totalLength - completedLength) / downloadSpeed)),
Removed = torrent.Status == "removed",
SeedRatio = totalLength > 0 ? (double)uploadedLength / totalLength : 0,
Status = status,
Title = title,
TotalSize = totalLength,
};
}
}
public override void RemoveItem(DownloadClientItem item, bool deleteData)
{
// Aria2 doesn't support file deletion: https://github.com/aria2/aria2/issues/728
var hash = item.DownloadId.ToLower();
var aria2Item = _proxy.GetTorrents(Settings).FirstOrDefault(t => t.InfoHash?.ToLower() == hash);
if (aria2Item == null)
{
_logger.Error($"Aria2 could not find infoHash '{hash}' for deletion.");
return;
}
_logger.Debug($"Aria2 removing hash:'{hash}' gid:'{aria2Item.Gid}'");
if (aria2Item.Status == "complete" || aria2Item.Status == "error" || aria2Item.Status == "removed")
{
if (!_proxy.RemoveCompletedTorrent(Settings, aria2Item.Gid))
{
_logger.Error($"Aria2 error while deleting {hash}.");
return;
}
}
else
{
if (!_proxy.RemoveTorrent(Settings, aria2Item.Gid))
{
_logger.Error($"Aria2 error while deleting {hash}.");
return;
}
}
if (deleteData)
{
DeleteItemData(item);
}
}
public override DownloadClientInfo GetStatus()
{
var destDir = _proxy.GetGlobals(Settings);
return new DownloadClientInfo
{
IsLocalhost = Settings.Host.Contains("127.0.0.1") || Settings.Host.Contains("localhost"),
OutputRootFolders = new List<OsPath> { _remotePathMappingService.RemapRemoteToLocal(Settings.Host, new OsPath(destDir["dir"])) }
};
}
private bool WaitForTorrent(string gid, string hash, int tries, int retryDelay)
{
for (var i = 0; i < tries; i++)
{
var found = _proxy.GetFromGID(Settings, gid);
if (found?.InfoHash?.ToLower() == hash?.ToLower())
{
return true;
}
Thread.Sleep(retryDelay);
}
_logger.Debug("Could not find hash {0} in {1} tries at {2} ms intervals.", hash, tries, retryDelay);
return false;
}
protected override void Test(List<ValidationFailure> failures)
{
failures.AddIfNotNull(TestConnection());
if (failures.HasErrors())
{
return;
}
}
private ValidationFailure TestConnection()
{
try
{
var version = _proxy.GetVersion(Settings);
if (new Version(version) < new Version("1.34.0"))
{
return new ValidationFailure(string.Empty, "Aria2 version should be at least 1.34.0. Version reported is {0}", version);
}
}
catch (Exception ex)
{
_logger.Error(ex, "Failed to test Aria2");
return new NzbDroneValidationFailure("Host", "Unable to connect to Aria2")
{
DetailedDescription = ex.Message
};
}
return null;
}
private string GetOutputPath(Aria2Status torrent)
{
if (torrent.Files.Length == 1)
{
return torrent.Files.First().Path;
}
return torrent.Files.Select(f => f.Path).ToList().GetLongestCommonPath();
}
}
}