public bool SaveFile(string fileName) { SftpConfig config = new SftpConfig { Host = "192.168.2.102", Port = 22, UserName = "******", Password = "******" }; using (var client = new SftpClient(config.Host, config.Port, config.UserName, config.Password)) { // connect and log in client.Connect(); if (!client.IsConnected) { Console.WriteLine("It's not connected!"); return(false); } using (var fileStream = new FileStream(fileName, FileMode.Create)) { client.BufferSize = 4 * 1024; client.DownloadFile(Path.GetFileName(fileName), fileStream); } return(true); } }
public IActionResult GetPrescription(string path) { var config = new SftpConfig { Host = "192.168.0.20", Port = 2222, Username = "******", Password = "******" }; string localFile = "GeneratedPrescription/" + path + ".txt"; using var client = new SftpClient(config.Host, config.Port, config.Username, config.Password); try { client.Connect(); using var s = System.IO.File.OpenRead(localFile); Console.WriteLine(localFile); client.UploadFile(s, "test.txt"); return(Ok($"Finished uploading file [{localFile}] to [{"."}]")); } catch (Exception exception) { return(BadRequest(exception.Message + $"Failed in uploading file [{localFile}] to [{"."}]")); } finally { client.Disconnect(); } }
public bool SendFile(string fileName) { SftpConfig config = new SftpConfig { Host = "192.168.2.102", Port = 22, UserName = "******", Password = "******" }; using (var client = new SftpClient(config.Host, config.Port, config.UserName, config.Password)) { client.Connect(); if (client.IsConnected) { using (var fileStream = new FileStream(fileName, FileMode.Open)) { client.BufferSize = 4 * 1024; client.UploadFile(fileStream, Path.GetFileName(fileName)); } return(true); } else { Console.WriteLine("It's not connected."); return(false); } } }
public SftpCommunicator(SftpConfig sftpOptions) { _host = sftpOptions.Host; _port = sftpOptions.Port; _username = sftpOptions.Username; _password = sftpOptions.Password; }
public Sftp(string serverHost) { Config = new SftpConfig() { ServerHost = serverHost }; _current = new DirectoryInfo(Config.LocalRoot); }
public static void UseSftp(this IServiceCollection services, SftpConfig config) { var isValidConfig = config?.IsValid() ?? false; if (!isValidConfig) { throw new Exception($"{nameof(SftpConfig)} is invalid"); } RegisterDependencies(services, config); }
public void GetAllDrugs_Invalid_Endpoint() { SftpConfig sftpConfig = new SftpConfig() { Host = "192.168.1.4", Port = 22, Password = "******", Username = "******" }; PharmacySystemAdapterParameters parameters = new PharmacySystemAdapterParameters() { ApiKey = "api", GrpcAdress = new GrpcAdress("localhost", 9080), Url = "http://localhost:8090", HospitalName = "HealthcareSystem-ORG4", SftpConfig = sftpConfig }; _adapter.Initialize(parameters, new HttpClient()); var ret = _adapter.GetAllDrugs(); //Assert.True(ret.Count == 0); }
private static SftpService InitializeSftpService() { SftpConfig sftpConfig = new SftpConfig { Host = Environment.GetEnvironmentVariable("SftpHost"), Port = Convert.ToInt32(Environment.GetEnvironmentVariable("SftpPort")), UserName = Environment.GetEnvironmentVariable("SftpUserName"), Password = Environment.GetEnvironmentVariable("SftpPassword"), Key = Environment.GetEnvironmentVariable("SftpKey") }; #if DEBUG sftpConfig.Key = File.ReadAllText(@"PATH/TO/lplp.ppk"); #endif LoggerFactory loggerFactory = new LoggerFactory(); return(new SftpService(loggerFactory.CreateLogger(typeof(SftpService)), sftpConfig)); }
private static void Main() { var config = new SftpConfig { Host = "test.rebex.net", Port = 22, UserName = "******", Password = "******" }; var sftpService = new SftpService(new NullLogger <SftpService>(), config); // list files var files = sftpService.ListAllFiles("/pub/example"); foreach (var file in files) { if (file.IsDirectory) { Console.WriteLine($"Directory: [{file.FullName}]"); } else if (file.IsRegularFile) { Console.WriteLine($"File: [{file.FullName}]"); } } // download a file const string pngFile = @"hi.png"; File.Delete(pngFile); sftpService.DownloadFile(@"/pub/example/imap-console-client.png", pngFile); if (File.Exists(pngFile)) { Console.WriteLine($"file {pngFile} downloaded"); } // upload a file // not working for this demo SFTP server due to readonly permission var testFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "test.txt"); sftpService.UploadFile(testFile, @"/pub/test.txt"); sftpService.DeleteFile(@"/pub/test.txt"); }
public void DrugAvailibility_Returns_Searched_Drugs_Invalid_Endpoint() { SftpConfig sftpConfig = new SftpConfig() { Host = "192.168.1.4", Port = 22, Password = "******", Username = "******" }; PharmacySystemAdapterParameters parameters = new PharmacySystemAdapterParameters() { ApiKey = "api", GrpcAdress = new GrpcAdress("localhost", 9080), Url = "http://localhost:8090", HospitalName = "HealthcareSystem-ORG4", SftpConfig = sftpConfig }; _adapter.Initialize(parameters, new HttpClient()); if (_isaRunning) { var ret = _adapter.DrugAvailibility("brufen"); Assert.True(ret.Count == 0); } }
public void OrderDrugs_Invalid_Drug() { SftpConfig sftpConfig = new SftpConfig() { Host = "192.168.1.4", Port = 22, Password = "******", Username = "******" }; PharmacySystemAdapterParameters parameters = new PharmacySystemAdapterParameters() { ApiKey = "api", GrpcAdress = new GrpcAdress("localhost", 9090), Url = "http://localhost:8080", HospitalName = "HealthcareSystem-ORG4", SftpConfig = sftpConfig }; _adapter.Initialize(parameters, new HttpClient()); if (_isaRunning) { var ret = _adapter.OrderDrugs(1, 99, 1); Assert.False(ret); } }
public void GetDrugSpecifications_Invalid_Specification() { SftpConfig sftpConfig = new SftpConfig() { Host = "192.168.1.4", Port = 22, Password = "******", Username = "******" }; PharmacySystemAdapterParameters parameters = new PharmacySystemAdapterParameters() { ApiKey = "api", GrpcAdress = new GrpcAdress("localhost", 9090), Url = "http://localhost:8080", HospitalName = "HealthcareSystem-ORG4", SftpConfig = sftpConfig }; _adapter.Initialize(parameters, new HttpClient()); System.IO.Directory.CreateDirectory("Resources"); if (_isaRunning) { var ret = _adapter.GetDrugSpecifications(99); Assert.False(ret); } }
public void SendDrugConsumptionReport_Valid_Endpoint() { SftpConfig sftpConfig = new SftpConfig() { Host = "192.168.1.4", Port = 22, Password = "******", Username = "******" }; PharmacySystemAdapterParameters parameters = new PharmacySystemAdapterParameters() { ApiKey = "api", GrpcAdress = new GrpcAdress("localhost", 9090), Url = "http://localhost:8080", HospitalName = "HealthcareSystem-ORG4", SftpConfig = sftpConfig }; _adapter.Initialize(parameters, new HttpClient()); System.IO.Directory.CreateDirectory("Resources"); using (StreamWriter sw = File.CreateText("Resources/report.txt")) { sw.WriteLine("report test"); } if (_isaRunning) { var ret = _adapter.SendDrugConsumptionReport("Resources", "report.txt"); Assert.True(ret); } }
private static void RegisterDependencies(IServiceCollection services, SftpConfig config) { services.AddSingleton(config); services.AddSingleton <ISftpManager, SftpManager>(); }
public SftpManager(SftpConfig config, ILogger <SftpManager> logger = null) { _config = config ?? throw new ArgumentNullException(); _logger = logger; _isLoggingEnabled = _logger != null; }