1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
// <copyright file="FTPService.cs" company="JP Dillingham">
// ▄▄▄▄ ▄▄▄▄ ▄▄▄▄
// ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █
// █__ --█ █__ --█ ◄█ - █
// █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█
// ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━ ━┉ ┉ ┉
// │ Copyright (c) JP Dillingham.
// │
// │ 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, version 3.
// │
// │ 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/.
// │
// │ This program is distributed with Additional Terms pursuant to Section 7
// │ of the AGPLv3. See the LICENSE file in the root directory of this
// │ project for the complete terms and conditions.
// │
// │ https://slskd.org
// │
// ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌ ╌╌╌╌ ╌
// │ SPDX-FileCopyrightText: JP Dillingham
// │ SPDX-License-Identifier: AGPL-3.0-only
// ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈
// </copyright>
using Microsoft.Extensions.Options;
namespace slskd.Integrations.FTP
{
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using FluentFTP;
using FluentFTP.Exceptions;
using Microsoft.Extensions.Logging;
using static slskd.Options.IntegrationsOptions;
/// <summary>
/// FTP Integration service.
/// </summary>
public class FTPService : IFTPService
{
/// <summary>
/// Initializes a new instance of the <see cref="FTPService"/> class.
/// </summary>
/// <param name="ftpClientFactory">The FTP client factory to use.</param>
/// <param name="optionsMonitor">The options monitor used to derive application options.</param>
/// <param name="log">The logger.</param>
public FTPService(
IFTPClientFactory ftpClientFactory,
IOptionsMonitor<Options> optionsMonitor,
ILogger<FTPService> log)
{
Factory = ftpClientFactory;
OptionsMonitor = optionsMonitor;
Log = log;
}
private IFTPClientFactory Factory { get; set; }
private FtpOptions FtpOptions => OptionsMonitor.CurrentValue.Integrations.Ftp;
private ILogger<FTPService> Log { get; set; }
private IOptionsMonitor<Options> OptionsMonitor { get; }
/// <summary>
/// Uploads the specified <paramref name="filename"/> to the configured FTP server.
/// </summary>
/// <param name="filename">The fully qualified name of the file to upload.</param>
/// <returns>The operation context.</returns>
public async Task UploadAsync(string filename)
{
if (!FtpOptions.Enabled)
{
Log.LogDebug("Skipping FTP upload of {filename}; FTP integration is disabled", filename);
return;
}
var fileAndParentDirectory = GetFileAndParentDirectoryFromFilename(filename);
try
{
await Retry.Do(
task: () => AttemptUploadAsync(filename),
isRetryable: (attempts, ex) => true,
onFailure: (attempts, ex) => Log.LogInformation("Failed attempt #{Attempts} to upload {Filename} to FTP: {Message}", attempts, fileAndParentDirectory, ex.Message),
maxAttempts: FtpOptions.RetryAttempts,
baseDelayInMilliseconds: 1000,
maxDelayInMilliseconds: 30000);
}
catch (RetryException ex)
{
Log.LogError(ex, "Fatal error retrying upload of {Filename} to FTP: {Message}", fileAndParentDirectory, ex.Message);
throw;
}
catch (Exception ex)
{
Log.LogWarning("Failed to upload {Filename} to FTP after {Attempts} attempts: {Message}", fileAndParentDirectory, FtpOptions.RetryAttempts, ex.Message);
throw;
}
}
private async Task AttemptUploadAsync(string filename)
{
var fileAndParentDirectory = GetFileAndParentDirectoryFromFilename(filename);
var remotePath = FtpOptions.RemotePath.TrimEnd('/', '\\');
var remoteFilename = $"{remotePath}/{fileAndParentDirectory}";
var existsMode = FtpOptions.OverwriteExisting ? FtpRemoteExists.Overwrite : FtpRemoteExists.Skip;
using var client = Factory.CreateFtpClient();
var timeoutTaskCompletionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
using var timeoutCancellationTokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(FtpOptions.ConnectionTimeout));
using var timeoutCancellationTokenRegistration =
timeoutCancellationTokenSource.Token.Register(() => timeoutTaskCompletionSource.TrySetResult(true));
Log.LogDebug("Connecting to FTP at {Address}:{Port} using encryption mode {EncryptionMode}", client.Host, client.Port, client.Config.EncryptionMode);
var connectTask = client.Connect();
var completedTask = await Task.WhenAny(connectTask, timeoutTaskCompletionSource.Task);
if (completedTask == timeoutTaskCompletionSource.Task)
{
throw new TimeoutException($"Failed to connect to server after {FtpOptions.ConnectionTimeout}ms");
}
if (connectTask.Exception != null)
{
throw connectTask.Exception;
}
Log.LogInformation("Uploading {Filename} to FTP {Address}:{Port} as {RemoteFilename}", fileAndParentDirectory, FtpOptions.Address, FtpOptions.Port, remoteFilename);
var status = await client.UploadFile(filename, 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);
}
private string GetFileAndParentDirectoryFromFilename(string filename)
{
var fileOnly = Path.GetFileName(filename);
return Path.Combine(Path.GetDirectoryName(filename).Replace(Path.GetDirectoryName(Path.GetDirectoryName(filename)), string.Empty), fileOnly).TrimStart('/').TrimStart('\\');
}
}
}
|