diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-06-27 18:19:35 -0500 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2021-06-27 18:19:35 -0500 |
| commit | 9035870600587b6bf2c689e148d2f5c77e85a9e7 (patch) | |
| tree | 9937256985fcfc1207fb1f209c71f6e38aa00f1d | |
| parent | 7cd62baa4501b9dbedfee2f13d5902555116a587 (diff) | |
| download | slskd-9035870600587b6bf2c689e148d2f5c77e85a9e7.tar.gz | |
implement upload
| -rw-r--r-- | src/slskd/Application/Transfer/API/Controllers/TransfersController.cs | 12 | ||||
| -rw-r--r-- | src/slskd/Integrations/FTP/FTPClient.cs | 105 | ||||
| -rw-r--r-- | src/slskd/Integrations/FTP/FTPClientFactory.cs (renamed from src/slskd/Integrations/FTP/FTPService.cs) | 30 | ||||
| -rw-r--r-- | src/slskd/Integrations/FTP/IFTPClient.cs (renamed from src/slskd/Integrations/FTP/IFTPService.cs) | 5 | ||||
| -rw-r--r-- | src/slskd/Integrations/FTP/IFTPClientFactory.cs | 26 | ||||
| -rw-r--r-- | src/slskd/Startup.cs | 4 |
6 files changed, 159 insertions, 23 deletions
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; /// <summary> @@ -43,19 +44,23 @@ namespace slskd.Transfer.API /// <param name="options"></param> /// <param name="client"></param> /// <param name="tracker"></param> + /// <param name="ftpClient"></param> public TransfersController( Microsoft.Extensions.Options.IOptionsSnapshot<Options> 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; } /// <summary> /// 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 file="FTPService.cs" company="slskd Team"> +// 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/. +// </copyright> + +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<Options> optionsMonitor, + ILogger<FTPClient> 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<FTPClient> 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/FTPService.cs b/src/slskd/Integrations/FTP/FTPClientFactory.cs index b704063c..887623a2 100644 --- a/src/slskd/Integrations/FTP/FTPService.cs +++ b/src/slskd/Integrations/FTP/FTPClientFactory.cs @@ -1,4 +1,4 @@ -// <copyright file="FTPService.cs" company="slskd Team"> +// <copyright file="FTPClientFactory.cs" company="slskd Team"> // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -18,44 +18,36 @@ 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 class FTPClientFactory : IFTPClientFactory { - public FTPService( - Microsoft.Extensions.Options.IOptionsMonitor<Options> optionsMonitor, - ILogger<FTPService> log) + public FTPClientFactory(Microsoft.Extensions.Options.IOptionsMonitor<Options> optionsMonitor) { Options = optionsMonitor.CurrentValue; - Log = log; - - FtpEncryptionMode encryptionMode; try { - encryptionMode = (FtpEncryptionMode)Enum.Parse(typeof(FtpEncryptionMode), FTPOptions.EncryptionMode, ignoreCase: true); + 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 Options Options { get; set; } private FTPOptions FTPOptions => Options.Integration.FTP; - private ILogger<FTPService> Log { get; set; } - private FtpClient Client { get; set; } + private FtpEncryptionMode EncryptionMode { get; set; } - public Task UploadAsync(string filename) + public FtpClient CreateFtpClient() { - throw new NotImplementedException(); + var client = new FtpClient(FTPOptions.Address, FTPOptions.Port, FTPOptions.Username, FTPOptions.Password); + client.EncryptionMode = EncryptionMode; + + return client; } } } diff --git a/src/slskd/Integrations/FTP/IFTPService.cs b/src/slskd/Integrations/FTP/IFTPClient.cs index 11c12073..b556ea3b 100644 --- a/src/slskd/Integrations/FTP/IFTPService.cs +++ b/src/slskd/Integrations/FTP/IFTPClient.cs @@ -1,4 +1,4 @@ -// <copyright file="IFTPService.cs" company="slskd Team"> +// <copyright file="IFTPClient.cs" company="slskd Team"> // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -19,8 +19,9 @@ namespace slskd.Integrations.FTP { using System.Threading.Tasks; - public interface IFTPService + 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 file="IFTPClientFactory.cs" company="slskd Team"> +// 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/. +// </copyright> + +namespace slskd.Integrations.FTP +{ + using FluentFTP; + + public interface IFTPClientFactory + { + public FtpClient CreateFtpClient(); + } +} 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<ISearchService, SearchService>(); services.AddSingleton<IPeerService, PeerService>(); services.AddSingleton<IManagementService, ManagementService>(); - services.AddSingleton<IFTPService, FTPService>(); + + services.AddSingleton<IFTPClientFactory, FTPClientFactory>(); + services.AddTransient<IFTPClient, FTPClient>(); services.AddHostedService<Service>(); services.AddSingleton(_ => Service.SoulseekClient); |
