refactor(*): Replace GenerateFile method by just using the RepoFile constructor

This commit is contained in:
Josh Creek
2021-09-02 20:06:05 +01:00
parent c05c2c5eb2
commit 45d0f05d2d
3 changed files with 19 additions and 27 deletions
+13 -18
View File
@@ -8,6 +8,19 @@ namespace Creek.FileRepository.Models
/// </summary>
public class RepoFile
{
/// <summary>
/// Initialises a new instance of the <see cref="RepoFile"/> class.
/// </summary>
/// <param name="filename">The filename of the file.</param>
/// <param name="created">The DateTime the file was created.</param>
/// <param name="content">The content of the file.</param>
public RepoFile(string filename, DateTime created, Stream content = null)
{
this.Filename = filename;
this.Content = content ?? new MemoryStream();
this.Created = created;
}
/// <summary>
/// Gets or sets the actual file name of the file, this serves as the key field/unique identifier.
/// </summary>
@@ -22,23 +35,5 @@ namespace Creek.FileRepository.Models
/// Gets or sets the datetime object representing when this file object was created.
/// </summary>
public DateTime Created { get; set; }
/// <summary>
/// Initialise a File model based on passed parameters.
/// </summary>
/// <param name="filename">The filename of the file.</param>
/// <param name="content">The content of the file.</param>
/// <returns>Returns an initialised File object.</returns>
public RepoFile GenerateFile(string filename, Stream content)
{
RepoFile newFile = new RepoFile()
{
Filename = filename,
Content = content,
Created = DateTime.Now,
};
return newFile;
}
}
}
@@ -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;
}
/// <summary>
+2 -2
View File
@@ -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);
}