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

HicServices / RDMP / 6530123255

16 Oct 2023 07:01AM UTC coverage: 56.984% (+0.07%) from 56.913%
6530123255

Pull #1649

github

web-flow
Merge branch 'develop' into feature/ftptidy
Pull Request #1649: New FTP/FTPS support, improve SFTP

10690 of 20211 branches covered (0.0%)

Branch coverage included in aggregate %.

61 of 61 new or added lines in 2 files covered. (100.0%)

30533 of 52130 relevant lines covered (58.57%)

7347.19 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.Diagnostics;
9
using System.IO;
10
using System.Linq;
11
using System.Threading;
12
using Rdmp.Core.Curation;
13
using Rdmp.Core.Curation.Data;
14
using Rdmp.Core.ReusableLibraryCode.Progress;
15
using Renci.SshNet;
16

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

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

30
    private readonly Lazy<SftpClient> _connection;
31

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

37
    private SftpClient SetupSftp()
38
    {
39
        var host = FTPServer?.Server ?? throw new NullReferenceException("FTP server not set");
×
40
        var username = FTPServer.Username ?? "anonymous";
×
41
        var password = string.IsNullOrWhiteSpace(FTPServer.Password) ? "guest" : FTPServer.GetDecryptedPassword();
×
42
        var c = new SftpClient(host, username, password);
×
43
        c.Connect();
×
44
        return c;
×
45
    }
46

47

48
    protected override void Download(string file, ILoadDirectory destination)
49
    {
50
        if (file.Contains('/') || file.Contains('\\'))
×
51
            throw new Exception("Was not expecting a relative path here");
×
52

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

56
        var destinationFilePath = Path.Combine(destination.ForLoading.FullName, file);
×
57

58
        using (var dest=File.Create(destinationFilePath))
×
59
            _connection.Value.DownloadFile(fullFilePath,dest);
×
60
        _filesRetrieved.Add(fullFilePath);
×
61
    }
×
62

63

64
    public override void LoadCompletedSoDispose(ExitCodeType exitCode, IDataLoadEventListener postLoadEventListener)
65
    {
66
        if (exitCode != ExitCodeType.Success) return;
×
67

68
        foreach (var retrievedFiles in _filesRetrieved)
×
69
            try
70
            {
71
                _connection.Value.DeleteFile(retrievedFiles);
×
72
                postLoadEventListener.OnNotify(this, new NotifyEventArgs(ProgressEventType.Information,
×
73
                    $"Deleted SFTP file {retrievedFiles} from SFTP server"));
×
74
            }
×
75
            catch (Exception e)
×
76
            {
77
                postLoadEventListener.OnNotify(this, new NotifyEventArgs(ProgressEventType.Error,
×
78
                    $"Could not delete SFTP file {retrievedFiles} from SFTP server", e));
×
79
            }
×
80
    }
×
81

82

83
    protected override string[] GetFileList()
84
    {
85
        var directory = string.IsNullOrWhiteSpace(RemoteDirectory) ? "." : RemoteDirectory;
×
86

87
        return _connection.Value.ListDirectory(directory).Select(static d => d.Name).ToArray();
×
88
    }
89
}
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