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

HicServices / RDMP / 7528620232

15 Jan 2024 12:09PM UTC coverage: 56.76% (-0.02%) from 56.776%
7528620232

push

github

JFriel
fix bad merge

10725 of 20361 branches covered (0.0%)

Branch coverage included in aggregate %.

30655 of 52543 relevant lines covered (58.34%)

7292.26 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 FluentFTP;
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
    public SFTPDownloader()
×
38
    {
39
        _connection = new Lazy<SftpClient>(SetupSftp, LazyThreadSafetyMode.ExecutionAndPublication);
×
40
    }
×
41

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

52

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

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

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

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

68

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

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

87

88
    protected override string[] GetFileList()
89
    {
90
        var directory = string.IsNullOrWhiteSpace(RemoteDirectory) ? "." : RemoteDirectory;
×
91

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