mirror of
https://github.com/jcreek/Creek.FileRepository.git
synced 2026-07-13 10:53:43 +00:00
refactor(*): Replace GenerateFile method by just using the RepoFile constructor
This commit is contained in:
@@ -8,6 +8,19 @@ namespace Creek.FileRepository.Models
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class RepoFile
|
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>
|
/// <summary>
|
||||||
/// Gets or sets the actual file name of the file, this serves as the key field/unique identifier.
|
/// Gets or sets the actual file name of the file, this serves as the key field/unique identifier.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -22,23 +35,5 @@ namespace Creek.FileRepository.Models
|
|||||||
/// Gets or sets the datetime object representing when this file object was created.
|
/// Gets or sets the datetime object representing when this file object was created.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public DateTime Created { get; set; }
|
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));
|
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))
|
using (SftpClient client = new SftpClient(this.host, this.port == 0 ? 22 : this.port, this.username, this.password))
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
client.Connect();
|
client.Connect();
|
||||||
|
|
||||||
|
RepoFile repoFile = new RepoFile(filename, DateTime.Now);
|
||||||
|
|
||||||
client.DownloadFile($"{this.remoteDirectoryPath}/{filename}", output: repoFile.Content);
|
client.DownloadFile($"{this.remoteDirectoryPath}/{filename}", output: repoFile.Content);
|
||||||
|
|
||||||
repoFile = repoFile.GenerateFile(filename, repoFile.Content);
|
|
||||||
|
|
||||||
//_logger.LogInformation($"Finished downloading file [{localFilePath}] from [{remoteFilePath}]");
|
//_logger.LogInformation($"Finished downloading file [{localFilePath}] from [{remoteFilePath}]");
|
||||||
|
|
||||||
|
return repoFile;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -131,8 +130,6 @@ namespace Creek.FileRepository.Repositories
|
|||||||
client.Disconnect();
|
client.Disconnect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return repoFile;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -146,10 +146,10 @@ namespace UnitTests
|
|||||||
private async Task CreateTestFile(String filename)
|
private async Task CreateTestFile(String filename)
|
||||||
{
|
{
|
||||||
IFileRepository fileRepository = Factory.GetFileRepository(repositoryType, config);
|
IFileRepository fileRepository = Factory.GetFileRepository(repositoryType, config);
|
||||||
RepoFile repoFile = new RepoFile();
|
|
||||||
Stream contentStream = StreamHelper.GenerateStreamFromString("a,b \n c,d");
|
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);
|
await fileRepository.CreateFileAsync(repoFile);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user