From 94c685a5caf30dbef5708dca0018ba8df436b6d2 Mon Sep 17 00:00:00 2001 From: TwentyNine78 Date: Fri, 2 Jul 2021 22:22:22 +0200 Subject: [PATCH] Fixed: issue with DownloadStationTaskProxyV2 if no destination folder specified in settings --- .../Clients/DownloadStation/DiskStationApi.cs | 1 + .../DownloadStation2SettingsLocationProxy.cs | 31 +++++++++++++++++ .../Proxies/DownloadStationTaskProxyV2.cs | 33 +++++++++++++++++++ ...ownloadStation2SettingsLocationResponse.cs | 7 ++++ 4 files changed, 72 insertions(+) create mode 100644 src/NzbDrone.Core/Download/Clients/DownloadStation/Proxies/DownloadStation2SettingsLocationProxy.cs create mode 100644 src/NzbDrone.Core/Download/Clients/DownloadStation/Responses/DownloadStation2SettingsLocationResponse.cs diff --git a/src/NzbDrone.Core/Download/Clients/DownloadStation/DiskStationApi.cs b/src/NzbDrone.Core/Download/Clients/DownloadStation/DiskStationApi.cs index 9b3cbfc33f..6c831fb3b7 100644 --- a/src/NzbDrone.Core/Download/Clients/DownloadStation/DiskStationApi.cs +++ b/src/NzbDrone.Core/Download/Clients/DownloadStation/DiskStationApi.cs @@ -7,6 +7,7 @@ public enum DiskStationApi DownloadStationInfo, DownloadStationTask, DownloadStation2Task, + DownloadStation2SettingsLocation, FileStationList, DSMInfo, } diff --git a/src/NzbDrone.Core/Download/Clients/DownloadStation/Proxies/DownloadStation2SettingsLocationProxy.cs b/src/NzbDrone.Core/Download/Clients/DownloadStation/Proxies/DownloadStation2SettingsLocationProxy.cs new file mode 100644 index 0000000000..7ca124e9cf --- /dev/null +++ b/src/NzbDrone.Core/Download/Clients/DownloadStation/Proxies/DownloadStation2SettingsLocationProxy.cs @@ -0,0 +1,31 @@ +using NLog; +using NzbDrone.Common.Cache; +using NzbDrone.Common.Http; +using NzbDrone.Core.Download.Clients.DownloadStation.Responses; + +namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies +{ + public interface IDownloadStation2SettingsLocationProxy + { + string GetDefaultDestination(DownloadStationSettings settings); + } + + public class DownloadStation2SettingsLocationProxy : DiskStationProxyBase, IDownloadStation2SettingsLocationProxy + { + public DownloadStation2SettingsLocationProxy(IHttpClient httpClient, ICacheManager cacheManager, Logger logger) + : base(DiskStationApi.DownloadStation2SettingsLocation, "SYNO.DownloadStation2.Settings.Location", httpClient, cacheManager, logger) + { + } + + public string GetDefaultDestination(DownloadStationSettings settings) + { + var info = GetApiInfo(settings); + + var requestBuilder = BuildRequest(settings, "get", info.MinVersion); + + var response = ProcessRequest(requestBuilder, "get default destination folder", settings); + + return response.Data.Default_Destination; + } + } +} diff --git a/src/NzbDrone.Core/Download/Clients/DownloadStation/Proxies/DownloadStationTaskProxyV2.cs b/src/NzbDrone.Core/Download/Clients/DownloadStation/Proxies/DownloadStationTaskProxyV2.cs index 5e5dc2a029..261f76e197 100644 --- a/src/NzbDrone.Core/Download/Clients/DownloadStation/Proxies/DownloadStationTaskProxyV2.cs +++ b/src/NzbDrone.Core/Download/Clients/DownloadStation/Proxies/DownloadStationTaskProxyV2.cs @@ -10,9 +10,12 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies { public class DownloadStationTaskProxyV2 : DiskStationProxyBase, IDownloadStationTaskProxy { + private readonly IDownloadStation2SettingsLocationProxy _defaultDestinationProxy; + public DownloadStationTaskProxyV2(IHttpClient httpClient, ICacheManager cacheManager, Logger logger) : base(DiskStationApi.DownloadStation2Task, "SYNO.DownloadStation2.Task", httpClient, cacheManager, logger) { + _defaultDestinationProxy = new DownloadStation2SettingsLocationProxy(httpClient, cacheManager, logger); } public bool IsApiSupported(DownloadStationSettings settings) @@ -32,6 +35,21 @@ public void AddTaskFromData(byte[] data, string filename, string downloadDirecto { requestBuilder.AddFormParameter("destination", $"\"{downloadDirectory}\""); } + else + { + _logger.Trace("No directory configured in settings; falling back to client default destination folder."); + string defaultDestination = _defaultDestinationProxy.GetDefaultDestination(settings); + + if (defaultDestination.IsNotNullOrWhiteSpace()) + { + _logger.Trace($"Default destination folder found: {defaultDestination}."); + requestBuilder.AddFormParameter("destination", $"\"{defaultDestination}\""); + } + else + { + _logger.Error("Unable to get default destination folder from DownloadStation."); + } + } requestBuilder.AddFormUpload("fileData", filename, data); @@ -50,6 +68,21 @@ public void AddTaskFromUrl(string url, string downloadDirectory, DownloadStation { requestBuilder.AddQueryParam("destination", downloadDirectory); } + else + { + _logger.Trace("No directory configured in settings; falling back to client default destination folder."); + string defaultDestination = _defaultDestinationProxy.GetDefaultDestination(settings); + + if (defaultDestination.IsNotNullOrWhiteSpace()) + { + _logger.Trace($"Default destination folder found: {defaultDestination}."); + requestBuilder.AddQueryParam("destination", $"\"{defaultDestination}\""); + } + else + { + _logger.Error("Unable to get default destination folder from DownloadStation."); + } + } ProcessRequest(requestBuilder, $"add task from url {url}", settings); } diff --git a/src/NzbDrone.Core/Download/Clients/DownloadStation/Responses/DownloadStation2SettingsLocationResponse.cs b/src/NzbDrone.Core/Download/Clients/DownloadStation/Responses/DownloadStation2SettingsLocationResponse.cs new file mode 100644 index 0000000000..70598e0502 --- /dev/null +++ b/src/NzbDrone.Core/Download/Clients/DownloadStation/Responses/DownloadStation2SettingsLocationResponse.cs @@ -0,0 +1,7 @@ +namespace NzbDrone.Core.Download.Clients.DownloadStation.Responses +{ + public class DownloadStation2SettingsLocationResponse + { + public string Default_Destination { get; set; } + } +}