public DownloadedFile CreateFile(string fileName, string folderPath, byte[] data) { string filePath = GetUniqueFilename(ref fileName, folderPath); File.WriteAllBytes(filePath, data); var file = new DownloadedFile() { Name = fileName, Modified = DateTime.Now, Path = filePath, }; LoadPreviewData(file); return(file); }
async public void DeleteFile(string titleId, DownloadedFile fileInfo) { var currentPlayFabTitleId = PlayFabSettings.TitleId; var currentDevKey = PlayFabSettings.DeveloperSecretKey; var title = FindTitle(titleId); PlayFabSettings.TitleId = titleId; PlayFabSettings.DeveloperSecretKey = title.SecretKey; var key = fileInfo.FilePath.Split('/').ToList()[fileInfo.FilePath.Split('/').ToList().Count - 1]; var type = MimeMapping.GetMimeMapping(key); var file = new DeleteContentRequest(); var result = await PlayFabAdminAPI.DeleteContentAsync(file); }
public ActionResult GetPDF(int id) { try { DownloadedFile downloadedFile = new DownloadedFile(); downloadedFile.Download(id); Response.AppendHeader("Content-Disposition", "inline; filename=" + downloadedFile.FileName); return(File(downloadedFile.FileContent, downloadedFile.FileType)); } catch (Exception ex) { return(View("DisplayError", ex)); } }
public static DownloadedFile GetHttp(string apiKey, string path, IDictionary <string, string> headers) { try { System.Net.ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path); request.Method = "GET"; AddAdditionalHeaders(request, headers); setupAuthorization(request, apiKey); request.Accept = ESL_ACCEPT_TYPE_APPLICATION; SetProxy(request); WebResponse response = request.GetResponse(); using (Stream responseStream = response.GetResponseStream()) { var memoryStream = new MemoryStream(); CopyTo(responseStream, memoryStream); byte[] result = memoryStream.ToArray(); string fileName = ""; if (!String.IsNullOrEmpty(response.Headers["Content-Disposition"])) { fileName = GetFilename(response.Headers["Content-Disposition"].ToString()); } DownloadedFile downloadedFile = new DownloadedFile(fileName, result); return(downloadedFile); } } catch (WebException e) { using (var stream = e.Response.GetResponseStream()) using (var reader = new StreamReader(stream)) { string errorDetails = reader.ReadToEnd(); throw new EslServerException(String.Format("{0} HTTP {1} on URI {2}. Optional details: {3}", e.Message, ((HttpWebResponse)e.Response).Method, e.Response.ResponseUri, errorDetails), errorDetails, e); } } catch (Exception e) { throw new EslException("Error communicating with esl server. " + e.Message, e); } }
public void OnRequestingFile(String url) { lock (this.resultsLock) { DownloadedFile df = GetDownloadedFile(url); if (df == null) { return; } df.Done = false; Redraw(); } }
public void OnRequestingFile(String url) { lock (this.resultsLock) { DownloadedFile df = GetDownloadedFile(url); if (df == null) { return; } df.Done = false; base.RefreshObject(df); } }
public void OnFileReceived(String url) { lock (this.resultsLock) { DownloadedFile df = GetDownloadedFile(url); if (df == null) { return; } df.Done = true; base.RefreshObject(df); } }
public HttpResponseMessage RejectFile(DownloadedFile file) { try { var rejectedFile = downloadManager.RejectFile(file); return(Request.CreateResponse(HttpStatusCode.OK, rejectedFile)); } catch (Exception ex) { //handle exception logger.AddErrorLog(ex); return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex)); } }
public List <DownloadedFile> LoadAllFilesFromPath(string fileStoragePath) { List <DownloadedFile> files = new List <DownloadedFile>(); foreach (string filePath in Directory.EnumerateFiles(fileStoragePath)) { var file = new DownloadedFile() { Name = Path.GetFileName(filePath), Path = filePath, Modified = File.GetLastWriteTime(filePath), }; LoadPreviewData(file); files.Add(file); } return(files.OrderBy(f => f.Modified).ToList()); }
public static DownloadedFile GetHttp(string apiToken, string path) { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path); request.Method = "GET"; request.Headers.Add("Authorization", "Basic " + apiToken); request.Accept = ESL_ACCEPT_TYPE_APPLICATION; SetProxy(request); WebResponse response = request.GetResponse(); using (Stream responseStream = response.GetResponseStream()) { var memoryStream = new MemoryStream(); CopyTo(responseStream, memoryStream); byte[] result = memoryStream.ToArray(); string fileName = ""; if (!String.IsNullOrEmpty(response.Headers["Content-Disposition"])) { fileName = GetFilename(response.Headers["Content-Disposition"].ToString()); } DownloadedFile downloadedFile = new DownloadedFile(fileName, result); return(downloadedFile); } } catch (WebException e) { using (var stream = e.Response.GetResponseStream()) using (var reader = new StreamReader(stream)) { string errorDetails = reader.ReadToEnd(); throw new EslServerException(String.Format("{0} HTTP {1} on URI {2}. Optional details: {3}", e.Message, ((HttpWebResponse)e.Response).Method, e.Response.ResponseUri, errorDetails), errorDetails, e); } } catch (Exception e) { throw new EslException("Error communicating with esl server. " + e.Message, e); } }
public void OnSendingData(String url, int length) { if (length <= 0) { return; } lock (this.resultsLock) { DownloadedFile df = GetDownloadedFile(url); if (df == null) { return; } df.Out += length; base.RefreshObject(df); } }
public void OnReceivingData(String url, int length) { if (length <= 0) { return; } lock (this.resultsLock) { DownloadedFile df = GetDownloadedFile(url); if (df == null) { return; } df.In += length; Redraw(); } }
private void LoadPreviewData(DownloadedFile file) { var extension = Path.GetExtension(file.Path); switch (extension) { case ".txt": case ".js": case ".css": file.PreviewText = ReadChars(file.Path, 100); break; case ".jpg": case ".png": using (var bmpTemp = new Bitmap(file.Path)) { file.Image = new Bitmap(bmpTemp); } break; } }
private static async Task <DownloadedFile[]> DownloadFile(string url, HttpClientHandler clientHandler) { var client = new HttpClient(clientHandler); var downloadedFile = new DownloadedFile(); try { HttpResponseMessage responseMessage = await client.GetAsync(url); if (responseMessage.Content.Headers.ContentDisposition == null) { return(new DownloadedFile[0]); } downloadedFile.FileName = Path.Combine( Properties.Settings.Default.workingDirectory, responseMessage.Content.Headers.ContentDisposition.FileName); if (!Directory.Exists(Properties.Settings.Default.workingDirectory)) { Directory.CreateDirectory(Properties.Settings.Default.workingDirectory); } using (var httpStream = await responseMessage.Content.ReadAsStreamAsync()) using (var filestream = new FileStream( downloadedFile.FileName, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: true)) { await httpStream.CopyToAsync(filestream, 4096); } } // TODO: improve catch (Exception ex) { return(new DownloadedFile[0]); } return(new[] { downloadedFile }); }
public static DownloadedFile GetHttpAsOctetStream(string apiToken, string path) { try { System.Net.ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path); request.Method = "GET"; request.Headers.Add("Authorization", "Basic " + apiToken); request.Accept = ESL_ACCEPT_TYPE_APPLICATION_OCTET_STREAM; SetProxy(request); WebResponse response = request.GetResponse(); using (Stream responseStream = response.GetResponseStream()) { var memoryStream = new MemoryStream(); CopyTo(responseStream, memoryStream); byte[] result = memoryStream.ToArray(); DownloadedFile downloadedFile = new DownloadedFile("", result); return(downloadedFile); } } catch (WebException e) { using (var stream = e.Response.GetResponseStream()) using (var reader = new StreamReader(stream)) { string errorDetails = reader.ReadToEnd(); throw new EslServerException(String.Format("{0} HTTP {1} on URI {2}. Optional details: {3}", e.Message, ((HttpWebResponse)e.Response).Method, e.Response.ResponseUri, errorDetails), errorDetails, e); } } catch (Exception e) { throw new EslException("Error communicating with esl server. " + e.Message, e); } }
/// <summary> /// Methode de chargement de fichier à partir du FileAccessor. /// </summary> /// <param name="fileAccessorName">Nom du FileAccessor.</param> /// <param name="pk">Clef primaire de l'objet pointant vers le fichier.</param> /// <returns>Le contenu du fichier.</returns> public DownloadedFile GetFile(string fileAccessorName, object pk) { if (fileAccessorName == null) { throw new ArgumentNullException("fileAccessorName"); } if (pk == null) { throw new ArgumentNullException("pk"); } Accessor accessor = _fileAccessors[fileAccessorName]; object obj; ChannelFactory factory; object service = GetService(accessor.ContractType, out factory); try { obj = accessor.Method.Invoke(service, new object[] { pk }); } finally { if (factory != null) { factory.Close(); } } DownloadedFile file = obj as DownloadedFile; if (file == null) { throw new FileNotFoundException(); } return(file); }
async public static Task <bool> UploadFile(string titleId, DownloadedFile fileInfo) { var currentPlayFabTitleId = PlayFabSettings.TitleId; var currentDevKey = PlayFabSettings.DeveloperSecretKey; var title = FindTitle(titleId); PlayFabSettings.TitleId = titleId; PlayFabSettings.DeveloperSecretKey = title.SecretKey; var key = fileInfo.FilePath.Split('/').ToList()[fileInfo.FilePath.Split('/').ToList().Count - 1]; var type = MimeMapping.GetMimeMapping(key); var result = await PlayFabAdminAPI.GetContentUploadUrlAsync( new GetContentUploadUrlRequest() { Key = fileInfo.Data.Key, ContentType = type } ); if (result.Error != null) { Console.WriteLine(PlayFabUtil.GetErrorReport(result.Error)); return(false); } PlayFabSettings.TitleId = currentPlayFabTitleId; PlayFabSettings.DeveloperSecretKey = currentDevKey; bool success = await PlayFabExtensions.UploadFile(result.Result.URL, fileInfo.FilePath); if (!success) { return(false); } return(true); }
/// <summary> /// Пытается скачать и вернуть один файл по указанной ссылке с указанными параметрами. /// В случае ошибки возвращает NULL и сообщение об ошибке в ввыводном параметре. /// </summary> /// <param name="DownloadURI">Ссылка на скачивание файла</param> /// <param name="Referer">Referer - ссылка на страницу, с которой якобы осуществлён запрос на скачивание файла</param> /// <param name="UserAgent">UserAgent, который будет использоваться при выполнении запроса</param> /// <param name="ErrorMessage">выводной параметр, содержащий сообщение об ошибке, если она произошла, /// или NULL в случае успеха</param> /// <returns></returns> internal static DownloadedFile TryDownloadFile(Uri DownloadURI, Uri Referer, String UserAgent, out String ErrorMessage) { if (DownloadURI == null) { throw new ArgumentNullException("DownloadURI", "DownloadURI не может быть NULL"); } if (Referer == null) { throw new ArgumentNullException("Referer", "Referer не может быть NULL"); } if (UserAgent == null) { throw new ArgumentNullException("UserAgent", "UserAgent не может быть NULL"); } ErrorMessage = null; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(DownloadURI); request.Method = "GET"; request.UserAgent = UserAgent; request.Referer = Referer.OriginalString; try { using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { if (response.StatusCode != HttpStatusCode.OK) { ErrorMessage = "Код ответа сервера не является 200 OK: " + response.StatusCode.ToString(); return null; } String content_disposition = response.Headers["Content-Disposition"]; String content_type = response.Headers["Content-Type"]; String raw_content_length = response.Headers["Content-Length"]; Int32 content_length = raw_content_length.TryParseNumber<Int32>(NumberStyles.Integer, null, -10); if (content_disposition.HasAlphaNumericChars() == false || content_type.HasAlphaNumericChars() == false || raw_content_length.HasAlphaNumericChars() == false || raw_content_length.Equals("0", StringComparison.InvariantCultureIgnoreCase) == true) { ErrorMessage = String.Format("Значения некоторых хидеров некорректны. "+ "Content-Disposition: {0}; Content-Type: {1}; Content-Length: {2}.", content_disposition.ToStringS("NULL"), content_type.ToStringS("NULL"), raw_content_length.ToStringS("NULL")); return null; } if (content_length == -10) { ErrorMessage = "Значение HTTP-хидера Content-Length некорректно и является : " + raw_content_length; return null; } ////application/mp3 //if (content_type.Equals("application/octet-stream", StringComparison.OrdinalIgnoreCase) == false) //{ // ErrorMessage = "Значение HTTP-хидера Content-Type некорректно и является : " + content_type; // return null; //} if (content_disposition.Contains("attachment", StringComparison.OrdinalIgnoreCase) == false && content_disposition.Contains("filename", StringComparison.OrdinalIgnoreCase) == false) { ErrorMessage = "Значение HTTP-хидера Content-Disposition некорректно и является : " + content_disposition; return null; } String original_filename_without_ext = StringTools.SubstringHelpers.GetSubstringToToken(content_disposition, "filename=", false, StringTools.Direction.FromEndToStart, StringComparison.OrdinalIgnoreCase); Int32 out_pos; String ext = StringTools.SubstringHelpers.GetInnerStringBetweenTokens (response.ResponseUri.ToString(), "ex=", "&", 0, StringComparison.OrdinalIgnoreCase, out out_pos); String original_filename_full = ext.HasAlphaNumericChars() == false || original_filename_without_ext.EndsWith(ext, StringComparison.OrdinalIgnoreCase) == true ? original_filename_without_ext : original_filename_without_ext + ext; MemoryStream file_body = new MemoryStream(content_length); using (Stream receiveStream = response.GetResponseStream()) { StreamTools.CopyStream(receiveStream, file_body, false, false); } DownloadedFile output = new DownloadedFile(original_filename_full, content_length, file_body); return output; } } catch (WebException wex) { ErrorMessage = String.Format( "Ошибка при попытке скачать файл. Статус ответа сервера: {0}. Сообщение об ошибке: {1}.", wex.Status.ToString(), wex.TotalMessage()); return null; } catch (Exception ex) { ErrorMessage = "Прозошло неизвестное исключение: " + ex.TotalMessage(); return null; } }
/// <summary> /// Сохраняет указанный файл в указанную папку с опционально указанным именем /// </summary> /// <param name="ReceivedFile"></param> /// <param name="FolderPath"></param> /// <param name="NewFilename">Необязательное новое имя файла. Если не указано или некорректно, будет использовано то имя файла, которое было получено с сервера</param> /// <returns>Сообщение об ошибке, если она произошла, или NULL, если всё успешно</returns> public static String TrySaveDownloadedFileToDisk (DownloadedFile ReceivedFile, String FolderPath, String NewFilename = null) { if (ReceivedFile == null) { throw new ArgumentNullException("ReceivedFile", "ReceivedFile не может быть NULL"); } if (FolderPath == null) { throw new ArgumentNullException("FolderPath", "FolderPath не может быть NULL"); } String filename = FilePathTools.IsValidFilename(NewFilename) == true ? NewFilename : ReceivedFile.Filename; String full_filename = Path.Combine(FolderPath, filename); try { using (FileStream fs = new FileStream(full_filename, FileMode.Create, FileAccess.Write, FileShare.None)) { StreamTools.CopyStream(ReceivedFile.FileBody, fs, true, false); return null; } } catch (Exception ex) { return ex.TotalMessage(); } }
override public void Execute() { sdkJournalEntries = eslClient.PackageService.GetJournalEntries(senderUID); csvJournalEntries = eslClient.PackageService.GetJournalEntriesAsCSV(senderUID); }
/// <summary> /// Пытается асинхронно, не блокируя основной поток выполнения, /// сохранить указанный файл в указанную папку с опционально указанным именем файла /// </summary> /// <param name="ReceivedFile"></param> /// <param name="FolderPath"></param> /// <param name="NewFilename">Необязательное новое имя файла. Если не указано или некорректно, будет использовано то имя файла, которое было получено с сервера</param> /// <returns>Сообщение об ошибке, если она произошла, или NULL, если всё успешно</returns> public static async Task<String> TrySaveDownloadedFileToDiskAsync (DownloadedFile ReceivedFile, String FolderPath, String NewFilename = null) { const Int32 buffer_size = 1024*16; if (ReceivedFile == null) { throw new ArgumentNullException("ReceivedFile", "ReceivedFile не может быть NULL"); } if (FolderPath == null) { throw new ArgumentNullException("FolderPath", "FolderPath не может быть NULL"); } String filename = FilePathTools.IsValidFilename(NewFilename) == true ? NewFilename : ReceivedFile.Filename; String full_filename = Path.Combine(FolderPath, filename); try { using (FileStream fs = new FileStream(full_filename, FileMode.Create, FileAccess.Write, FileShare.None, buffer_size, FileOptions.Asynchronous)) { ReceivedFile.FileBody.Position = 0; await ReceivedFile.FileBody.CopyToAsync(fs, buffer_size); return null; } } catch (Exception ex) { return ex.TotalMessage(); } }
static void RunProgram() { string jobId = DateTime.Now.ToString("yyyyMMddhhmmss"); try { Log.Debug("Processing in progress..."); // Basic validation for sftp config if (string.IsNullOrEmpty(AppSettings.SftpHost) || string.IsNullOrEmpty(AppSettings.SftpUsername) || string.IsNullOrEmpty(AppSettings.SftpPassword) || string.IsNullOrEmpty(AppSettings.SftpDownloadDirectory) || string.IsNullOrEmpty(AppSettings.SftpDownloadFilename) || string.IsNullOrEmpty(AppSettings.SftpUploadDirectory)) { Log.Error("Missing SFTP Configuration."); SendSystemEmail(Constants.StatusEmailSubject + " - Configuration Error", "Missing SFTP Configuration."); return; } if (!SftpHelper.VerifyConnection(out string error)) { Log.Error("Unable to connect to SFTP, " + error); SendSystemEmail(Constants.StatusEmailSubject + " - Failed to connect to SFTP", "Please ensure the SFTP Configuration is valid.<h3>Exception</h3>" + error); return; } string downloadFolderPath = Path.Combine(AppSettings.DownloadPath, jobId); string workingFolderPath = Path.Combine(AppSettings.WorkingPath, jobId); var parser = new DataParser(); parser.LoadParserSettings(); var allData = new List <ParsedData>(); var failedRows = new List <Tuple <int, string, string> >(); // Download from sftp remote server DownloadedFile downloadedFile = SftpHelper.DownloadFile(AppSettings.SftpDownloadDirectory, AppSettings.SftpDownloadFilename, downloadFolderPath); // Terminate if no files available for download if (downloadedFile == null) { Log.Error("No files available for download"); SendSystemEmail(Constants.StatusEmailSubject, "No files available for download."); FileHelper.DeleteFile(downloadFolderPath); return; } //Read from downloaded csv and parse into order list var rows = CsvUtil.GetRecords <InOrderConfirmation>(downloadedFile.FullName); var rowIndex = 1; //csv header consider as 1 row foreach (var row in rows) { rowIndex++; var data = parser.ParseXML(row.OrderXML, out List <string> errors, enableThrowException: false); if (data != null && !errors.Any()) { allData.AddRange(data); } else { failedRows.Add(new Tuple <int, string, string>(rowIndex, row.OrderNumber, string.Join(",", errors))); } } // Terminate if any invalid rows if (failedRows.Any()) { Log.Error("Invalid Records Found"); SendParseErrorEmail(jobId, failedRows); ArchiveFiles(jobId, true); return; } // Process Export and Upload var results = new Dictionary <string, UploadResult>(); if (allData.Any()) { FileHelper.CreateDirectory(workingFolderPath); // Flatten the data var flattenData = allData.GroupBy(x => x.Name) .Select(group => new ParsedData { Name = group.Key, Data = group.SelectMany(item => item.Data).Distinct().ToList() }).ToList(); // Export csv and upload to SFTP foreach (var item in flattenData) { string filename = item.Name + "_" + jobId + ".csv"; string path = workingFolderPath + " /" + filename; try { parser.ExportAsCsv(item.Data, path); var result = UploadExportCsvToSftp(AppSettings.SftpUploadDirectory, path); results.Add(filename, result); } catch (Exception ex) { results.Add(filename, new UploadResult { Success = false, ErrorMessage = "Failed to export csv for upload. Exception: " + ex.ToString() }); } } } // Send status result email SendStatusEmail(jobId, results); } catch (Exception ex) { Log.Error("RunProgram Error", ex); SendSystemEmail(Constants.StatusEmailSubject + " - Task Error", ex.ToString()); } finally { ArchiveFiles(jobId, true); } }
/// <summary> /// load model from list /// </summary> /// <param name="filepath">target file path</param> public async void LoadModelFromDB(string filepath) { if (!downloader.GetDownloadStatus()) { DownloadedFile data = null; await Task.Run(() => { data = downloader.DownloadFileFromStorage(filepath, auth.User).Result; }); var taggedObject = GameObject.FindGameObjectsWithTag("Player"); foreach (GameObject target in taggedObject) { Destroy(target); } if (GameObject.Find("Live2DCubism3CVVTuberExample")) { Destroy(GameObject.Find("Live2DCubism3CVVTuberExample")); } if (GameObject.Find("VRMCVVTuberExample")) { Destroy(GameObject.Find("VRMCVVTuberExample")); } //loading model if (data.Type == "application/json") { Debug.Log("Stasrt loading Live2D"); live2dloader.LoadModelFromCompressor(data.File); //add Vtuber system to this model await Task.Delay(500); CubismModel target = GameObject.FindGameObjectWithTag("Player").GetComponent <CubismModel>(); target.gameObject.AddComponent <ObjectMover>(); target.gameObject.AddComponent <LookAround>(); /* * target.gameObject.GetComponent<Animator>().runtimeAnimatorController = animation; * target.transform.Translate(0, 1.5f, 0); * var loader = Instantiate(live2DcvLoader); * loader.name = live2DcvLoader.name; * * HRC = GameObject.Find("Live2DCubism3CVVTuberExample/Live2DCubism3HeadRotationController").GetComponent<Live2DCubism3HeadRotationController>(); * FAC = GameObject.Find("Live2DCubism3CVVTuberExample/Live2DCubism3FaceAnimationController").GetComponent<Live2DCubism3FaceAnimationController>(); * CVVCM = GameObject.Find("Live2DCubism3CVVTuberExample/Live2DCubism3ControllManager").GetComponent<CVVTuberControllManager>(); * GameObject.Find("Live2DCubism3CVVTuberExample/DlibFaceLandmarkGetter").GetComponent<DlibFaceLandmarkGetter>().screen = GameObject.Find("Canvas/RawImage").GetComponent<RawImage>(); * HRC.target = target; * FAC.live2DCubism3Model = target; * GameObject.Find("Live2DCubism3CVVTuberExample/Live2DCubism3KeyInputExpressionController").GetComponent<Live2DCubism3KeyInputExpressionController>().target = target.gameObject.GetComponent<Animator>(); * GameObject.Find("Live2DCubism3CVVTuberExample/Live2DCubism3ControllManager").GetComponent<CVVTuberControllManager>().enabled = true; */ } else if (data.Type == "application/vrm") { Debug.Log("Stasrt loading VRM"); await vrmloader.LoadVRM(data.File); /* * var Instance = Instantiate(VRMcvLoader); * Instance.name = VRMcvLoader.name; * var currentGameObject = GameObject.Find("VRMCVVTuberExample/VRMControllManager"); * var loader = currentGameObject.GetComponent<CVVTuber.VRM.VRMLoader>(); * loader.meta = GameObject.FindGameObjectWithTag("Player").GetComponent<VRM.VRMMeta>(); * GameObject.Find("VRMCVVTuberExample/DlibFaceLandmarkGetter").GetComponent<DlibFaceLandmarkGetter>().screen = GameObject.Find("Canvas/RawImage").GetComponent<RawImage>(); * currentGameObject.GetComponent<CVVTuber.VRM.VRMCVVTuberControllManager>().enabled = true; */ } } else { Debug.LogError("Download in progress"); } }
public static DownloadedFile DownloadFile(ConnectionInfo connectionInfo, string remoteDirectory, string remoteFileName, string localDirectory) { DownloadedFile downloadedFile = null; try { using (var sftp = new SftpClient(connectionInfo)) { try { Log.Trace("Connecting to " + connectionInfo.Host + " ..."); sftp.Connect(); Log.Trace("Connected to " + connectionInfo.Host); } catch (Exception ex) { Log.Error("Could not connect to " + connectionInfo.Host + " server as " + connectionInfo.Username, ex); } if (sftp.IsConnected) { var files = sftp.ListDirectory(remoteDirectory); if (!Directory.Exists(localDirectory)) { Directory.CreateDirectory(localDirectory); Log.Trace("Local download directory " + localDirectory + " Created."); } foreach (var file in files) { if (!file.IsDirectory && !file.Name.StartsWith(".") && file.Name.ToLower().Trim().Equals(remoteFileName.ToLower().Trim())) //&& file.LastWriteTime.Date == DateTime.Today { string downloadPathName = Path.Combine(localDirectory, file.Name); if (File.Exists(downloadPathName)) { Log.Warn("File " + file.Name + " Exists"); } else { Log.Debug("Downloading file: " + file.Name); try { using (var fs = File.OpenWrite(downloadPathName)) { sftp.DownloadFile(file.FullName, fs); downloadedFile = new DownloadedFile(file, file.Name, downloadPathName, DateTime.Now); } Log.Debug("Downloaded file: " + file.Name); } catch (Exception ex) { Log.Error("Failed to download file: " + file.Name, ex); } } // get out of the loop break; } } sftp.Disconnect(); } else { Log.Warn("Could not download files because client was not connected."); } } } catch (Exception ex) { Log.Error("DownloadFiles Error", ex); } return(downloadedFile); }
private void ReceiveDocuments(InboundDocumentType documentType, WaybillSource waybillSource, Uri uri) { var haveErrors = false; if (uri == null) { return; } _logger.InfoFormat("Попытка получения документов с FTP поставщика (код поставщика: {0}).\nТип документов: {1}.\nUrl: {2}", waybillSource.Id, documentType.DocType.GetDescription(), uri); try { using (var ftpClient = waybillSource.CreateFtpClient()) { ftpClient.Connect(uri.Host, uri.Port); ftpClient.Authenticate(waybillSource.UserName, waybillSource.Password); ftpClient.SetCurrentDir(uri.PathAndQuery); var files = ftpClient.GetList(); foreach (var file in files.Tables["DirInfo"].AsEnumerable()) { if (Convert.ToBoolean(file["IsDirectory"])) { continue; } Cancellation.ThrowIfCancellationRequested(); var source = file["Name"].ToString(); var sourceDate = Convert.ToDateTime(file["Date"]); var sourceLength = Convert.ToInt64(file["Size"]); var dest = Path.Combine(DownHandlerPath, source); try { ftpClient.ReceiveFile(source, dest); var destLenth = new FileInfo(dest).Length; if (destLenth != sourceLength) { _logger.WarnFormat("Не совпадает размер загруженного файла {0} {1} размер на ftp {2} полученный рамер {3}", uri, source, sourceLength, destLenth); continue; } var downloadedFile = new DownloadedFile(dest, sourceDate); ProcessFile(documentType, waybillSource, downloadedFile); ftpClient.DeleteFile(source); } catch (Exception e) { haveErrors = true; LogError(e, waybillSource, uri, documentType); } } } if (!haveErrors && _failedSources.Contains(waybillSource.Id)) { waybillSource.LastError = DateTime.Now; _failedSources.Remove(waybillSource.Id); _logger.WarnFormat("После возникновения ошибок загрузка накладных прошла успешно. Код поставщика: {0}", waybillSource.Id); } } catch (Exception e) { LogError(e, waybillSource, uri, documentType); } }
override public void Execute() { // Signer1 with 1 attachment requirement signer1 = SignerBuilder.NewSignerWithEmail(email1) .WithFirstName("John") .WithLastName("Smith") .WithCustomId(SIGNER1_ID) .WithAttachmentRequirement(AttachmentRequirementBuilder.NewAttachmentRequirementWithName(NAME1) .WithDescription(DESCRIPTION1) .IsRequiredAttachment() .Build()) .Build(); // Signer2 with 2 attachment requirements Signer signer2 = SignerBuilder.NewSignerWithEmail(email2) .WithFirstName("Patty") .WithLastName("Galant") .WithCustomId(SIGNER2_ID) .WithAttachmentRequirement(AttachmentRequirementBuilder.NewAttachmentRequirementWithName(NAME2) .WithDescription(DESCRIPTION2) .Build()) .WithAttachmentRequirement(AttachmentRequirementBuilder.NewAttachmentRequirementWithName(NAME3) .WithDescription(DESCRIPTION3) .IsRequiredAttachment() .Build()) .Build(); DocumentPackage superDuperPackage = PackageBuilder.NewPackageNamed(PackageName) .DescribedAs("This is a package created using the eSignLive SDK") .WithSigner(signer1) .WithSigner(signer2) .WithDocument(DocumentBuilder.NewDocumentNamed("test document") .FromStream(fileStream1, DocumentType.PDF) .WithSignature(SignatureBuilder.SignatureFor(email1) .Build()) .Build()) .Build(); packageId = eslClient.CreateAndSendPackage(superDuperPackage); retrievedPackage = eslClient.GetPackage(packageId); attachment1Id = retrievedPackage.GetSigner(email1).GetAttachmentRequirement(NAME1).Id; signer1 = retrievedPackage.GetSigner(email1); signer1Attachments = retrievedPackage.GetSigner(email1).Attachments; signer2Attachments = retrievedPackage.GetSigner(email2).Attachments; signer1Att1 = signer1Attachments[0]; signer2Att1 = signer2Attachments[0]; signer2Att2 = signer2Attachments[1]; retrievedSigner1Att1RequirementStatus = signer1Att1.Status; retrievedSigner2Att1RequirementStatus = signer2Att1.Status; retrievedSigner2Att2RequirementStatus = signer2Att2.Status; // Upload attachment for signer1 byte[] attachment1ForSigner1FileContent = new StreamDocumentSource(attachmentInputStream1).Content(); attachment1ForSigner1FileSize = attachment1ForSigner1FileContent.Length; eslClient.UploadAttachment(packageId, signer1Att1.Id, ATTACHMENT_FILE_NAME1, attachment1ForSigner1FileContent, SIGNER1_ID); eslClient.UploadAttachment(packageId, signer2Att1.Id, ATTACHMENT_FILE_NAME2, new StreamDocumentSource(attachmentInputStream2).Content(), SIGNER2_ID); eslClient.UploadAttachment(PackageId, signer2Att2.Id, ATTACHMENT_FILE_NAME3, new StreamDocumentSource(attachmentInputStream3).Content(), SIGNER2_ID); // Sender rejects Signer1's uploaded attachment eslClient.AttachmentRequirementService.RejectAttachment(packageId, signer1, NAME1, REJECTION_COMMENT); retrievedPackageAfterRejection = eslClient.GetPackage(packageId); retrievedSigner1Att1RequirementStatusAfterRejection = retrievedPackageAfterRejection.GetSigner(email1).GetAttachmentRequirement(NAME1).Status; retrievedSigner1Att1RequirementSenderCommentAfterRejection = retrievedPackageAfterRejection.GetSigner(email1).GetAttachmentRequirement(NAME1).SenderComment; // Sender accepts Signer1's uploaded attachment eslClient.AttachmentRequirementService.AcceptAttachment(packageId, signer1, NAME1); retrievedPackageAfterAccepting = eslClient.GetPackage(packageId); retrievedSigner1Att1RequirementStatusAfterAccepting = retrievedPackageAfterAccepting.GetSigner(email1).GetAttachmentRequirement(NAME1).Status; retrievedSigner1Att1RequirementSenderCommentAfterAccepting = retrievedPackageAfterAccepting.GetSigner(email1).GetAttachmentRequirement(NAME1).SenderComment; // Download signer1's attachment DownloadedFile downloadedAttachment = eslClient.AttachmentRequirementService.DownloadAttachmentFile(packageId, attachment1Id); System.IO.File.WriteAllBytes(downloadedAttachment.Filename, downloadedAttachment.Contents); // Download all attachments for the package DownloadedFile downloadedAllAttachmentsForPackage = eslClient.AttachmentRequirementService.DownloadAllAttachmentFilesForPackage(packageId); System.IO.File.WriteAllBytes(DOWNLOADED_ALL_ATTACHMENTS_FOR_PACKAGE_ZIP, downloadedAllAttachmentsForPackage.Contents); // Download all attachments for the signer1 in the package DownloadedFile downloadedAllAttachmentsForSigner1InPackage = eslClient.AttachmentRequirementService.DownloadAllAttachmentFilesForSignerInPackage(retrievedPackage, signer1); System.IO.File.WriteAllBytes(DOWNLOADED_ALL_ATTACHMENTS_FOR_SIGNER1_IN_PACKAGE_ZIP, downloadedAllAttachmentsForSigner1InPackage.Contents); // Download all attachments for the signer2 in the package DownloadedFile downloadedAllAttachmentsForSigner2InPackage = eslClient.AttachmentRequirementService.DownloadAllAttachmentFilesForSignerInPackage(retrievedPackage, signer2); System.IO.File.WriteAllBytes(DOWNLOADED_ALL_ATTACHMENTS_FOR_SIGNER2_IN_PACKAGE_ZIP, downloadedAllAttachmentsForSigner2InPackage.Contents); downloadedAttachemnt1 = new FileInfo(downloadedAttachment.Filename); }
private List <DocumentReceiveLog> ProcessWaybill(DocType documentType, WaybillSource source, DownloadedFile downloadedFile) { var documentLogs = new List <DocumentReceiveLog>(); var reader = new SupplierFtpReader(); var addressIds = With.Connection(c => reader.ParseAddressIds(c, source.Id, downloadedFile.FileName, downloadedFile.FileName)); foreach (var addressId in addressIds) { // Если накладная - это архив, разархивируем логируем каждый файл и копируем в папку клиенту var waybillFiles = new[] { downloadedFile.FileName }; try { waybillFiles = FileHelper.TryExtractArchive(downloadedFile.FileName, downloadedFile.FileName + BaseSourceHandler.ExtrDirSuffix) ?? waybillFiles; } catch (ArchiveHelper.ArchiveException e) { _logger.Warn($"Ошибка при извлечении файлов из архива {downloadedFile.FileName}", e); WaybillService.SaveWaybill(downloadedFile.FileName); continue; } foreach (var file in waybillFiles) { var isNew = IsNewWaybill(source, (uint)addressId, Path.GetFileName(file), new FileInfo(file).Length); if (!isNew) { _logger.DebugFormat("Файл {0} не является новой накладной, не обрабатываем его", file); continue; } var log = DocumentReceiveLog.LogNoCommit(source.Id, (uint)addressId, file, documentType, "Получен с клиентского FTP"); _logger.InfoFormat("WaybillFtpSourceHandler: обработка файла {0}", file); documentLogs.Add(log); } } return(documentLogs); }
public void ProcessFile(InboundDocumentType documentType, WaybillSource waybillSource, DownloadedFile downloadedFile) { var logs = ProcessWaybill(documentType.DocType, waybillSource, downloadedFile); // Обработка накладной(или отказа), помещение ее в папку клиенту new WaybillService().Process(logs); }
private void Event4(DownloadedFile file, Bitmap bmp) { this.AddToLog(String.Format( "Этап 4: Обложка успешно скачана и преобразована в изображение. "+ "Название - {0}, размер - {1} байт, тип - {2}, линейные размеры - {3}.", file.Filename, file.Contentlength, ImageTools.GetImageFormat(bmp).ToString(), bmp.Size.ToString())); }
/// <summary> /// Пытается асинхронно, не блокируя текущий поток, скачать и вернуть один файл по указанной ссылке /// с указанными параметрами. В случае ошибки возвращает NULL. /// </summary> /// <param name="DownloadURI">Ссылка на скачивание файла</param> /// <param name="Referer">Referer - ссылка на страницу, с которой якобы осуществлён запрос на скачивание файла</param> /// <param name="UserAgent">UserAgent, который будет использоваться при выполнении запроса</param> /// <returns></returns> internal static async Task<DownloadedFile> TryDownloadFileAsync(Uri DownloadURI, Uri Referer, String UserAgent) { if (DownloadURI == null) { throw new ArgumentNullException("DownloadURI", "DownloadURI не может быть NULL"); } if (Referer == null) { throw new ArgumentNullException("Referer", "Referer не может быть NULL"); } if (UserAgent == null) { throw new ArgumentNullException("UserAgent", "UserAgent не может быть NULL"); } HttpWebRequest request = (HttpWebRequest)WebRequest.Create(DownloadURI); request.Method = "GET"; request.UserAgent = UserAgent; request.Referer = Referer.ToString(); try { using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync()) { if (response.StatusCode != HttpStatusCode.OK) { return null; } String content_disposition = response.Headers["Content-Disposition"]; String content_type = response.Headers["Content-Type"]; String raw_content_length = response.Headers["Content-Length"]; Int32 content_length = raw_content_length.TryParseNumber<Int32>(NumberStyles.Integer, null, -10); if (content_disposition.HasAlphaNumericChars() == false || content_type.HasAlphaNumericChars() == false || raw_content_length.HasAlphaNumericChars() == false || raw_content_length.Equals("0", StringComparison.InvariantCultureIgnoreCase) == true) { return null; } if (content_length == -10) { return null; } ////application/mp3 //if (content_type.Equals("application/octet-stream", StringComparison.OrdinalIgnoreCase) == false) //{ // return null; //} if (content_disposition.Contains("attachment", StringComparison.OrdinalIgnoreCase) == false || content_disposition.Contains("filename", StringComparison.OrdinalIgnoreCase) == false) { return null; } String original_filename_without_ext = StringTools.SubstringHelpers.GetSubstringToToken(content_disposition, "filename=", false, StringTools.Direction.FromEndToStart, StringComparison.OrdinalIgnoreCase); Int32 out_pos; String ext = StringTools.SubstringHelpers.GetInnerStringBetweenTokens (response.ResponseUri.ToString(), "ex=", "&", 0, StringComparison.OrdinalIgnoreCase, out out_pos); String original_filename_full = ext.HasAlphaNumericChars() == false || original_filename_without_ext.EndsWith(ext, StringComparison.OrdinalIgnoreCase) == true ? original_filename_without_ext : original_filename_without_ext + ext; MemoryStream file_body = new MemoryStream(content_length); using (Stream receiveStream = response.GetResponseStream()) { if (receiveStream == null) { return null; } await receiveStream.CopyToAsync(file_body); } DownloadedFile output = new DownloadedFile(original_filename_full, content_length, file_body); return output; } } catch { return null; } }
protected virtual void OnDownloadedFile(object sender, Fetching.EventHandlers.DownloadedFileEventArgs e) { DownloadedFile?.Invoke(this, e); }
/// <summary> /// Пытатся преобразовать указанный файл <paramref name="ReceivedFile"/> в битовую карту и в случае успеха вернуть её /// </summary> /// <param name="ReceivedFile"></param> /// <returns></returns> internal static Bitmap TryConvertFileToImage(DownloadedFile ReceivedFile) { if (ReceivedFile == null) { throw new ArgumentNullException("ReceivedFile", "ReceivedFile не может быть NULL"); } Bitmap output; try { output = new Bitmap(ReceivedFile.FileBody); } catch { output = null; } return output; }