From ea150385946688250b8f1ffb67177bd5811a525f Mon Sep 17 00:00:00 2001 From: Josh Creek Date: Sun, 29 Aug 2021 15:44:47 +0100 Subject: [PATCH] feat(*): Add StfpService --- .../Services/SftpService.cs | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 YouTubeChannelDownloader/Services/SftpService.cs diff --git a/YouTubeChannelDownloader/Services/SftpService.cs b/YouTubeChannelDownloader/Services/SftpService.cs new file mode 100644 index 0000000..0adacb6 --- /dev/null +++ b/YouTubeChannelDownloader/Services/SftpService.cs @@ -0,0 +1,106 @@ +using Microsoft.Extensions.Logging; +using Renci.SshNet; +using Renci.SshNet.Sftp; +using System; +using System.Collections.Generic; +using System.IO; +using YouTubeChannelDownloader.Models; + +namespace YouTubeChannelDownloader.Services +{ + public class SftpService + { + private readonly ILogger _logger; + private readonly SftpConfig _config; + + public SftpService(ILogger logger, SftpConfig sftpConfig) + { + // TODO - actually implement the ILogger in the console application to pass into this class + _logger = logger; + _config = sftpConfig; + } + + public IEnumerable ListAllFiles(string remoteDirectory = ".") + { + using var client = new SftpClient(_config.Host, _config.Port == 0 ? 22 : _config.Port, _config.UserName, _config.Password); + try + { + client.Connect(); + return client.ListDirectory(remoteDirectory); + } + catch (Exception exception) + { + _logger.LogError(exception, $"Failed in listing files under [{remoteDirectory}]"); + return null; + } + finally + { + client.Disconnect(); + } + } + + public void UploadFile(string localFilePath, string remoteDirectory, string fileName) + { + using var client = new SftpClient(_config.Host, _config.Port == 0 ? 22 : _config.Port, _config.UserName, _config.Password); + try + { + client.Connect(); + client.ChangeDirectory(remoteDirectory); + + using (var uplfileStream = File.OpenRead(localFilePath)) + { + client.UploadFile(uplfileStream, fileName, true); + } + + //_logger.LogInformation($"Finished uploading file [{localFilePath}] to [{remoteDirectory}]"); + } + catch (Exception exception) + { + //_logger.LogError(exception, $"Failed in uploading file [{localFilePath}] to [{remoteDirectory}]"); + } + finally + { + client.Disconnect(); + } + } + + public void DownloadFile(string remoteFilePath, string localFilePath) + { + using var client = new SftpClient(_config.Host, _config.Port == 0 ? 22 : _config.Port, _config.UserName, _config.Password); + try + { + client.Connect(); + using var s = File.Create(localFilePath); + client.DownloadFile(remoteFilePath, s); + _logger.LogInformation($"Finished downloading file [{localFilePath}] from [{remoteFilePath}]"); + } + catch (Exception exception) + { + _logger.LogError(exception, $"Failed in downloading file [{localFilePath}] from [{remoteFilePath}]"); + } + finally + { + client.Disconnect(); + } + } + + public void DeleteFile(string remoteFilePath) + { + using var client = new SftpClient(_config.Host, _config.Port == 0 ? 22 : _config.Port, _config.UserName, _config.Password); + try + { + client.Connect(); + client.DeleteFile(remoteFilePath); + _logger.LogInformation($"File [{remoteFilePath}] deleted."); + } + catch (Exception exception) + { + _logger.LogError(exception, $"Failed in deleting file [{remoteFilePath}]"); + } + finally + { + client.Disconnect(); + } + } + } +}