using FileRepository.Models; using Microsoft.Extensions.Configuration; using Renci.SshNet; using Renci.SshNet.Sftp; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; namespace FileRepository.Repositories { /// /// The repository implementation for SFTP. /// internal class SftpRepository : IFileRepository { 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"]; this.port = int.Parse(config["SftpRepository:port"]); this.username = config["SftpRepository:username"].ToString(); this.password = config["SftpRepository:password"].ToString(); 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; this.UploadFile(file, canOverride); return file.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}"); // _logger.LogInformation($"File [{remoteFilePath}] deleted."); } catch (Exception exception) { // _logger.LogError(exception, $"Failed in deleting file [{remoteFilePath}]"); throw; } finally { client.Disconnect(); } } return true; } /// /// 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(); 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 ex) { //_logger.LogError(ex, $"Failed in downloading file [{localFilePath}] from [{remoteFilePath}]"); throw; } finally { client.Disconnect(); } } 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; this.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(); } } } } }