private static async Task UploadInputFilesAsync(long jobId, List <long?> taskIds, string sessionCode) { var client = new RestClient(baseUrl); var request = new RestRequest("FileTransfer/GetFileTransferMethod", Method.Post) { RequestFormat = DataFormat.Json }.AddJsonBody( new GetFileTransferMethodModel { SubmittedJobInfoId = jobId, SessionCode = sessionCode }); var response = await client.ExecuteAsync(request); if (response.StatusCode != System.Net.HttpStatusCode.OK) { throw new Exception(response.Content.ToString()); } FileTransferMethodExt ft = JsonConvert.DeserializeObject <FileTransferMethodExt>(response.Content.ToString()); using (MemoryStream pKeyStream = new MemoryStream(Encoding.UTF8.GetBytes(ft.Credentials.PrivateKey))) { using (ScpClient scpClient = new ScpClient(ft.ServerHostname, ft.Credentials.Username, new PrivateKeyFile(pKeyStream))) { scpClient.Connect(); DirectoryInfo di = new DirectoryInfo(@"C:\Heappe\projects\develop\tests\input\"); foreach (var taskId in taskIds) { foreach (FileInfo fi in di.GetFiles()) { sb.AppendLine($"Uploading file: {fi.Name}"); scpClient.Upload(fi, ft.SharedBasepath + "/" + taskId + "/" + fi.Name); sb.AppendLine($"File uploaded: {fi.Name}"); } } } } client = new RestClient(baseUrl); request = new RestRequest("FileTransfer/EndFileTransfer", Method.Post) { RequestFormat = DataFormat.Json }.AddJsonBody( new EndFileTransferModel { SubmittedJobInfoId = jobId, UsedTransferMethod = ft, SessionCode = sessionCode }); response = await client.ExecuteAsync(request); if (response.StatusCode != System.Net.HttpStatusCode.OK) { throw new Exception(response.Content.ToString()); } }
public static FileTransferMethod ConvertFileTransferMethodExtToIn(FileTransferMethodExt usedTransferMethod) { FileTransferMethod convert = new FileTransferMethod { Credentials = usedTransferMethod.Credentials.ConvertExtToInt(), Protocol = ConvertFileTransferProtocolExtToIn(usedTransferMethod.Protocol), ServerHostname = usedTransferMethod.ServerHostname, SharedBasePath = usedTransferMethod.SharedBasepath }; return(convert); }
public static FileTransferMethodExt ConvertIntToExt(this FileTransferMethod fileTransferMethod) { FileTransferMethodExt convert = new FileTransferMethodExt() { ServerHostname = fileTransferMethod.ServerHostname, SharedBasepath = fileTransferMethod.SharedBasePath, Protocol = ConvertFileTransferProtocolIntToExt(fileTransferMethod.Protocol), Credentials = fileTransferMethod.Credentials.ConvertIntToExt() }; return(convert); }
public override ValidationResult Validate() { string message = _validationObject switch { FileTransferMethodExt ext => ValidateFileTransferMethod(ext), TaskFileOffsetExt ext => ValidateTaskFileOffset(ext), GetFileTransferMethodModel methodModel => ValidateGetFileTransferMethodModel(methodModel), EndFileTransferModel transferModel => ValidateEndFileTransferModel(transferModel), DownloadPartsOfJobFilesFromClusterModel clusterModel => ValidateDownloadPartsOfJobFilesFromClusterModel(clusterModel), ListChangedFilesForJobModel jobModel => ValidateListChangedFilesForJobModel(jobModel), DownloadFileFromClusterModel clusterModel => ValidateDownloadFileFromClusterModel(clusterModel), _ => string.Empty }; return(new ValidationResult(string.IsNullOrEmpty(message), message)); }
public void EndFileTransfer(long submittedJobInfoId, FileTransferMethodExt usedTransferMethod, string sessionCode) { try { using (IUnitOfWork unitOfWork = UnitOfWorkFactory.GetUnitOfWorkFactory().CreateUnitOfWork()) { AdaptorUser loggedUser = UserAndLimitationManagementService.GetValidatedUserForSessionCode(sessionCode, unitOfWork, UserRoleType.Submitter); IFileTransferLogic fileTransferLogic = LogicFactory.GetLogicFactory().CreateFileTransferLogic(unitOfWork); fileTransferLogic.EndFileTransfer(submittedJobInfoId, FileTransferConverts.ConvertFileTransferMethodExtToIn(usedTransferMethod), loggedUser); } } catch (Exception exc) { ExceptionHandler.ThrowProperExternalException(exc); } }
private string ValidateFileTransferMethod(FileTransferMethodExt fileTransferMethod) { ValidationResult credentialsValidator = new CredentialsValidator(fileTransferMethod.Credentials).Validate(); if (!credentialsValidator.IsValid) { _messageBuilder.AppendLine(credentialsValidator.Message); } if (string.IsNullOrEmpty(fileTransferMethod.ServerHostname)) { _messageBuilder.AppendLine("ServerHostname cannot be empty"); } else if (!IsIpAddress(fileTransferMethod.ServerHostname) && !IsDomainName(fileTransferMethod.ServerHostname) && !IsIpAddressWithPort(fileTransferMethod.ServerHostname) && !IsDomainNamePort(fileTransferMethod.ServerHostname)) { _messageBuilder.AppendLine("ServerHostname has unknown format. If using ipv6, please try to specify 'full address' without shortening."); } if (string.IsNullOrEmpty(fileTransferMethod.SharedBasepath)) { _messageBuilder.AppendLine("SharedBasePath cannot be empty"); } else { ValidationResult pathValidator = new PathValidator(fileTransferMethod.SharedBasepath).Validate(); if (!pathValidator.IsValid) { _messageBuilder.AppendLine(pathValidator.Message); } } if (!fileTransferMethod.Protocol.HasValue) { _messageBuilder.AppendLine($"Protocol must be set"); } return(_messageBuilder.ToString()); }
private static async Task DownloadOutputFilesAsync(long jobId, string sessionCode) { //GetFileTransferMethod var client = new RestClient(baseUrl); var request = new RestRequest("FileTransfer/GetFileTransferMethod", Method.Post) { RequestFormat = DataFormat.Json }.AddJsonBody( new GetFileTransferMethodModel { SubmittedJobInfoId = jobId, SessionCode = sessionCode }); var response = await client.ExecuteAsync(request); if (response.StatusCode != System.Net.HttpStatusCode.OK) { throw new Exception(response.Content.ToString()); } FileTransferMethodExt ft = JsonConvert.DeserializeObject <FileTransferMethodExt>(response.Content.ToString()); //ListChangedFilesForJob request = new RestRequest("FileTransfer/ListChangedFilesForJob", Method.Post) { RequestFormat = DataFormat.Json }.AddJsonBody( new ListChangedFilesForJobModel { SubmittedJobInfoId = jobId, SessionCode = sessionCode }); response = await client.ExecuteAsync(request); if (response.StatusCode != System.Net.HttpStatusCode.OK) { throw new Exception(response.Content.ToString()); } var outputFiles = JsonConvert.DeserializeObject <FileInformationExt[]>(response.Content.ToString()); //DownloadOutputFiles using (MemoryStream pKeyStream = new MemoryStream(Encoding.UTF8.GetBytes(ft.Credentials.PrivateKey))) { using (ScpClient scpClient = new ScpClient(ft.ServerHostname, ft.Credentials.Username, new PrivateKeyFile(pKeyStream))) { scpClient.Connect(); string localOutputPath = $"C:\\Heappe\\projects\\develop\\tests\\output\\{jobId}\\"; DirectoryInfo di = new DirectoryInfo(localOutputPath); if (!di.Exists) { di.Create(); } foreach (var outputFile in outputFiles) { FileInfo fi = new FileInfo(localOutputPath + outputFile.FileName); if (!fi.Directory.Exists) { fi.Directory.Create(); } using (Stream fileStream = System.IO.File.OpenWrite(localOutputPath + outputFile.FileName)) { sb.AppendLine(String.Format("Downloading file {0}", outputFile.FileName)); scpClient.Download(ft.SharedBasepath + "/" + outputFile.FileName, fileStream); sb.AppendLine(String.Format("File downloaded {0}", outputFile.FileName)); } } } } //EndFileTransfer request = new RestRequest("FileTransfer/EndFileTransfer", Method.Post) { RequestFormat = DataFormat.Json }.AddJsonBody( new EndFileTransferModel { SubmittedJobInfoId = jobId, UsedTransferMethod = ft, SessionCode = sessionCode }); response = await client.ExecuteAsync(request); if (response.StatusCode != System.Net.HttpStatusCode.OK) { throw new Exception(response.Content.ToString()); } }