From bc4decc617d330dc9d5724805846a857dbefadd0 Mon Sep 17 00:00:00 2001 From: Josh Creek Date: Mon, 30 Aug 2021 00:31:36 +0100 Subject: [PATCH] feat(*): Add an initial barebones implementation of the SftpRepository --- FileRepository/FileRepository.csproj | 1 + FileRepository/Repositories/SftpRepository.cs | 117 ++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 FileRepository/Repositories/SftpRepository.cs diff --git a/FileRepository/FileRepository.csproj b/FileRepository/FileRepository.csproj index 70a36fc..976e4f7 100644 --- a/FileRepository/FileRepository.csproj +++ b/FileRepository/FileRepository.csproj @@ -13,6 +13,7 @@ + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/FileRepository/Repositories/SftpRepository.cs b/FileRepository/Repositories/SftpRepository.cs new file mode 100644 index 0000000..13eb063 --- /dev/null +++ b/FileRepository/Repositories/SftpRepository.cs @@ -0,0 +1,117 @@ +using FileRepository.Models; +using Renci.SshNet; +using Renci.SshNet.Sftp; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace FileRepository.Repositories +{ + internal class SftpRepository : IFileRepository + { + public async Task CreateFileAsync(RepoFile file) + { + bool canOverride = false; + UploadFile(file, canOverride); + + return file.FileName; + } + + public async Task DeleteFileAsync(string fileName) + { + using (var client = new SftpClient(this.host, this.port == 0 ? 22 : this.port, this.userName, this.password)) + { + try + { + client.Connect(); + client.DeleteFile($"{this.remoteDirectoryPath}/{fileName}"); + // _logger.LogInformation($"File [{remoteFilePath}] deleted."); + } + catch (Exception exception) + { + // _logger.LogError(exception, $"Failed in deleting file [{remoteFilePath}]"); + throw; + } + finally + { + client.Disconnect(); + } + } + + return true; + } + + public async Task ReadFileAsync(string fileName) + { + RepoFile repoFile = new RepoFile(); + + using (SftpClient client = new SftpClient(this.host, this.port == 0 ? 22 : this.port, this.userName, this.password)) + { + try + { + client.Connect(); + + client.DownloadFile($"{this.remoteDirectoryPath}/{fileName}", output: repoFile.Content); + + repoFile = repoFile.GenerateFile(fileName, repoFile.Content); + + //_logger.LogInformation($"Finished downloading file [{localFilePath}] from [{remoteFilePath}]"); + } + catch (Exception exception) + { + //_logger.LogError(exception, $"Failed in downloading file [{localFilePath}] from [{remoteFilePath}]"); + } + finally + { + client.Disconnect(); + } + } + + return repoFile; + } + + public async Task UpdateFileAsync(RepoFile file) + { + bool canOverride = true; + UploadFile(file, canOverride); + + return true; + } + + private void UploadFile(RepoFile file, bool canOverride) + { + using (SftpClient client = new SftpClient(this.host, this.port == 0 ? 22 : this.port, this.userName, this.password)) + { + try + { + client.Connect(); + client.ChangeDirectory(this.remoteDirectoryPath); + + client.UploadFile(file.Content, file.FileName, canOverride); + + //_logger.LogInformation($"Finished uploading file {file.FileName} to [{remoteDirectory}]"); + } + // TODO - handle specific exceptions where a file already exists and cannot be overwritten + catch (Exception ex) + { + //_logger.LogError(ex, $"Failed in uploading file {file.FileName} to [{remoteDirectory}]"); + + throw; + } + finally + { + client.Disconnect(); + } + } + } + + // Set these from the config file or environment variables + private string host = ""; + + private int port = 0; + private string userName = ""; + private string password = ""; + private string remoteDirectoryPath = ""; + } +}