• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

HicServices / RDMP / 9521283302

14 Jun 2024 07:19PM UTC coverage: 56.904% (-0.009%) from 56.913%
9521283302

Pull #1856

github

jas88
FTP, SFTP timeouts and liveness checks
Pull Request #1856: FTP, SFTP timeouts and liveness checks

10817 of 20488 branches covered (52.8%)

Branch coverage included in aggregate %.

0 of 17 new or added lines in 2 files covered. (0.0%)

3 existing lines in 2 files now uncovered.

30827 of 52695 relevant lines covered (58.5%)

7418.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/Rdmp.Core/DataLoad/Modules/FTP/SFTPDownloader.cs
1
// Copyright (c) The University of Dundee 2018-2019
2
// This file is part of the Research Data Management Platform (RDMP).
3
// RDMP is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
4
// RDMP 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 General Public License for more details.
5
// You should have received a copy of the GNU General Public License along with RDMP. If not, see <https://www.gnu.org/licenses/>.
6

7
using System;
8
using System.IO;
9
using System.Linq;
10
using System.Threading;
11
using Rdmp.Core.Curation;
12
using Rdmp.Core.Curation.Data;
13
using Rdmp.Core.ReusableLibraryCode.Progress;
14
using Renci.SshNet;
15

16
namespace Rdmp.Core.DataLoad.Modules.FTP;
17

18
/// <summary>
19
/// load component which downloads files from a remote SFTP (Secure File Transfer Protocol) server to the ForLoading directory
20
/// 
21
/// <para>Operates in the same way as <see cref="FTPDownloader"/> except that it uses SSH.  In addition, this
22
/// class will not bother downloading any files that already exist in the forLoading directory (have the same name - file size is NOT checked)</para>
23
/// </summary>
24
public class SFTPDownloader : FTPDownloader
25
{
26
    [DemandsInitialization("The keep-alive interval.  In milliseconds.  Requires KeepAlive to be set to take effect.")]
27
    public int KeepAliveIntervalMilliseconds { get; set; }
×
28

29
    private readonly Lazy<SftpClient> _connection;
30

31
    public SFTPDownloader(Lazy<SftpClient> connection)
×
32
    {
33
        _connection = new Lazy<SftpClient>(SetupSftp,LazyThreadSafetyMode.ExecutionAndPublication);
×
34
    }
×
35

36
    public SFTPDownloader()
×
37
    {
38
        _connection = new Lazy<SftpClient>(SetupSftp, LazyThreadSafetyMode.ExecutionAndPublication);
×
39
    }
×
40

41
    private SftpClient SetupSftp()
42
    {
43
        var host = FTPServer?.Server ?? throw new NullReferenceException("FTP server not set");
×
44
        var username = FTPServer.Username ?? "anonymous";
×
45
        var password = string.IsNullOrWhiteSpace(FTPServer.Password) ? "guest" : FTPServer.GetDecryptedPassword();
×
46
        var c = new SftpClient(host, username, password);
×
47
        c.Connect();
×
NEW
48
        if (KeepAlive)
×
NEW
49
            c.KeepAliveInterval = TimeSpan.FromMilliseconds(KeepAliveIntervalMilliseconds);
×
UNCOV
50
        return c;
×
51
    }
52

53

54
    protected override void Download(string file, ILoadDirectory destination)
55
    {
56
        if (file.Contains('/') || file.Contains('\\'))
×
57
            throw new Exception("Was not expecting a relative path here");
×
58

59
        //if there is a specified remote directory then reference it otherwise reference it locally (or however we were told about it from GetFileList())
60
        var fullFilePath = !string.IsNullOrWhiteSpace(RemoteDirectory) ? Path.Combine(RemoteDirectory, file) : file;
×
61

62
        var destinationFilePath = Path.Combine(destination.ForLoading.FullName, file);
×
63

64
        using (var dest=File.Create(destinationFilePath))
×
65
            _connection.Value.DownloadFile(fullFilePath,dest);
×
66
        _filesRetrieved.Add(fullFilePath);
×
67
    }
×
68

69

70
    public override void LoadCompletedSoDispose(ExitCodeType exitCode, IDataLoadEventListener postLoadEventListener)
71
    {
72
        if (exitCode != ExitCodeType.Success) return;
×
73

74
        // Reconnect if we got cut off, for example due to idle timers
NEW
75
        if (!_connection.Value.IsConnected)
×
NEW
76
            _connection.Value.Connect();
×
77

UNCOV
78
        foreach (var retrievedFiles in _filesRetrieved)
×
79
            try
80
            {
81
                _connection.Value.DeleteFile(retrievedFiles);
×
82
                postLoadEventListener.OnNotify(this, new NotifyEventArgs(ProgressEventType.Information,
×
83
                    $"Deleted SFTP file {retrievedFiles} from SFTP server"));
×
84
            }
×
85
            catch (Exception e)
×
86
            {
87
                postLoadEventListener.OnNotify(this, new NotifyEventArgs(ProgressEventType.Error,
×
88
                    $"Could not delete SFTP file {retrievedFiles} from SFTP server", e));
×
89
            }
×
90
    }
×
91

92

93
    protected override string[] GetFileList()
94
    {
95
        var directory = string.IsNullOrWhiteSpace(RemoteDirectory) ? "." : RemoteDirectory;
×
96

97
        return _connection.Value.ListDirectory(directory).Select(static d => d.Name).ToArray();
×
98
    }
99
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc