From 9035870600587b6bf2c689e148d2f5c77e85a9e7 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 27 Jun 2021 18:19:35 -0500 Subject: implement upload --- .../API/Controllers/TransfersController.cs | 12 ++- src/slskd/Integrations/FTP/FTPClient.cs | 105 +++++++++++++++++++++ src/slskd/Integrations/FTP/FTPClientFactory.cs | 53 +++++++++++ src/slskd/Integrations/FTP/FTPService.cs | 61 ------------ src/slskd/Integrations/FTP/IFTPClient.cs | 27 ++++++ src/slskd/Integrations/FTP/IFTPClientFactory.cs | 26 +++++ src/slskd/Integrations/FTP/IFTPService.cs | 26 ----- src/slskd/Startup.cs | 4 +- 8 files changed, 225 insertions(+), 89 deletions(-) create mode 100644 src/slskd/Integrations/FTP/FTPClient.cs create mode 100644 src/slskd/Integrations/FTP/FTPClientFactory.cs delete mode 100644 src/slskd/Integrations/FTP/FTPService.cs create mode 100644 src/slskd/Integrations/FTP/IFTPClient.cs create mode 100644 src/slskd/Integrations/FTP/IFTPClientFactory.cs delete mode 100644 src/slskd/Integrations/FTP/IFTPService.cs (limited to 'src') diff --git a/src/slskd/Application/Transfer/API/Controllers/TransfersController.cs b/src/slskd/Application/Transfer/API/Controllers/TransfersController.cs index f7274157..8131aa8c 100644 --- a/src/slskd/Application/Transfer/API/Controllers/TransfersController.cs +++ b/src/slskd/Application/Transfer/API/Controllers/TransfersController.cs @@ -25,6 +25,7 @@ namespace slskd.Transfer.API using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; + using slskd.Integrations.FTP; using Soulseek; /// @@ -43,19 +44,23 @@ namespace slskd.Transfer.API /// /// /// + /// public TransfersController( Microsoft.Extensions.Options.IOptionsSnapshot options, ISoulseekClient client, - ITransferTracker tracker) + ITransferTracker tracker, + IFTPClient ftpClient) { Client = client; Tracker = tracker; Options = options.Value; + FTP = ftpClient; } private Options Options { get; } private ISoulseekClient Client { get; } private ITransferTracker Tracker { get; } + private IFTPClient FTP { get; } /// /// Cancels the specified download. @@ -129,6 +134,11 @@ namespace slskd.Transfer.API }, progressUpdated: (e) => Tracker.AddOrUpdate(e, cts)), cts.Token); MoveFile(request.Filename, Options.Directories.Incomplete, Options.Directories.Downloads); + + if (FTP.Enabled) + { + _ = FTP.UploadAsync(request.Filename.ToLocalFilename(Options.Directories.Downloads)); + } }); // wait until either the waitUntilEnqueue task completes because the download was successfully queued, or the diff --git a/src/slskd/Integrations/FTP/FTPClient.cs b/src/slskd/Integrations/FTP/FTPClient.cs new file mode 100644 index 00000000..6f23ffc6 --- /dev/null +++ b/src/slskd/Integrations/FTP/FTPClient.cs @@ -0,0 +1,105 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd.Integrations.FTP +{ + using System; + using System.IO; + using System.Threading.Tasks; + using FluentFTP; + using Microsoft.Extensions.Logging; + using static slskd.Options.IntegrationOptions; + + public class FTPClient : IFTPClient + { + public FTPClient( + IFTPClientFactory ftpClientFactory, + Microsoft.Extensions.Options.IOptionsMonitor optionsMonitor, + ILogger log) + { + Factory = ftpClientFactory; + Options = optionsMonitor.CurrentValue; + Log = log; + + try + { + EncryptionMode = (FtpEncryptionMode)Enum.Parse(typeof(FtpEncryptionMode), FTPOptions.EncryptionMode, ignoreCase: true); + } + catch (Exception ex) + { + // Options should validate that the given string is parsable to FtpEncryptionMode through EnumAttribute; if this throws there's a bug somewhere. + throw new ArgumentException($"Failed to parse {typeof(FtpEncryptionMode).Name} from application Options. This is most likely a programming error; please file a GitHub issue and include your FTP configuration.", ex); + } + } + + public bool Enabled => !string.IsNullOrEmpty(Options.Integration.FTP.Address); + + private Options Options { get; } + private FTPOptions FTPOptions => Options.Integration.FTP; + private ILogger Log { get; set; } + private IFTPClientFactory Factory { get; set; } + private FtpEncryptionMode EncryptionMode { get; set; } + + public FtpClient CreateFtpClient() + { + var client = new FtpClient(FTPOptions.Address, FTPOptions.Port, FTPOptions.Username, FTPOptions.Password); + client.EncryptionMode = EncryptionMode; + + return client; + } + + public async Task UploadAsync(string filename) + { + if (!Enabled) + { + Log.LogDebug("Skipping FTP upload of {filename}; FTP integration is disabled"); + return; + } + + try + { + var fileOnly = Path.GetFileName(filename); + var fileAndParentDirectory = Path.Combine(Path.GetDirectoryName(filename).Replace(Path.GetDirectoryName(Path.GetDirectoryName(filename)), string.Empty), fileOnly).TrimStart('/').TrimStart('\\'); + + var remotePath = FTPOptions.RemotePath.TrimEnd('/').TrimEnd('\\'); + var remoteFilename = $"{remotePath}/{fileAndParentDirectory}"; + + var existsMode = FTPOptions.OverwriteExisting ? FtpRemoteExists.Overwrite : FtpRemoteExists.Skip; + + using var stream = new FileStream(filename, FileMode.Open, FileAccess.Read); + + Log.LogInformation("Uploading {Filename} to FTP {Address}:{Port} as {RemoteFilename}", fileAndParentDirectory, FTPOptions.Address, FTPOptions.Port, remoteFilename); + var client = Factory.CreateFtpClient(); + await client.ConnectAsync(); + var status = await client.UploadAsync(stream, remoteFilename, existsMode, createRemoteDir: true); + + if (status == FtpStatus.Failed) + { + throw new FtpException("FTP client reported a failed transfer"); + } + + Log.LogInformation("FTP upload of {Filename} to {Address}:{Port} complete", fileAndParentDirectory, FTPOptions.Address, FTPOptions.Port); + Console.WriteLine($"========================DONE: {filename}, {status}"); + } + catch (Exception ex) + { + Log.LogWarning(ex, "Failed to upload {Filename} to FTP: {Message}", ex.Message); + throw; + } + } + } +} diff --git a/src/slskd/Integrations/FTP/FTPClientFactory.cs b/src/slskd/Integrations/FTP/FTPClientFactory.cs new file mode 100644 index 00000000..887623a2 --- /dev/null +++ b/src/slskd/Integrations/FTP/FTPClientFactory.cs @@ -0,0 +1,53 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd.Integrations.FTP +{ + using System; + using FluentFTP; + using static slskd.Options.IntegrationOptions; + + public class FTPClientFactory : IFTPClientFactory + { + public FTPClientFactory(Microsoft.Extensions.Options.IOptionsMonitor optionsMonitor) + { + Options = optionsMonitor.CurrentValue; + + try + { + EncryptionMode = (FtpEncryptionMode)Enum.Parse(typeof(FtpEncryptionMode), FTPOptions.EncryptionMode, ignoreCase: true); + } + catch (Exception ex) + { + // Options should validate that the given string is parsable to FtpEncryptionMode through EnumAttribute; if this throws there's a bug somewhere. + throw new ArgumentException($"Failed to parse {typeof(FtpEncryptionMode).Name} from application Options. This is most likely a programming error; please file a GitHub issue and include your FTP configuration.", ex); + } + } + + private Options Options { get; set; } + private FTPOptions FTPOptions => Options.Integration.FTP; + private FtpEncryptionMode EncryptionMode { get; set; } + + public FtpClient CreateFtpClient() + { + var client = new FtpClient(FTPOptions.Address, FTPOptions.Port, FTPOptions.Username, FTPOptions.Password); + client.EncryptionMode = EncryptionMode; + + return client; + } + } +} diff --git a/src/slskd/Integrations/FTP/FTPService.cs b/src/slskd/Integrations/FTP/FTPService.cs deleted file mode 100644 index b704063c..00000000 --- a/src/slskd/Integrations/FTP/FTPService.cs +++ /dev/null @@ -1,61 +0,0 @@ -// -// Copyright (c) slskd Team. All rights reserved. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published -// by the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see https://www.gnu.org/licenses/. -// - -namespace slskd.Integrations.FTP -{ - using System; - using System.Threading.Tasks; - using FluentFTP; - using Microsoft.Extensions.Logging; - using static slskd.Options.IntegrationOptions; - - public class FTPService : IFTPService - { - public FTPService( - Microsoft.Extensions.Options.IOptionsMonitor optionsMonitor, - ILogger log) - { - Options = optionsMonitor.CurrentValue; - Log = log; - - FtpEncryptionMode encryptionMode; - - try - { - encryptionMode = (FtpEncryptionMode)Enum.Parse(typeof(FtpEncryptionMode), FTPOptions.EncryptionMode, ignoreCase: true); - } - catch (Exception ex) - { - // Options should validate that the given string is parsable to FtpEncryptionMode through EnumAttribute; if this throws there's a bug somewhere. - throw new ArgumentException($"Failed to parse {typeof(FtpEncryptionMode).Name} from application Options. This is most likely a programming error; please file a GitHub issue and include your FTP configuration.", ex); - } - - Client = new FtpClient(FTPOptions.Address, FTPOptions.Port, FTPOptions.Username, FTPOptions.Password); - Client.EncryptionMode = encryptionMode; - } - - private Options Options { get; } - private FTPOptions FTPOptions => Options.Integration.FTP; - private ILogger Log { get; set; } - private FtpClient Client { get; set; } - - public Task UploadAsync(string filename) - { - throw new NotImplementedException(); - } - } -} diff --git a/src/slskd/Integrations/FTP/IFTPClient.cs b/src/slskd/Integrations/FTP/IFTPClient.cs new file mode 100644 index 00000000..b556ea3b --- /dev/null +++ b/src/slskd/Integrations/FTP/IFTPClient.cs @@ -0,0 +1,27 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd.Integrations.FTP +{ + using System.Threading.Tasks; + + public interface IFTPClient + { + Task UploadAsync(string filename); + bool Enabled { get; } + } +} diff --git a/src/slskd/Integrations/FTP/IFTPClientFactory.cs b/src/slskd/Integrations/FTP/IFTPClientFactory.cs new file mode 100644 index 00000000..760db612 --- /dev/null +++ b/src/slskd/Integrations/FTP/IFTPClientFactory.cs @@ -0,0 +1,26 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd.Integrations.FTP +{ + using FluentFTP; + + public interface IFTPClientFactory + { + public FtpClient CreateFtpClient(); + } +} diff --git a/src/slskd/Integrations/FTP/IFTPService.cs b/src/slskd/Integrations/FTP/IFTPService.cs deleted file mode 100644 index 11c12073..00000000 --- a/src/slskd/Integrations/FTP/IFTPService.cs +++ /dev/null @@ -1,26 +0,0 @@ -// -// Copyright (c) slskd Team. All rights reserved. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published -// by the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see https://www.gnu.org/licenses/. -// - -namespace slskd.Integrations.FTP -{ - using System.Threading.Tasks; - - public interface IFTPService - { - Task UploadAsync(string filename); - } -} diff --git a/src/slskd/Startup.cs b/src/slskd/Startup.cs index ddf2f637..f802a56a 100644 --- a/src/slskd/Startup.cs +++ b/src/slskd/Startup.cs @@ -187,7 +187,9 @@ namespace slskd services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(); + + services.AddSingleton(); + services.AddTransient(); services.AddHostedService(); services.AddSingleton(_ => Service.SoulseekClient); -- cgit v1.2.3