Correct Path using qbittorrent
This commit is contained in:
parent
047326a30f
commit
fcbd9d8096
3 changed files with 80 additions and 42 deletions
|
|
@ -144,7 +144,7 @@ http://127.0.0.1:6800/jsonrpc.")]
|
||||||
public String? DownloadStationPassword { get; set; } = null;
|
public String? DownloadStationPassword { get; set; } = null;
|
||||||
|
|
||||||
[DisplayName("Synology Download Station Download Path")]
|
[DisplayName("Synology Download Station Download Path")]
|
||||||
[Description("The root path to doawnload the file on the Synology DownloadStation host, if empty use the default DownloadStation path but won't create catagory folders.")]
|
[Description("The root path to doawnload the file on the Synology DownloadStation host, if empty use the default DownloadStation path.")]
|
||||||
public String? DownloadStationDownloadPath { get; set; } = null;
|
public String? DownloadStationDownloadPath { get; set; } = null;
|
||||||
|
|
||||||
[DisplayName("Log level")]
|
[DisplayName("Log level")]
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@
|
||||||
<PackageReference Include="Serilog" Version="4.0.1" />
|
<PackageReference Include="Serilog" Version="4.0.1" />
|
||||||
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
|
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
|
||||||
<PackageReference Include="SharpCompress" Version="0.38.0" />
|
<PackageReference Include="SharpCompress" Version="0.38.0" />
|
||||||
<PackageReference Include="Synology.Api.Client" Version="0.2.66" />
|
<PackageReference Include="Synology.Api.Client" Version="0.2.68" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
|
using Serilog;
|
||||||
using Serilog;
|
|
||||||
using Synology.Api.Client;
|
using Synology.Api.Client;
|
||||||
using Synology.Api.Client.Apis.DownloadStation.Task.Models;
|
using Synology.Api.Client.Apis.DownloadStation.Task.Models;
|
||||||
|
using Synology.Api.Client.Apis.FileStation.List.Models;
|
||||||
|
|
||||||
namespace RdtClient.Service.Services.Downloaders;
|
namespace RdtClient.Service.Services.Downloaders;
|
||||||
|
|
||||||
|
|
@ -15,36 +15,55 @@ public class DownloadStationDownloader : IDownloader
|
||||||
private readonly SynologyClient _synologyClient;
|
private readonly SynologyClient _synologyClient;
|
||||||
|
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private readonly String _uri;
|
|
||||||
private readonly String _filePath;
|
private readonly String _filePath;
|
||||||
|
private readonly String _uri;
|
||||||
private readonly String? _remotePath;
|
private readonly String? _remotePath;
|
||||||
|
|
||||||
private String? _gid;
|
private String? _gid;
|
||||||
private DownloadStationDownloader(String? gid, String uri, String filePath, String downloadPath, String? category)
|
private DownloadStationDownloader(String? gid, String uri, String? remotePath, String filePath, String downloadPath, String? category, SynologyClient synologyClient)
|
||||||
{
|
{
|
||||||
_logger = Log.ForContext<DownloadStationDownloader>();
|
_logger = Log.ForContext<DownloadStationDownloader>();
|
||||||
_logger.Debug($"Instantiated new DownloadStation Downloader for URI {uri} to filePath {filePath} and downloadPath {downloadPath} and GID {gid}");
|
_logger.Debug($"Instantiated new DownloadStation Downloader for URI {uri} to filePath {filePath} and downloadPath {downloadPath} and GID {gid}");
|
||||||
|
|
||||||
_gid = gid;
|
_gid = gid;
|
||||||
_uri = uri;
|
|
||||||
_filePath = filePath;
|
_filePath = filePath;
|
||||||
|
_uri = uri;
|
||||||
_remotePath = !String.IsNullOrWhiteSpace(Settings.Get.DownloadClient.DownloadStationDownloadPath)
|
_remotePath = remotePath;
|
||||||
? String.IsNullOrWhiteSpace(category)
|
_synologyClient = synologyClient;
|
||||||
? Path.Combine(ToCurrentPath(Settings.Get.DownloadClient.DownloadStationDownloadPath))
|
|
||||||
: Path.Combine(ToCurrentPath(Settings.Get.DownloadClient.DownloadStationDownloadPath), category)
|
|
||||||
: null;
|
|
||||||
|
|
||||||
_synologyClient = new SynologyClient(Settings.Get.DownloadClient.DownloadStationUrl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<DownloadStationDownloader> Init(String? gid, String uri, String filePath, String downloadPath, String? category)
|
public static async Task<DownloadStationDownloader> Init(String? gid, String uri, String filePath, String downloadPath, String? category)
|
||||||
{
|
{
|
||||||
var result = new DownloadStationDownloader(gid, uri, filePath, downloadPath, category);
|
var synologyClient = new SynologyClient(Settings.Get.DownloadClient.DownloadStationUrl);
|
||||||
if (Settings.Get.DownloadClient.DownloadStationUsername != null && Settings.Get.DownloadClient.DownloadStationPassword != null)
|
if (Settings.Get.DownloadClient.DownloadStationUsername != null && Settings.Get.DownloadClient.DownloadStationPassword != null)
|
||||||
await result._synologyClient.LoginAsync(Settings.Get.DownloadClient.DownloadStationUsername, Settings.Get.DownloadClient.DownloadStationPassword);
|
await synologyClient.LoginAsync(Settings.Get.DownloadClient.DownloadStationUsername, Settings.Get.DownloadClient.DownloadStationPassword);
|
||||||
|
|
||||||
return result;
|
String? remotePath = null;
|
||||||
|
String? rootPath;
|
||||||
|
if (!String.IsNullOrWhiteSpace(Settings.Get.DownloadClient.DownloadStationDownloadPath))
|
||||||
|
{
|
||||||
|
rootPath = Settings.Get.DownloadClient.DownloadStationDownloadPath;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var config = await synologyClient.DownloadStationApi().InfoEndpoint().GetConfigAsync();
|
||||||
|
rootPath = config.DefaultDestination;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rootPath != null)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (String.IsNullOrWhiteSpace(category))
|
||||||
|
{
|
||||||
|
remotePath = Path.Combine(rootPath, downloadPath).Replace('\\', '/');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
remotePath = Path.Combine(downloadPath, category, downloadPath).Replace('\\', '/');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new DownloadStationDownloader(gid, uri, remotePath, filePath, downloadPath, category, synologyClient);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Cancel()
|
public async Task Cancel()
|
||||||
|
|
@ -63,7 +82,8 @@ public class DownloadStationDownloader : IDownloader
|
||||||
|
|
||||||
public async Task<string> Download()
|
public async Task<string> Download()
|
||||||
{
|
{
|
||||||
var path = _remotePath != null ? ToUnixPath(_remotePath) : null;
|
var path = Path.GetDirectoryName(_remotePath)?.Replace('\\', '/') ?? throw new($"Invalid file path {_filePath}");
|
||||||
|
if (!path.StartsWith('/')) path = '/' + path;
|
||||||
_logger.Debug($"Starting download of {_uri}, writing to path: {path}");
|
_logger.Debug($"Starting download of {_uri}, writing to path: {path}");
|
||||||
|
|
||||||
if (_gid != null)
|
if (_gid != null)
|
||||||
|
|
@ -84,33 +104,43 @@ public class DownloadStationDownloader : IDownloader
|
||||||
_logger.Debug($"Download with ID {_gid} found in DownloadStation");
|
_logger.Debug($"Download with ID {_gid} found in DownloadStation");
|
||||||
return _gid;
|
return _gid;
|
||||||
}
|
}
|
||||||
|
try
|
||||||
var createResult = await _synologyClient
|
|
||||||
.DownloadStationApi()
|
|
||||||
.TaskEndpoint()
|
|
||||||
.CreateAsync(new DownloadStationTaskCreateRequest(_uri, path));
|
|
||||||
|
|
||||||
_logger.Debug($"Added download to DownloadStation, received ID {_gid}");
|
|
||||||
|
|
||||||
if (createResult.Success)
|
|
||||||
{
|
{
|
||||||
_gid = await GetGidFromUri();
|
await CheckFolderOrCreate(path);
|
||||||
if (_gid != null)
|
|
||||||
|
var createResult = await _synologyClient
|
||||||
|
.DownloadStationApi()
|
||||||
|
.TaskEndpoint()
|
||||||
|
.CreateAsync(new DownloadStationTaskCreateRequest(_uri, path.Substring(1)));
|
||||||
|
|
||||||
|
_logger.Debug($"Added download to DownloadStation, received ID {_gid}");
|
||||||
|
|
||||||
|
if (createResult.Success)
|
||||||
{
|
{
|
||||||
_logger.Debug($"Download with ID {_gid} found in DownloadStation");
|
_gid = await GetGidFromUri();
|
||||||
return _gid;
|
if (_gid != null)
|
||||||
|
{
|
||||||
|
_logger.Debug($"Download with ID {_gid} found in DownloadStation");
|
||||||
|
return _gid;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
retryCount++;
|
||||||
|
_logger.Debug($"Task not found in DownloadStation after creat Sucess. Retrying {retryCount}/{RetryCount}");
|
||||||
|
await Task.Delay(retryCount * 1000);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
retryCount++;
|
retryCount++;
|
||||||
_logger.Debug($"Task not found in DownloadStation after creat Sucess. Retrying {retryCount}/{RetryCount}");
|
_logger.Debug($"Error starting download: {createResult.Error.Code}. Retrying {retryCount}/{RetryCount}");
|
||||||
await Task.Delay(retryCount * 1000);
|
await Task.Delay(retryCount * 1000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
retryCount++;
|
retryCount++;
|
||||||
_logger.Debug($"Error starting download: {createResult.Error.Code}. Retrying {retryCount}/{RetryCount}");
|
_logger.Debug($"Error starting download: {e.Message}. Retrying {retryCount}/{RetryCount}");
|
||||||
await Task.Delay(retryCount * 1000);
|
await Task.Delay(retryCount * 1000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -165,14 +195,22 @@ public class DownloadStationDownloader : IDownloader
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string ToUnixPath(string path)
|
private async Task CheckFolderOrCreate(string path)
|
||||||
{
|
{
|
||||||
return path.Replace(Path.DirectorySeparatorChar, '/');
|
if (path != null)
|
||||||
}
|
{
|
||||||
|
try
|
||||||
private static string ToCurrentPath(string path)
|
{
|
||||||
{
|
await _synologyClient.FileStationApi().ListEndpoint()
|
||||||
return path.Replace('/', Path.DirectorySeparatorChar);
|
.ListAsync(new FileStationListRequest(path));
|
||||||
|
}
|
||||||
|
catch // if not exists create it
|
||||||
|
{
|
||||||
|
await _synologyClient.FileStationApi().CreateFolderEndpoint()
|
||||||
|
.CreateAsync(new[] { path }, true);
|
||||||
|
//if error handle by the caller
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<DownloadStationTask?> GetTask()
|
private async Task<DownloadStationTask?> GetTask()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue