aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Integrations/FTP/FTPClient.cs
blob: 105863ea9c4d1a1fb818008467a560273cae7955 (plain) (blame)
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
// <copyright file="FTPClient.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;
    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;
            }

            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;

            try
            {
                await Retry.Do(async () =>
                {
                    Log.LogInformation("Uploading {Filename} to FTP {Address}:{Port} as {RemoteFilename}", fileAndParentDirectory, FTPOptions.Address, FTPOptions.Port, remoteFilename);
                    var client = Factory.CreateFtpClient();

                    var cts = new CancellationTokenSource(FTPOptions.ConnectionTimeout);
                    await client.ConnectAsync(cts.Token);

                    var status = await client.UploadFileAsync(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);
                }, (attempts, ex) =>
                {
                    Log.LogWarning("Failed attempt {Attempts} to upload {Filename} to FTP: {Message}", attempts, ex.Message);
                    return true;
                }, maxAttempts: FTPOptions.RetryAttempts, maxDelayInMilliseconds: 30000);
            }
            catch (Exception ex)
            {
                Log.LogWarning(ex, "Failed to upload {Filename} to FTP after {Attempts}: {Message}", FTPOptions.RetryAttempts, ex.Message);
                throw;
            }
        }
    }
}