From 98d03ae2f2bb12a7aef3adab8fa92c3818c11449 Mon Sep 17 00:00:00 2001 From: Josh Creek Date: Mon, 30 Aug 2021 13:03:54 +0100 Subject: [PATCH] feat(*): Clean up warnings in SftpRepository --- FileRepository/Repositories/SftpRepository.cs | 61 +++++++++++++++---- 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/FileRepository/Repositories/SftpRepository.cs b/FileRepository/Repositories/SftpRepository.cs index a5c0795..4e06492 100644 --- a/FileRepository/Repositories/SftpRepository.cs +++ b/FileRepository/Repositories/SftpRepository.cs @@ -9,14 +9,21 @@ using System.Threading.Tasks; namespace FileRepository.Repositories { + /// + /// The repository implementation for SFTP. + /// internal class SftpRepository : IFileRepository { - private string host; - private int port; - private string username; - private string password; - private string remoteDirectoryPath; + private readonly string host; + private readonly int port; + private readonly string username; + private readonly string password; + private readonly string remoteDirectoryPath; + /// + /// Initialises a new instance of the class with configuration. + /// + /// The configuration to initialise the repository with. internal SftpRepository(IConfiguration config) { this.host = config["SftpRepository:host"]; @@ -26,22 +33,37 @@ namespace FileRepository.Repositories this.remoteDirectoryPath = config["SftpRepository:remoteDirectoryPath"].ToString(); } + /// + /// Create operation for the repository. + /// + /// The file to be created. + /// Returns the filename. public async Task CreateFileAsync(RepoFile file) { bool canOverride = false; - UploadFile(file, canOverride); + this.UploadFile(file, canOverride); return file.Filename; } - public async Task DeleteFileAsync(string fileName) + /// + /// Delete operation for the repository. + /// + /// The filename to be deleted. + /// Returns true if deletion was successful. + public async Task DeleteFileAsync(string filename) { + if (string.IsNullOrEmpty(filename)) + { + throw new ArgumentException($"'{nameof(filename)}' cannot be null or empty.", nameof(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}"); + client.DeleteFile($"{this.remoteDirectoryPath}/{filename}"); // _logger.LogInformation($"File [{remoteFilePath}] deleted."); } catch (Exception exception) @@ -58,8 +80,18 @@ namespace FileRepository.Repositories return true; } - public async Task ReadFileAsync(string fileName) + /// + /// Read operation for the repository. + /// + /// The filename to be read. + /// Returns the RepoFile if reading was successful. + public async Task ReadFileAsync(string filename) { + if (string.IsNullOrEmpty(filename)) + { + throw new ArgumentException($"'{nameof(filename)}' cannot be null or empty.", nameof(filename)); + } + RepoFile repoFile = new RepoFile(); repoFile.Content = new System.IO.MemoryStream(); @@ -69,9 +101,9 @@ namespace FileRepository.Repositories { client.Connect(); - client.DownloadFile($"{this.remoteDirectoryPath}/{fileName}", output: repoFile.Content); + client.DownloadFile($"{this.remoteDirectoryPath}/{filename}", output: repoFile.Content); - repoFile = repoFile.GenerateFile(fileName, repoFile.Content); + repoFile = repoFile.GenerateFile(filename, repoFile.Content); //_logger.LogInformation($"Finished downloading file [{localFilePath}] from [{remoteFilePath}]"); } @@ -89,10 +121,15 @@ namespace FileRepository.Repositories return repoFile; } + /// + /// Update operation for the repository. + /// + /// The file to be updated. + /// Returns true if update was successful. public async Task UpdateFileAsync(RepoFile file) { bool canOverride = true; - UploadFile(file, canOverride); + this.UploadFile(file, canOverride); return true; }