mirror of
https://github.com/Sonarr/Sonarr.git
synced 2024-12-16 11:37:58 +02:00
Aria2 fixes
Fixed: Removing completed downloads from Aria2 Fixed: Return correct path for Aria2 downloads in a job folder Fixed: Seeding torrents in Aria2 are treated as finished downloading Closes #4648
This commit is contained in:
parent
77fdebc366
commit
1d8b711eda
@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using NzbDrone.Common.Disk;
|
using NzbDrone.Common.Disk;
|
||||||
using NzbDrone.Common.EnsureThat;
|
using NzbDrone.Common.EnsureThat;
|
||||||
@ -232,6 +233,33 @@ public static string GetAncestorPath(this string path, string ancestorName)
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static string GetLongestCommonPath(this List<string> paths)
|
||||||
|
{
|
||||||
|
var firstPath = paths.First();
|
||||||
|
var length = firstPath.Length;
|
||||||
|
|
||||||
|
for (int i = 1; i < paths.Count; i++)
|
||||||
|
{
|
||||||
|
var path = paths[i];
|
||||||
|
|
||||||
|
length = Math.Min(length, path.Length);
|
||||||
|
|
||||||
|
for (int characterIndex = 0; characterIndex < length; characterIndex++)
|
||||||
|
{
|
||||||
|
if (path[characterIndex] != firstPath[characterIndex])
|
||||||
|
{
|
||||||
|
length = characterIndex;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var substring = firstPath.Substring(0, length);
|
||||||
|
var lastSeparatorIndex = substring.LastIndexOfAny(new[] { '/', '\\' });
|
||||||
|
|
||||||
|
return substring.Substring(0, lastSeparatorIndex);
|
||||||
|
}
|
||||||
|
|
||||||
public static string GetAppDataPath(this IAppFolderInfo appFolderInfo)
|
public static string GetAppDataPath(this IAppFolderInfo appFolderInfo)
|
||||||
{
|
{
|
||||||
return appFolderInfo.AppDataFolder;
|
return appFolderInfo.AppDataFolder;
|
||||||
|
@ -83,12 +83,12 @@ public override IEnumerable<DownloadClientItem> GetItems()
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
long completedLength = long.Parse(torrent.CompletedLength);
|
var completedLength = long.Parse(torrent.CompletedLength);
|
||||||
long totalLength = long.Parse(torrent.TotalLength);
|
var totalLength = long.Parse(torrent.TotalLength);
|
||||||
long uploadedLength = long.Parse(torrent.UploadLength);
|
var uploadedLength = long.Parse(torrent.UploadLength);
|
||||||
long downloadSpeed = long.Parse(torrent.DownloadSpeed);
|
var downloadSpeed = long.Parse(torrent.DownloadSpeed);
|
||||||
|
|
||||||
var sta = DownloadItemStatus.Failed;
|
var status = DownloadItemStatus.Failed;
|
||||||
var title = "";
|
var title = "";
|
||||||
|
|
||||||
if(torrent.Bittorrent?.ContainsKey("info") == true && ((XmlRpcStruct)torrent.Bittorrent["info"]).ContainsKey("name"))
|
if(torrent.Bittorrent?.ContainsKey("info") == true && ((XmlRpcStruct)torrent.Bittorrent["info"]).ContainsKey("name"))
|
||||||
@ -99,42 +99,52 @@ public override IEnumerable<DownloadClientItem> GetItems()
|
|||||||
switch (torrent.Status)
|
switch (torrent.Status)
|
||||||
{
|
{
|
||||||
case "active":
|
case "active":
|
||||||
sta = DownloadItemStatus.Downloading;
|
if (completedLength == totalLength)
|
||||||
|
{
|
||||||
|
status = DownloadItemStatus.Completed;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
status = DownloadItemStatus.Downloading;
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case "waiting":
|
case "waiting":
|
||||||
sta = DownloadItemStatus.Queued;
|
status = DownloadItemStatus.Queued;
|
||||||
break;
|
break;
|
||||||
case "paused":
|
case "paused":
|
||||||
sta = DownloadItemStatus.Paused;
|
status = DownloadItemStatus.Paused;
|
||||||
break;
|
break;
|
||||||
case "error":
|
case "error":
|
||||||
sta = DownloadItemStatus.Failed;
|
status = DownloadItemStatus.Failed;
|
||||||
break;
|
break;
|
||||||
case "complete":
|
case "complete":
|
||||||
sta = DownloadItemStatus.Completed;
|
status = DownloadItemStatus.Completed;
|
||||||
break;
|
break;
|
||||||
case "removed":
|
case "removed":
|
||||||
sta = DownloadItemStatus.Failed;
|
status = DownloadItemStatus.Failed;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
_logger.Debug($"- aria2 getstatus hash:'{torrent.InfoHash}' gid:'{torrent.Gid}' sta:'{sta}' tot:{totalLength} comp:'{completedLength}'");
|
_logger.Trace($"- aria2 getstatus hash:'{torrent.InfoHash}' gid:'{torrent.Gid}' status:'{status}' total:{totalLength} completed:'{completedLength}'");
|
||||||
|
|
||||||
yield return new DownloadClientItem()
|
var outputPath = _remotePathMappingService.RemapRemoteToLocal(Settings.Host, new OsPath(GetOutputPath(torrent)));
|
||||||
|
|
||||||
|
yield return new DownloadClientItem
|
||||||
{
|
{
|
||||||
CanMoveFiles = false,
|
CanMoveFiles = false,
|
||||||
CanBeRemoved = true,
|
CanBeRemoved = torrent.Status == "complete",
|
||||||
Category = null,
|
Category = null,
|
||||||
DownloadClientInfo = DownloadClientItemClientInfo.FromDownloadClient(this),
|
DownloadClientInfo = DownloadClientItemClientInfo.FromDownloadClient(this),
|
||||||
DownloadId = torrent.InfoHash?.ToUpper(),
|
DownloadId = torrent.InfoHash?.ToUpper(),
|
||||||
IsEncrypted = false,
|
IsEncrypted = false,
|
||||||
Message = torrent.ErrorMessage,
|
Message = torrent.ErrorMessage,
|
||||||
OutputPath = _remotePathMappingService.RemapRemoteToLocal(Settings.Host, new OsPath(torrent.Dir)),
|
OutputPath = outputPath,
|
||||||
RemainingSize = totalLength - completedLength,
|
RemainingSize = totalLength - completedLength,
|
||||||
RemainingTime = downloadSpeed == 0 ? (TimeSpan?)null : new TimeSpan(0,0, (int)((totalLength - completedLength) / downloadSpeed)),
|
RemainingTime = downloadSpeed == 0 ? (TimeSpan?)null : new TimeSpan(0,0, (int)((totalLength - completedLength) / downloadSpeed)),
|
||||||
Removed = torrent.Status == "removed",
|
Removed = torrent.Status == "removed",
|
||||||
SeedRatio = totalLength > 0 ? (double)uploadedLength / totalLength : 0,
|
SeedRatio = totalLength > 0 ? (double)uploadedLength / totalLength : 0,
|
||||||
Status = sta,
|
Status = status,
|
||||||
Title = title,
|
Title = title,
|
||||||
TotalSize = totalLength,
|
TotalSize = totalLength,
|
||||||
};
|
};
|
||||||
@ -143,7 +153,7 @@ public override IEnumerable<DownloadClientItem> GetItems()
|
|||||||
|
|
||||||
public override void RemoveItem(DownloadClientItem item, bool deleteData)
|
public override void RemoveItem(DownloadClientItem item, bool deleteData)
|
||||||
{
|
{
|
||||||
//Aria2 doesn't support file deletion: https://github.com/aria2/aria2/issues/728
|
// Aria2 doesn't support file deletion: https://github.com/aria2/aria2/issues/728
|
||||||
var hash = item.DownloadId.ToLower();
|
var hash = item.DownloadId.ToLower();
|
||||||
var aria2Item = _proxy.GetTorrents(Settings).FirstOrDefault(t => t.InfoHash?.ToLower() == hash);
|
var aria2Item = _proxy.GetTorrents(Settings).FirstOrDefault(t => t.InfoHash?.ToLower() == hash);
|
||||||
|
|
||||||
@ -155,11 +165,24 @@ public override void RemoveItem(DownloadClientItem item, bool deleteData)
|
|||||||
|
|
||||||
_logger.Debug($"Aria2 removing hash:'{hash}' gid:'{aria2Item.Gid}'");
|
_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))
|
if (!_proxy.RemoveTorrent(Settings, aria2Item.Gid))
|
||||||
{
|
{
|
||||||
_logger.Error($"Aria2 error while deleting {hash}.");
|
_logger.Error($"Aria2 error while deleting {hash}.");
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (deleteData)
|
if (deleteData)
|
||||||
{
|
{
|
||||||
@ -230,5 +253,15 @@ private ValidationFailure TestConnection()
|
|||||||
|
|
||||||
return null;
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,8 +13,9 @@ public interface IAria2Proxy
|
|||||||
string AddMagnet(Aria2Settings settings, string magnet);
|
string AddMagnet(Aria2Settings settings, string magnet);
|
||||||
string AddTorrent(Aria2Settings settings, byte[] torrent);
|
string AddTorrent(Aria2Settings settings, byte[] torrent);
|
||||||
bool RemoveTorrent(Aria2Settings settings, string gid);
|
bool RemoveTorrent(Aria2Settings settings, string gid);
|
||||||
|
bool RemoveCompletedTorrent(Aria2Settings settings, string gid);
|
||||||
Dictionary<string, string> GetGlobals(Aria2Settings settings);
|
Dictionary<string, string> GetGlobals(Aria2Settings settings);
|
||||||
Aria2Status[] GetTorrents(Aria2Settings settings);
|
List<Aria2Status> GetTorrents(Aria2Settings settings);
|
||||||
Aria2Status GetFromGID(Aria2Settings settings, string gid);
|
Aria2Status GetFromGID(Aria2Settings settings, string gid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,6 +33,9 @@ public interface IAria2 : IXmlRpcProxy
|
|||||||
[XmlRpcMethod("aria2.forceRemove")]
|
[XmlRpcMethod("aria2.forceRemove")]
|
||||||
string Remove(string token, string gid);
|
string Remove(string token, string gid);
|
||||||
|
|
||||||
|
[XmlRpcMethod("aria2.removeDownloadResult")]
|
||||||
|
string RemoveResult(string token, string gid);
|
||||||
|
|
||||||
[XmlRpcMethod("aria2.tellStatus")]
|
[XmlRpcMethod("aria2.tellStatus")]
|
||||||
Aria2Status GetFromGid(string token, string gid);
|
Aria2Status GetFromGid(string token, string gid);
|
||||||
|
|
||||||
@ -39,13 +43,13 @@ public interface IAria2 : IXmlRpcProxy
|
|||||||
XmlRpcStruct GetGlobalOption(string token);
|
XmlRpcStruct GetGlobalOption(string token);
|
||||||
|
|
||||||
[XmlRpcMethod("aria2.tellActive")]
|
[XmlRpcMethod("aria2.tellActive")]
|
||||||
Aria2Status[] GetActives(string token);
|
Aria2Status[] GetActive(string token);
|
||||||
|
|
||||||
[XmlRpcMethod("aria2.tellWaiting")]
|
[XmlRpcMethod("aria2.tellWaiting")]
|
||||||
Aria2Status[] GetWaitings(string token, int offset, int num);
|
Aria2Status[] GetWaiting(string token, int offset, int num);
|
||||||
|
|
||||||
[XmlRpcMethod("aria2.tellStopped")]
|
[XmlRpcMethod("aria2.tellStopped")]
|
||||||
Aria2Status[] GetStoppeds(string token, int offset, int num);
|
Aria2Status[] GetStopped(string token, int offset, int num);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Aria2Proxy : IAria2Proxy
|
public class Aria2Proxy : IAria2Proxy
|
||||||
@ -69,68 +73,68 @@ private string GetURL(Aria2Settings settings)
|
|||||||
|
|
||||||
public string GetVersion(Aria2Settings settings)
|
public string GetVersion(Aria2Settings settings)
|
||||||
{
|
{
|
||||||
_logger.Debug("> aria2.getVersion");
|
_logger.Trace("> aria2.getVersion");
|
||||||
|
|
||||||
var client = BuildClient(settings);
|
var client = BuildClient(settings);
|
||||||
var version = ExecuteRequest(() => client.GetVersion(GetToken(settings)));
|
var version = ExecuteRequest(() => client.GetVersion(GetToken(settings)));
|
||||||
|
|
||||||
_logger.Debug("< aria2.getVersion");
|
_logger.Trace("< aria2.getVersion");
|
||||||
|
|
||||||
return version.Version;
|
return version.Version;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Aria2Status GetFromGID(Aria2Settings settings, string gid)
|
public Aria2Status GetFromGID(Aria2Settings settings, string gid)
|
||||||
{
|
{
|
||||||
_logger.Debug("> aria2.tellStatus");
|
_logger.Trace("> aria2.tellStatus");
|
||||||
|
|
||||||
var client = BuildClient(settings);
|
var client = BuildClient(settings);
|
||||||
var found = ExecuteRequest(() => client.GetFromGid(GetToken(settings), gid));
|
var found = ExecuteRequest(() => client.GetFromGid(GetToken(settings), gid));
|
||||||
|
|
||||||
_logger.Debug("< aria2.tellStatus");
|
_logger.Trace("< aria2.tellStatus");
|
||||||
|
|
||||||
return found;
|
return found;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Aria2Status[] GetTorrents(Aria2Settings settings)
|
public List<Aria2Status> GetTorrents(Aria2Settings settings)
|
||||||
{
|
{
|
||||||
_logger.Debug("> aria2.tellActive");
|
_logger.Trace("> aria2.tellActive");
|
||||||
|
|
||||||
var client = BuildClient(settings);
|
var client = BuildClient(settings);
|
||||||
|
|
||||||
var actives = ExecuteRequest(() => client.GetActives(GetToken(settings)));
|
var active = ExecuteRequest(() => client.GetActive(GetToken(settings)));
|
||||||
|
|
||||||
_logger.Debug("< aria2.tellActive");
|
_logger.Trace("< aria2.tellActive");
|
||||||
|
|
||||||
_logger.Debug("> aria2.tellWaiting");
|
_logger.Trace("> aria2.tellWaiting");
|
||||||
|
|
||||||
var waitings = ExecuteRequest(() => client.GetWaitings(GetToken(settings), 1, 10*1024));
|
var waiting = ExecuteRequest(() => client.GetWaiting(GetToken(settings), 0, 10*1024));
|
||||||
|
|
||||||
_logger.Debug("< aria2.tellWaiting");
|
_logger.Trace("< aria2.tellWaiting");
|
||||||
|
|
||||||
_logger.Debug("> aria2.tellStopped");
|
_logger.Trace("> aria2.tellStopped");
|
||||||
|
|
||||||
var stoppeds = ExecuteRequest(() => client.GetStoppeds(GetToken(settings), 1, 10*1024));
|
var stopped = ExecuteRequest(() => client.GetStopped(GetToken(settings), 0, 10*1024));
|
||||||
|
|
||||||
_logger.Debug("< aria2.tellStopped");
|
_logger.Trace("< aria2.tellStopped");
|
||||||
|
|
||||||
var ret = new List<Aria2Status>();
|
var items = new List<Aria2Status>();
|
||||||
|
|
||||||
ret.AddRange(actives);
|
items.AddRange(active);
|
||||||
ret.AddRange(waitings);
|
items.AddRange(waiting);
|
||||||
ret.AddRange(stoppeds);
|
items.AddRange(stopped);
|
||||||
|
|
||||||
return ret.ToArray();
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Dictionary<string, string> GetGlobals(Aria2Settings settings)
|
public Dictionary<string, string> GetGlobals(Aria2Settings settings)
|
||||||
{
|
{
|
||||||
_logger.Debug("> aria2.getGlobalOption");
|
_logger.Trace("> aria2.getGlobalOption");
|
||||||
|
|
||||||
var client = BuildClient(settings);
|
var client = BuildClient(settings);
|
||||||
var options = ExecuteRequest(() => client.GetGlobalOption(GetToken(settings)));
|
var options = ExecuteRequest(() => client.GetGlobalOption(GetToken(settings)));
|
||||||
|
|
||||||
_logger.Debug("< aria2.getGlobalOption");
|
_logger.Trace("< aria2.getGlobalOption");
|
||||||
|
|
||||||
var ret = new Dictionary<string, string>();
|
var ret = new Dictionary<string, string>();
|
||||||
|
|
||||||
@ -144,40 +148,52 @@ public Dictionary<string, string> GetGlobals(Aria2Settings settings)
|
|||||||
|
|
||||||
public string AddMagnet(Aria2Settings settings, string magnet)
|
public string AddMagnet(Aria2Settings settings, string magnet)
|
||||||
{
|
{
|
||||||
_logger.Debug("> aria2.addUri");
|
_logger.Trace("> aria2.addUri");
|
||||||
|
|
||||||
var client = BuildClient(settings);
|
var client = BuildClient(settings);
|
||||||
var gid = ExecuteRequest(() => client.AddUri(GetToken(settings), new[] { magnet }));
|
var gid = ExecuteRequest(() => client.AddUri(GetToken(settings), new[] { magnet }));
|
||||||
|
|
||||||
_logger.Debug("< aria2.addUri");
|
_logger.Trace("< aria2.addUri");
|
||||||
|
|
||||||
return gid;
|
return gid;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string AddTorrent(Aria2Settings settings, byte[] torrent)
|
public string AddTorrent(Aria2Settings settings, byte[] torrent)
|
||||||
{
|
{
|
||||||
_logger.Debug("> aria2.addTorrent");
|
_logger.Trace("> aria2.addTorrent");
|
||||||
|
|
||||||
var client = BuildClient(settings);
|
var client = BuildClient(settings);
|
||||||
var gid = ExecuteRequest(() => client.AddTorrent(GetToken(settings), torrent));
|
var gid = ExecuteRequest(() => client.AddTorrent(GetToken(settings), torrent));
|
||||||
|
|
||||||
_logger.Debug("< aria2.addTorrent");
|
_logger.Trace("< aria2.addTorrent");
|
||||||
|
|
||||||
return gid;
|
return gid;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool RemoveTorrent(Aria2Settings settings, string gid)
|
public bool RemoveTorrent(Aria2Settings settings, string gid)
|
||||||
{
|
{
|
||||||
_logger.Debug("> aria2.forceRemove");
|
_logger.Trace("> aria2.forceRemove");
|
||||||
|
|
||||||
var client = BuildClient(settings);
|
var client = BuildClient(settings);
|
||||||
var gidres = ExecuteRequest(() => client.Remove(GetToken(settings), gid));
|
var gidres = ExecuteRequest(() => client.Remove(GetToken(settings), gid));
|
||||||
|
|
||||||
_logger.Debug("< aria2.forceRemove");
|
_logger.Trace("< aria2.forceRemove");
|
||||||
|
|
||||||
return gid == gidres;
|
return gid == gidres;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool RemoveCompletedTorrent(Aria2Settings settings, string gid)
|
||||||
|
{
|
||||||
|
_logger.Trace("> aria2.removeDownloadResult");
|
||||||
|
|
||||||
|
var client = BuildClient(settings);
|
||||||
|
var result = ExecuteRequest(() => client.RemoveResult(GetToken(settings), gid));
|
||||||
|
|
||||||
|
_logger.Trace("< aria2.removeDownloadResult");
|
||||||
|
|
||||||
|
return result == "OK";
|
||||||
|
}
|
||||||
|
|
||||||
private IAria2 BuildClient(Aria2Settings settings)
|
private IAria2 BuildClient(Aria2Settings settings)
|
||||||
{
|
{
|
||||||
var client = XmlRpcProxyGen.Create<IAria2>();
|
var client = XmlRpcProxyGen.Create<IAria2>();
|
||||||
|
Loading…
Reference in New Issue
Block a user