From 45d0f05d2d1180e59b847b0b8fed96a8c7e73d45 Mon Sep 17 00:00:00 2001 From: Josh Creek Date: Thu, 2 Sep 2021 20:06:05 +0100 Subject: [PATCH] refactor(*): Replace GenerateFile method by just using the RepoFile constructor --- Creek.FileRepository/Models/RepoFile.cs | 31 ++++++++----------- .../Repositories/SftpRepository.cs | 11 +++---- UnitTests/SftpRepositoryShould.cs | 4 +-- 3 files changed, 19 insertions(+), 27 deletions(-) diff --git a/Creek.FileRepository/Models/RepoFile.cs b/Creek.FileRepository/Models/RepoFile.cs index e885469..e14a7d5 100644 --- a/Creek.FileRepository/Models/RepoFile.cs +++ b/Creek.FileRepository/Models/RepoFile.cs @@ -8,6 +8,19 @@ namespace Creek.FileRepository.Models /// public class RepoFile { + /// + /// Initialises a new instance of the class. + /// + /// The filename of the file. + /// The DateTime the file was created. + /// The content of the file. + public RepoFile(string filename, DateTime created, Stream content = null) + { + this.Filename = filename; + this.Content = content ?? new MemoryStream(); + this.Created = created; + } + /// /// Gets or sets the actual file name of the file, this serves as the key field/unique identifier. /// @@ -22,23 +35,5 @@ namespace Creek.FileRepository.Models /// Gets or sets the datetime object representing when this file object was created. /// public DateTime Created { get; set; } - - /// - /// Initialise a File model based on passed parameters. - /// - /// The filename of the file. - /// The content of the file. - /// Returns an initialised File object. - public RepoFile GenerateFile(string filename, Stream content) - { - RepoFile newFile = new RepoFile() - { - Filename = filename, - Content = content, - Created = DateTime.Now, - }; - - return newFile; - } } } diff --git a/Creek.FileRepository/Repositories/SftpRepository.cs b/Creek.FileRepository/Repositories/SftpRepository.cs index 0f4aeba..e457730 100644 --- a/Creek.FileRepository/Repositories/SftpRepository.cs +++ b/Creek.FileRepository/Repositories/SftpRepository.cs @@ -106,20 +106,19 @@ namespace Creek.FileRepository.Repositories throw new ArgumentException($"'{nameof(filename)}' contains invalid characters for a filename.", nameof(filename)); } - RepoFile repoFile = new RepoFile(); - repoFile.Content = new System.IO.MemoryStream(); - using (SftpClient client = new SftpClient(this.host, this.port == 0 ? 22 : this.port, this.username, this.password)) { try { client.Connect(); + RepoFile repoFile = new RepoFile(filename, DateTime.Now); + client.DownloadFile($"{this.remoteDirectoryPath}/{filename}", output: repoFile.Content); - repoFile = repoFile.GenerateFile(filename, repoFile.Content); - //_logger.LogInformation($"Finished downloading file [{localFilePath}] from [{remoteFilePath}]"); + + return repoFile; } catch (Exception ex) { @@ -131,8 +130,6 @@ namespace Creek.FileRepository.Repositories client.Disconnect(); } } - - return repoFile; } /// diff --git a/UnitTests/SftpRepositoryShould.cs b/UnitTests/SftpRepositoryShould.cs index cf0b52f..27495af 100644 --- a/UnitTests/SftpRepositoryShould.cs +++ b/UnitTests/SftpRepositoryShould.cs @@ -146,10 +146,10 @@ namespace UnitTests private async Task CreateTestFile(String filename) { IFileRepository fileRepository = Factory.GetFileRepository(repositoryType, config); - RepoFile repoFile = new RepoFile(); + Stream contentStream = StreamHelper.GenerateStreamFromString("a,b \n c,d"); - repoFile = repoFile.GenerateFile(filename, contentStream); + RepoFile repoFile = new RepoFile(filename, DateTime.Now, contentStream); await fileRepository.CreateFileAsync(repoFile); }