public void OnDownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e) { if (FileProgress != null) { FileProgress.Invoke(sender, e); } }
protected override void OnMuxing(object sender, DownloadCompletedEventArgs e) { // Separate file extension. DownloadItemData IData = e.DownloadInfo.Data as DownloadItemData; FileProgress VideoFile = e.DownloadInfo.Files.FirstOrDefault(f => f.Type == StreamType.Video); FileProgress AudioFile = e.DownloadInfo.Files.FirstOrDefault(f => f.Type == StreamType.Audio); string SrcFile = IData.Media.FileName != null ? Settings.NaturalGroundingFolder + IData.Media.FileName : null; CompletionStatus Result = CompletionStatus.Success; if (IData.Media.FileName != null && File.Exists(SrcFile) && (VideoFile == null || AudioFile == null)) { // Upgrade audio or video FFmpegProcess MInfo = MediaInfo.GetFileInfo(SrcFile); string VideoFormat = VideoFile != null?Path.GetExtension(VideoFile.Destination).TrimStart('.') : MInfo.VideoStream?.Format; string AudioFormat = AudioFile != null?Path.GetExtension(AudioFile.Destination).TrimStart('.') : MInfo.AudioStream?.Format; string VideoDestExt = GetFinalExtension(VideoFormat, AudioFormat); e.DownloadInfo.Destination = e.DownloadInfo.DestinationNoExt + VideoDestExt; Result = MediaMuxer.Muxe(VideoFile?.Destination ?? SrcFile, AudioFile?.Destination ?? SrcFile, e.DownloadInfo.Destination); } if (Result == CompletionStatus.Success && File.Exists(SrcFile)) { FileOperationAPIWrapper.MoveToRecycleBin(SrcFile); } e.DownloadInfo.Status = Result == CompletionStatus.Success ? DownloadStatus.Done : DownloadStatus.Failed; }
public void FileCreated(FileProgress progress) { if (progress.CheckUpdate(divider)) { Console.WriteLine($"Created {progress.CurrentCount}/{progress.TotalCount} files, {progress.CurrentSize}/{progress.TotalSize} bytes"); } }
internal async Task <FileSystemResult <IFile> > InternalCreateFile(DirectoryImplementation dir, string name, Stream readstream, CancellationToken token, IProgress <FileProgress> progress, Dictionary <string, object> properties) { if (properties == null) { properties = new Dictionary <string, object>(); } string path = name; if (dir.Parent != null) { path = Path.Combine(dir.FullName, path); } Stream s = File.Open(path, FileMode.Create, FileAccess.Write); byte[] block = new byte[1024 * 128]; long left = readstream.Length; do { int size = (int)Math.Min(left, block.Length); int rsize = await readstream.ReadAsync(block, 0, size, token); await s.WriteAsync(block, 0, rsize, token); left -= rsize; FileProgress p = new FileProgress { Percentage = ((float)(readstream.Length - left) * 100) / readstream.Length, TotalSize = readstream.Length, TransferSize = readstream.Length - left }; progress.Report(p); } while (left > 0 && !token.IsCancellationRequested); s.Close(); if (token.IsCancellationRequested) { try { File.Delete(path); } catch { // ignored } return(new FileSystemResult <IFile>("Transfer canceled")); } FileInfo finfo = new FileInfo(path); if (properties.Any(a => a.Key.Equals("ModifiedDate", StringComparison.InvariantCultureIgnoreCase))) { finfo.LastWriteTime = (DateTime)properties.First(a => a.Key.Equals("ModifiedDate", StringComparison.InvariantCultureIgnoreCase)).Value; } if (properties.Any(a => a.Key.Equals("CreatedDate", StringComparison.InvariantCultureIgnoreCase))) { finfo.CreationTime = (DateTime)properties.First(a => a.Key.Equals("CreatedDate", StringComparison.InvariantCultureIgnoreCase)).Value; } LocalFile f = new LocalFile(finfo, FS); return(new FileSystemResult <IFile>(f)); }
public void SetValueAnimated(int val) { DoubleAnimation da = new DoubleAnimation(); da.To = val; da.Duration = new System.Windows.Duration(new System.TimeSpan(0, 0, 0, 0, 500)); FileProgress.BeginAnimation(System.Windows.Controls.ProgressBar.ValueProperty, da); }
public FileProgress( FileProgress source, long bytesTransferred = 0 ) { this.filename = source.filename; this.totalBytes = source.totalBytes; this.bytesTransferred = bytesTransferred; }
private void OnFileProgress(FileProgress s) { Functions.TaskManager.ActiveTask.mre.WaitOne(); if (Functions.TaskManager.CancellationToken.IsCancellationRequested) { throw (new OperationCanceledException(Functions.TaskManager.CancellationToken.Token)); } Functions.TaskManager.ActiveTask.TaskStatusInfo = Framework.StringFormat.Format(Functions.SLM.Translate(nameof(Properties.Resources.TaskStatus_CopyingFile)), new { Percentage = s.Percentage.ToString("0.00"), TransferredBytes = s.Transferred, TotalBytes = s.Total }); }
/// <summary> /// Sets progress values /// </summary> /// <param name="progress">Current progress</param> public void SetProgress(ProgressInfo progress) { if (progress.Done) { Close(); } else { FileInfo.SetText(string.Format(FILE_INFO_TEMPLATE, progress.Description)); FileProgress.SetProgress(progress); } }
/// <summary> /// Sets progress values /// </summary> /// <param name="itemProgress">Current progress</param> /// <param name="totalProgress">Total progress</param> public void SetProgress(ProgressInfo itemProgress, ProgressInfo totalProgress) { if (totalProgress.Done) { Close(); } else { FileSourceInfo.SetText(string.Format(FILE_INFO_TEMPLATE, itemProgress.Description)); FileProgress.SetProgress(itemProgress); TotalFilesCount.SetText("Files:" + $"{totalProgress.Count.ToString("#")}/{totalProgress.TotalCount.ToString("#")}".PadLeft(TotalFilesCount.Width - 6)); TotalBytesCount.SetText("Bytes:" + $"{totalProgress.Proceded.ToString("#")}/{totalProgress.Total.ToString("#")}".PadLeft(TotalBytesCount.Width - 6)); TotalProgress.SetProgress(totalProgress); } }
/// <summary> /// Upload file stream to the server /// </summary> /// <param name="apiSession">api session</param> /// <param name="inputStream">stream for read from</param> /// <param name="fileName">file name</param> /// <param name="fileSize">file size in bytes</param> /// <param name="destFolderPath">destination folder like /path/to/folder </param> /// <param name="cancellationToken">cancellation tokern</param> /// <param name="overwriteFileifExists"></param> /// <returns></returns> public async Task UploadFileAsync(ApiSession apiSession, Stream inputStream, string fileName, long fileSize, string destFolderPath, CancellationToken cancellationToken, bool overwriteFileifExists = false) { if (apiSession == null) { throw new ArgumentNullException(nameof(apiSession)); } try { var spaceName = apiSession.SpaceName; string boundary = "EasyMorphCommandClient--------" + Guid.NewGuid().ToString("N"); string url = UrlHelper.JoinUrl("space", spaceName, "files", destFolderPath); using (var content = new MultipartFormDataContent(boundary)) { var downloadProgress = new FileProgress(fileName, fileSize); downloadProgress.StateChanged += DownloadProgress_StateChanged; using (cancellationToken.Register(() => downloadProgress.ChangeState(FileProgressState.Cancelled))) { using (var streamContent = new ProgressStreamContent(inputStream, downloadProgress)) { content.Add(streamContent, "files", Path.GetFileName(fileName)); var requestMessage = BuildHttpRequestMessage(overwriteFileifExists ? HttpMethod.Put : HttpMethod.Post, url, content, apiSession); using (requestMessage) { using (var response = await GetHttpClient().SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, cancellationToken)) { await HandleResponse(response); } } } } } } catch (Exception ex) when(ex.InnerException != null && ex.InnerException is WebException) { var einner = ex.InnerException as WebException; if (einner.Status == WebExceptionStatus.ConnectionClosed) { throw new MorphApiNotFoundException("Specified folder not found"); } } }
/// <summary> /// Validates the file header. /// </summary> /// <param name="fileHeader">The file header.</param> /// <param name="fileProgress">The file progress.</param> /// <param name="errorMessage">The error message.</param> /// <returns></returns> protected virtual bool ValidateFileHeader(string fileHeader, FileProgress fileProgress, out string errorMessage) { if (!string.IsNullOrEmpty(HeaderLineV6) && HeaderLineV6.ToUpper().EqualsIgnoreCase(fileHeader.Trim().ToUpper())) { errorMessage = string.Format("The input file [{0}] does not appear to be of the correct file type.", fileProgress.FileName) + "\n\n" + "If you are trying to import the reports genrated by AHRQ QI v6.0 ICD-10 version, please note that MONAHRQ will not be able to process the file and can't import it at this time."; return(false); } var acceptedHeaders = _headerLine.ToUpper().Split(new string[] { "|" }, StringSplitOptions.None).ToList(); var result = acceptedHeaders.Any(h => fileHeader.ToUpper().StartsWith(h)); errorMessage = result ? string.Empty : string.Format("The input file [{0}] does not appear to be of the correct file type.", fileProgress.FileName); return(result); }
private void UpdateCompleate(int PatchStatus) { if (PatchStatus == 2) { // Restart as alt name so we can patch ourself if (File.Exists(MeAlt)) { System.Diagnostics.Process.Start(Directory.GetCurrentDirectory() + "\\" + MeAlt); } else { MessageBox.Show("Error: Can't Find file " + MeAlt, "Can't find file", MessageBoxButtons.OK, MessageBoxIcon.Error); ForceRepatch(); return; } } if (PatchStatus == 1) { // Change dir's if we can if (Directory.Exists("c:\\net7\\bin")) { Directory.SetCurrentDirectory("c:\\net7\\bin"); } // Execute Launcher if (File.Exists(LauncherExe)) { // Launch launcher System.Diagnostics.Process.Start(Directory.GetCurrentDirectory() + "\\" + LauncherExe, "patcher"); } else { MessageBox.Show("Error: Can't Find file " + LauncherExe, "Can't find file", MessageBoxButtons.OK, MessageBoxIcon.Error); // See if we changed directorys if (Directory.Exists("c:\\net7\\bin")) { Directory.SetCurrentDirectory("c:\\net7"); } ForceRepatch(); return; } } FileProgress.Invoke(new ExitCallBack(ExitApp), null); }
public static void SetTrackProgress(string userId, Guid trackId, double seconds) { using (var db = new HonyomiContext()) { FileProgress prog = db.FileProgresses.SingleOrDefault(x => x.FileId == trackId && x.UserId == userId); if (prog == null) { db.FileProgresses.Add(new FileProgress() { FileId = trackId, UserId = userId, Progress = seconds }); } else { prog.Progress = seconds; } db.SaveChanges(); } }
public static FileWithProgress GetUserFileProgress(string userId, Guid fileId) { using (var db = new HonyomiContext()) { IndexedFile file = db.Files.Include(x => x.Book).ThenInclude(x => x.Files) .SingleOrDefault(x => x.IndexedFileId == fileId); if (file == null) { //bail when such a file does not exist return(null); } FileWithProgress result = new FileWithProgress { Guid = fileId, Title = file.Title, BookGuid = file.BookId, BookTitle = file.Book.Title, TrackIndex = file.TrackIndex, MediaType = file.MimeType, Duration = file.Duration, NextFile = file.Book.Files.FirstOrDefault(x => x.TrackIndex == file.TrackIndex + 1)?.IndexedFileId ?? Guid.Empty }; FileProgress fProg = db.FileProgresses.SingleOrDefault(x => x.FileId == fileId && x.UserId == userId); if (fProg == null) { result.ProgressSeconds = 0; } else { result.ProgressSeconds = fProg.Progress; } return(result); } }
/// <summary> /// Processes the file. /// </summary> /// <param name="fileProgress">The file progress.</param> private void ProcessFile(FileProgress fileProgress) { try { AppendLogLinesDone(0); string sqlStatement = string.Format("SELECT TOP 0 * FROM [dbo].[{0}] ", DataContextObject.CustomTarget.DbSchemaName); // Dummy select to return 0 rows. using (SqlDataAdapter sqlDa = new SqlDataAdapter(sqlStatement, _session.Connection as SqlConnection)) { sqlDa.Fill(DynamicWingTargetDataTable); } var headerItems = DynamicWingTargetDataTable.Columns.OfType <DataColumn>() .Where(dc => !dc.ColumnName.EqualsIgnoreCase("Id") && !dc.ColumnName.EqualsIgnoreCase("Dataset_id")) .Select(dc => dc.ColumnName) .ToArray(); using (var fs = new FileStream(fileProgress.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (var inputFile = new StreamReader(fs)) { // search for the header line string inputLine; var foundHeader = false; while ((inputLine = inputFile.ReadLine()) != null) { fileProgress.LinesDone++; fileProgress.PercentComplete = (fileProgress.LinesDone * 100) / fileProgress.TotalLines; AppendLogLinesDone(fileProgress.LinesDone); if (string.IsNullOrEmpty(_headerLine)) { if (headerItems.Any()) { _headerLine = string.Join(",", headerItems); } } else { //_headerLine = Regex.Replace(_headerLine, "Id, ", string.Empty); //_headerLine = Regex.Replace(_headerLine, ", Dataset_id", string.Empty); _headerLine = _headerLine.Replace(" ", null); //_headerLine.Replace("Id, ", null) // .Replace(", Dataset_id", null) // .Replace(" ", null); } if ( inputLine.Replace(" ", null) .ToUpperInvariant() .StartsWith(_headerLine.ToUpperInvariant())) { //var headerColumns = new List<DataColumn>(); //foreach (var line in _headerLine.Split(',').ToList()) //{ // //DataContextObject.CustomTarget.Elements = DataContextObject.CustomTarget.Elements.ToList(); // var element = DataContextObject.CustomTarget // .Elements.OrderBy(x => x.Ordinal) // .FirstOrDefault(e => e.Name.ToUpper() == line.ToUpper()); // if(element == null) continue; // var column = new DataColumn(element.Name, typeof (int)) {AllowDBNull = element.}; //} //DynamicWingTargetDataTable.Columns.Clear(); //DynamicWingTargetDataTable.Columns.AddRange(DynamicWingDataColumns.ToArray()); // Found the file token in the header. Ready to start import. foundHeader = true; break; } if (fileProgress.LinesDone > 50) { break; } } if (!foundHeader) { // Didn't find file token in input file before EOF MessageBox.Show( AppendLog( string.Format( "The input file [{0}] does not appear to be of the correct file type.", fileProgress.FileName)), "Error importing file"); return; } AppendLog(string.Format("Header row found. Continuing from line {0}.", fileProgress.LinesDone)); // save owner before inserting all rows //if (DataContextObject.CurrentImportType == null) //{ // DataContextObject.CurrentImportType = new ContentTypeRecord // { // Name = DataContextObject.SelectedDataType.DataTypeName // }; // SaveImportTypeRecord(DataContextObject.CurrentImportType); //} // import each line var lineCount = 0; while ((inputLine = inputFile.ReadLine()) != null) { try { if (_lineFunction(inputLine)) { fileProgress.LinesProcessed++; lineCount++; } //else // fileProgress.LinesDuplicated++; if (lineCount == BatchSize || _totalFileLines == fileProgress.LinesProcessed) { if (!string.IsNullOrEmpty(base.DataContextObject.CustomTarget.ImportSQLScript)) { InsertViaCustomImportSQLScript(DynamicWingTargetDataTable); } else { BulkInsert(DynamicWingTargetDataTable); } DynamicWingTargetDataTable.Rows.Clear(); lineCount = 0; } } catch (Exception e) { fileProgress.LinesErrors++; AppendLog("Error : " + e.Message); } finally { fileProgress.LinesDone++; AppendLogLinesDone(fileProgress.LinesDone); } } } } AppendLog(fileProgress.LinesDone + " lines processed."); } catch (Exception e) { MessageBox.Show(AppendLog("Error : " + e.Message), string.Format("Error importing file [{0}]", fileProgress.FileName)); } finally { //this. } }
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) { FileProgress state = (FileProgress)e.UserState; MethodInvoker mi = delegate() { TreeNode childNode = state.File != null ? subnodes[state.File][state.Task] : patchnode; IDictionary <Task, TreeNode> grandchildNode = state.File != null ? subnodes[state.File] : new Dictionary <Task, TreeNode>(); if (state.State == TaskState.Starting) { childNode.EnsureVisible(); treeView1.SelectedNode = childNode; childNode.NodeFont = new Font(childNode.NodeFont ?? treeView1.Font, FontStyle.Italic); if (childNode == patchnode) { cancelButton.Enabled = false; } else { childNode.Parent.EnsureVisible(); } } else if (state.State == TaskState.Done) { childNode.Checked = true; treeView1.SelectedNode = childNode; childNode.EnsureVisible(); childNode.NodeFont = new Font(childNode.NodeFont ?? treeView1.Font, (childNode.NodeFont ?? treeView1.Font).Style & ~FontStyle.Italic); if (childNode != patchnode) { childNode.Parent.EnsureVisible(); } } if (state.Task == Task.IsDteNeeded && state.State == TaskState.Done) { if (!state.File.IsDteNeeded()) { TreeNode dteNode = subnodes[state.File][Task.CalculateDte]; dteNode.NodeFont = new Font(dteNode.NodeFont ?? treeView1.Font, FontStyle.Strikeout); dteNode.Checked = true; } } bool done = true; grandchildNode.ForEach(kvp => done = done && kvp.Value.Checked); if (done && grandchildNode.Count != 0) { TreeNode thisNode = treenodes[state.File]; treeView1.SelectedNode = thisNode; thisNode.Checked = true; thisNode.Collapse(); } else if (done && childNode == patchnode) { patchnode.Checked = true; } }; if (this.InvokeRequired) { this.Invoke(mi); } else { mi(); } }
//https://stackoverflow.com/questions/4986293/access-to-the-path-is-denied-when-using-directory-getfiles // public static List <string> GetFiles(string root, string searchPattern, SearchOption option = SearchOption.AllDirectories, FileProgress fileProgress = null) { Stack <string> pending = new Stack <string>(); pending.Push(root); List <string> listFiles = new List <string>(); int totalFoldersCount = 1; //root folder while (pending.Count != 0) { string path = pending.Pop(); if (fileProgress != null) { fileProgress.Value++; if (fileProgress.Cancel) { listFiles.Clear(); break; } fileProgress.SubStatus = string.Format("? {0:###,##0} Folders, ? {1:###,##0} Files", totalFoldersCount, listFiles.Count); } string[] fileList = null; try { fileList = Directory.GetFiles(path, searchPattern, SearchOption.TopDirectoryOnly); } catch (Exception e) { Debug.WriteLine("Get Files Path: {0} - Error: {1}", path, e); } if (fileList != null && fileList.Length != 0) { listFiles.AddRange(fileList); //foreach (string file in fileList) // yield return file; } try { if (option == SearchOption.AllDirectories) { string [] folders = Directory.GetDirectories(path, "*", SearchOption.TopDirectoryOnly); foreach (string subdir in folders) { pending.Push(subdir); } totalFoldersCount += folders.Length; } } catch (Exception e) { Debug.WriteLine("Get Folders Path: {0} - Error: {1}", path, e); } } return(listFiles); }
void Save(SaveHexOptionsVM hex, ref byte[] buffer) { var progress = new HexFileProgress(GetSize(hex.Document.StartOffset, hex.Document.EndOffset)); fileProgress = progress; if (buffer == null) buffer = new byte[64 * 1024]; try { if (File.Exists(hex.FileName)) File.Delete(hex.FileName); using (var stream = File.OpenWrite(hex.FileName)) { ulong offs = hex.Document.StartOffset; ulong end = hex.Document.EndOffset; while (offs <= end) { ThrowIfCanceled(); ulong bytesLeft = GetSize(offs, end); int bytesToWrite = bytesLeft > (ulong)buffer.Length ? buffer.Length : (int)bytesLeft; hex.Document.Read(offs, buffer, 0, bytesToWrite); stream.Write(buffer, 0, bytesToWrite); progress.BytesWritten += (ulong)bytesToWrite; NotifyProgressUpdated(); ulong nextOffs = offs + (ulong)bytesToWrite; if (nextOffs < offs) break; offs = nextOffs; } } } catch { DeleteFile(hex.FileName); throw; } }
void Save(SaveModuleOptionsVM vm) { fileProgress = new ModuleFileProgress(); var opts = vm.CreateWriterOptions(); opts.Listener = this; opts.Logger = this; var filename = vm.FileName; if (opts is NativeModuleWriterOptions) ((ModuleDefMD)vm.Module).NativeWrite(filename, (NativeModuleWriterOptions)opts); else vm.Module.Write(filename, (ModuleWriterOptions)opts); }
public void FilesCopyStarted(FileProgress progress) { Console.WriteLine("Coping started"); }
private void Start_Update() { bool Update = false; int Status = 1; Update = NeedUpdate(); if (!Update) { //MessageBox.Show("Nothing to be patched", "Patch", MessageBoxButtons.OK, MessageBoxIcon.Information); UpdateCompleate(Status); return; } string[] Files = FileList.Split('\n'); string CRC32; FilesToPatch = ""; // Generate a List of Files that need patched foreach (string Lines in Files) { string[] Data = Lines.Split('\t'); // Check CRC if file exists try { uint crc = Crc32.GetFileCRC32(Data[0]); CRC32 = String.Format("{0:X8}", crc); } catch { CRC32 = ""; // if its our file name read from seperate file if (Data[0] == Me) { try { StreamReader readcrc = File.OpenText(Me + ".crc"); CRC32 = readcrc.ReadLine(); readcrc.Close(); } catch { CRC32 = ""; } } } // If the CRC's match don't download the file if (Data[0] != "" && CRC32.CompareTo(Data[1]) != 0) { FilesToPatch += Data[0] + "\t" + Data[1] + "\n"; } } // Download/Patch the files string[] PatchFiles = FilesToPatch.Split('\n'); int LoopCount = 0; foreach (string File in PatchFiles) { // We are done if (File == "") { break; } string[] Data = File.Split('\t'); // Update File count LoopCount++; // Display file downloading FileProgress.Invoke(new UpdateFileNameCallback(UpdateFileName), new object[] { Data[0] } ); // Download File if (!DownloadFile(Data[0], Data[0] == Me ? MeAlt : Data[0])) { // if an error exit out of this return; } // Let the patcher know to restart if (Data[0] == Me) { Status = 2; } // Check CRC32 after download uint crc = Crc32.GetFileCRC32(Data[0] == Me? MeAlt : Data[0]); CRC32 = String.Format("{0:X8}", crc); if (Data[0] != "" && CRC32.CompareTo(Data[1]) != 0) { MessageBox.Show("CRC32 Error in File " + Data[0] + "\nPlease run the patcher Again", "CRC32 Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // Save our CRC in a file if (Data[0] == Me) { FileInfo crcfile = new FileInfo(Me + ".crc"); StreamWriter writecrc = crcfile.CreateText(); writecrc.WriteLine(Data[1]); writecrc.Close(); } // Update total progress bar FileProgress.Invoke(new UpdateTotalProgressCallback(UpdateTotalProgress), new object[] { LoopCount, PatchFiles.Length - 1 } ); } // Download the newest version info DownloadFile("Version.txt", "Version.txt"); MessageBox.Show("All Files are Updated", "Patching Done", MessageBoxButtons.OK, MessageBoxIcon.Information); UpdateCompleate(Status); return; }
// Download a file from the Update Server private bool DownloadFile(string FileName, string CopyName) { // Grab File List System.Net.WebClient Client = new WebClient(); // The stream of data retrieved from the web server Stream strResponse; // The stream of data that we write to the harddrive Stream strLocal; // The request to the web server for file information HttpWebRequest webRequest; // The response from the web server containing information about the file HttpWebResponse webResponse; try { // Create a request to the file we are downloading webRequest = (HttpWebRequest)WebRequest.Create(NET7_PATCH_URL + FileName); // Set default authentication for retrieving the file webRequest.Credentials = CredentialCache.DefaultCredentials; // Retrieve the response from the server webResponse = (HttpWebResponse)webRequest.GetResponse(); // Ask the server for the file size and store it Int64 fileSize = webResponse.ContentLength; // Open the URL for download strResponse = Client.OpenRead(NET7_PATCH_URL + FileName); // Split into directorys string[] Directorys = FileName.Split('/'); // Create Directorys if needed if (Directorys.Length != 1) { string CPath = "./"; int Loops = 0; // We need to make sure all of the directorys are created foreach (string Dir in Directorys) { Loops++; // Create directory if it does not exsist if (!Directory.Exists(CPath + "/" + Dir)) { Directory.CreateDirectory(CPath + "/" + Dir); } // Add to current path CPath = CPath + "/" + Dir; // If we are down to the file name drop out of the loop if (Loops == Directorys.Length - 1) { break; } } } // Create a new file stream where we will be saving the data (local drive) strLocal = new FileStream(CopyName, FileMode.Create, FileAccess.Write, FileShare.None); // It will store the current number of bytes we retrieved from the server int bytesSize = 0; // A buffer for storing and writing the data retrieved from the server byte[] downBuffer = new byte[2048]; // Loop through the buffer until the buffer is empty while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0) { // Write the data from the buffer to the local hard drive strLocal.Write(downBuffer, 0, bytesSize); // Update Progress Bar FileProgress.Invoke(new UpdateProgressCallback(UpdateProgress), new object[] { strLocal.Length, fileSize } ); } strResponse.Close(); strLocal.Close(); webResponse.Close(); } catch { MessageBox.Show("Error Patching File \"" + FileName + "\"\nMake sure application is shut down!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } return(true); }
// ReSharper disable once CyclomaticComplexity internal async Task <FileSystemResult <IFile> > InternalCreateFile(string name, string type, bool overwrite, AmazonObject parent, Stream readstream, CancellationToken token, IProgress <FileProgress> progress, Dictionary <string, object> properties) { if (readstream.Length == 0) { throw new ArgumentException("input stream must have length"); } string url = AmazonUpload.FormatRest(FS.OAuth.EndPoint.ContentUrl, "?suppress=deduplication"); Json.Metadata j = null; if (!overwrite) { if (properties == null) { properties = new Dictionary <string, object>(); } j = new Json.Metadata(); j.version = 0; j.contentProperties = new Json.ContentProperties(); j.contentProperties.size = readstream.Length; j.contentProperties.version = 0; j.contentProperties.extension = Path.GetExtension(name); string n = Extensions.ContentFromExtension(j.contentProperties.extension); j.contentProperties.contentType = !string.IsNullOrEmpty(n) ? n : "application/octet-stream"; if (properties.Any(a => a.Key.Equals("ModifiedDate", StringComparison.InvariantCultureIgnoreCase))) { j.modifiedDate = (DateTime) properties.First( a => a.Key.Equals("ModifiedDate", StringComparison.InvariantCultureIgnoreCase)).Value; } if (properties.Any(a => a.Key.Equals("CreatedDate", StringComparison.InvariantCultureIgnoreCase))) { j.createdDate = (DateTime) properties.First( a => a.Key.Equals("CreatedDate", StringComparison.InvariantCultureIgnoreCase)).Value; } if (properties.Any(a => a.Key.Equals("Application", StringComparison.InvariantCultureIgnoreCase))) { j.createdBy = (string) properties.First( a => a.Key.Equals("Application", StringComparison.InvariantCultureIgnoreCase)).Value; } else { j.createdBy = "CloudFileSystem"; } if (properties.Any(a => a.Key.Equals("MD5", StringComparison.InvariantCultureIgnoreCase))) { j.contentProperties.md5 = (string) properties.First(a => a.Key.Equals("MD5", StringComparison.InvariantCultureIgnoreCase)) .Value; } j.description = j.name = name; j.isShared = false; j.kind = type; j.parents = new List <string>(); j.parents.Add(parent.Id); } HttpRequestMessage msg = new HttpRequestMessage(overwrite ? HttpMethod.Put : HttpMethod.Post, url); string boundary = "--" + Guid.NewGuid().ToString(); msg.Headers.UserAgent.ParseAdd(FS.OAuth.UserAgent); MultipartFormDataContent ct = new MultipartFormDataContent(boundary); if (!overwrite) { string meta = JsonConvert.SerializeObject(j); StringContent sc = new StringContent(meta, Encoding.UTF8); ct.Add(sc, "metadata"); } StreamContent ssc = new StreamContent(readstream); ct.Add(ssc, "file", name); msg.Content = ct; HttpClientHandler handler = new HttpClientHandler(); ProgressMessageHandler progresshandler = new ProgressMessageHandler(handler); progresshandler.HttpSendProgress += (a, b) => { FileProgress u = new FileProgress(); u.Percentage = b.ProgressPercentage; u.TotalSize = b.TotalBytes ?? 0; u.TransferSize = b.BytesTransferred; progress.Report(u); }; handler.AllowAutoRedirect = true; handler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; await FS.CheckExpirations(); HttpClient cl = new HttpClient(progresshandler); HttpResponseMessage response = null; try { response = await cl.SendAsync(msg, token); if (response.StatusCode == HttpStatusCode.Created) { string dd = await response.Content.ReadAsStringAsync(); string parentpath = string.Empty; AmazonDirectory dir = parent as AmazonDirectory; if (dir == null || !dir.IsRoot) { parentpath = FullName; } AmazonFile file = new AmazonFile(parentpath, FS) { Parent = dir }; file.SetData(dd); progress.Report(new FileProgress { Percentage = 100, TotalSize = file.Size, TransferSize = file.Size }); return(new FileSystemResult <IFile>(file)); } return(new FileSystemResult <IFile>("Http Error : " + response.StatusCode)); } catch (Exception e) { return(new FileSystemResult <IFile>("Exception Error : " + e.Message)); } finally { response?.Dispose(); cl?.Dispose(); handler?.Dispose(); progresshandler?.Dispose(); msg?.Dispose(); } }
void Save(SaveHexOptionsVM hex, ref byte[] buffer) { var progress = new HexFileProgress(GetSize(hex.Buffer.Span.Start, hex.Buffer.Span.End)); fileProgress = progress; if (buffer == null) buffer = new byte[64 * 1024]; try { if (File.Exists(hex.FileName)) File.Delete(hex.FileName); using (var stream = File.OpenWrite(hex.FileName)) { var pos = hex.Buffer.Span.Start; var end = hex.Buffer.Span.End; while (pos < end) { ThrowIfCanceled(); ulong bytesLeft = GetSize(pos, end); int bytesToWrite = bytesLeft > (ulong)buffer.Length ? buffer.Length : (int)bytesLeft; hex.Buffer.ReadBytes(pos, buffer, 0, bytesToWrite); stream.Write(buffer, 0, bytesToWrite); progress.BytesWritten += (ulong)bytesToWrite; NotifyProgressUpdated(); pos = pos + (ulong)bytesToWrite; } } } catch { DeleteFile(hex.FileName); throw; } }
/// <summary> /// Process file and update the file progress. /// </summary> /// <param name="fileProgress">The file progress.</param> /// <returns></returns> private bool ProcessFile(FileProgress fileProgress) { try { using (var fs = new FileStream(fileProgress.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var inputFile = new StreamReader(fs)) { // search for the header line string inputLine; var foundHeader = false; var headerValidationErrorMessage = string.Empty; var metadataFound = false; while ((inputLine = inputFile.ReadLine()) != null) { if (inputLine.StartsWith("Report")) { metadataFound = true; } if (DataContextObject.DatasetItem.ContentType.Name.ToUpper() == ImportTypeName.ToUpper()) { if (!inputLine.ToUpper().StartsWith(HeaderLine.SubStrBefore(",").ToUpper()) && metadataFound) { continue; // Found metadata } metadataFound = false; } ////fileProgress.LinesDone++; //fileProgress.PercentComplete = fileProgress.LinesDone * 100 / fileProgress.TotalLines; //AppendLogLinesDone(fileProgress.LinesDone); if (ValidateFileHeader(inputLine.ToUpper().Trim(), fileProgress, out headerValidationErrorMessage)) { // Found the file token in the header. Ready to start import. foundHeader = true; break; } if (fileProgress.LinesDone > 50) { break; } } if (!foundHeader && !string.IsNullOrEmpty(headerValidationErrorMessage)) { // Didn't find file token in input file before EOF MessageBox.Show(AppendLog(headerValidationErrorMessage), "Error importing file"); return(false); } AppendLog(string.Format("Header row found. Continuing from line {0}.", fileProgress.LinesDone)); // TODO: move this to a service // save owner before inserting all rows DataContextObject.CurrentImportType = DataContextObject.GetTargetByName(DataContextObject.SelectedDataType.DataTypeName); // import each line while ((inputLine = inputFile.ReadLine()) != null) { try { if (_lineFunction(inputLine)) { fileProgress.LinesProcessed++; } else { fileProgress.LinesDuplicated++; } } catch (Exception e) { fileProgress.LinesErrors++; AppendLog("Error : " + e.Message); } finally { fileProgress.LinesDone++; // fileProgress.PercentComplete = (fileProgress.LinesDone * 100) / fileProgress.TotalLines; AppendLogLinesDone(fileProgress.LinesDone); } } } AppendLog(fileProgress.LinesDone + " lines processed."); //DataContextObject.DatasetItem.IsFinished = true; //DataContextObject.SaveImportEntry(DataContextObject.DatasetItem); } catch (Exception e) { MessageBox.Show(AppendLog("Error : " + e.Message), string.Format("Error importing file [{0}]", fileProgress.FileName)); return(false); } return(true); }
public void FilesCreationStarted(FileProgress progress) { Console.WriteLine("Creation started"); }
/// <summary> /// Scan the list of paths and get all found protections /// </summary> /// <returns>Dictionary of list of strings representing the found protections</returns> public Dictionary <string, List <string> > GetProtections(List <string> paths) { // If we have no paths, we can't scan if (paths == null || !paths.Any()) { return(null); } // Checkpoint FileProgress?.Report(new ProtectionProgress(null, 0, null)); // Temp variables for reporting string tempFilePath = Path.GetTempPath(); string tempFilePathWithGuid = Path.Combine(tempFilePath, Guid.NewGuid().ToString()); // Loop through each path and get the returned values var protections = new Dictionary <string, List <string> >(); foreach (string path in paths) { // Directories scan each internal file individually if (Directory.Exists(path)) { // Enumerate all files at first for easier access var files = Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories).ToList(); // Scan for path-detectable protections var directoryPathProtections = GetDirectoryPathProtections(path, files); Utilities.AppendToDictionary(protections, directoryPathProtections); // Scan each file in directory separately for (int i = 0; i < files.Count; i++) { // Get the current file string file = files.ElementAt(i); // Get the reportable file name string reportableFileName = file; if (reportableFileName.StartsWith(tempFilePath)) { reportableFileName = reportableFileName.Substring(tempFilePathWithGuid.Length); } // Checkpoint FileProgress?.Report(new ProtectionProgress(reportableFileName, i / (float)files.Count, "Checking file" + (file != reportableFileName ? " from archive" : string.Empty))); // Scan for path-detectable protections var filePathProtections = GetFilePathProtections(file); Utilities.AppendToDictionary(protections, filePathProtections); // Scan for content-detectable protections var fileProtections = GetInternalProtections(file); if (fileProtections != null && fileProtections.Any()) { foreach (string key in fileProtections.Keys) { if (!protections.ContainsKey(key)) { protections[key] = new List <string>(); } protections[key].AddRange(fileProtections[key]); } } // Checkpoint protections.TryGetValue(file, out List <string> fullProtectionList); string fullProtection = (fullProtectionList != null && fullProtectionList.Any() ? string.Join(", ", fullProtectionList) : null); FileProgress?.Report(new ProtectionProgress(reportableFileName, (i + 1) / (float)files.Count, fullProtection ?? string.Empty)); } } // Scan a single file by itself else if (File.Exists(path)) { // Get the reportable file name string reportableFileName = path; if (reportableFileName.StartsWith(tempFilePath)) { reportableFileName = reportableFileName.Substring(tempFilePathWithGuid.Length); } // Checkpoint FileProgress?.Report(new ProtectionProgress(reportableFileName, 0, "Checking file" + (path != reportableFileName ? " from archive" : string.Empty))); // Scan for path-detectable protections var filePathProtections = GetFilePathProtections(path); Utilities.AppendToDictionary(protections, filePathProtections); // Scan for content-detectable protections var fileProtections = GetInternalProtections(path); if (fileProtections != null && fileProtections.Any()) { foreach (string key in fileProtections.Keys) { if (!protections.ContainsKey(key)) { protections[key] = new List <string>(); } protections[key].AddRange(fileProtections[key]); } } // Checkpoint protections.TryGetValue(path, out List <string> fullProtectionList); string fullProtection = (fullProtectionList != null && fullProtectionList.Any() ? string.Join(", ", fullProtectionList) : null); FileProgress?.Report(new ProtectionProgress(reportableFileName, 1, fullProtection ?? string.Empty)); } // Throw on an invalid path else { Console.WriteLine($"{path} is not a directory or file, skipping..."); //throw new FileNotFoundException($"{path} is not a directory or file, skipping..."); } } // Clear out any empty keys Utilities.ClearEmptyKeys(protections); return(protections); }
public void SaveAll() { mustCancel = false; byte[] buffer = null; for (int i = 0; i < filesToSave.Length; i++) { fileIndex = i; var state = filesToSave[fileIndex]; if (OnWritingFile != null) OnWritingFile(this, new ModuleSaverWriteEventArgs(state.File, true)); fileProgress = null; switch (state.File.Type) { case SaveOptionsType.Module: Save((SaveModuleOptionsVM)state.File); break; case SaveOptionsType.Hex: Save((SaveHexOptionsVM)state.File, ref buffer); break; default: throw new InvalidOperationException(); } fileProgress = null; if (OnWritingFile != null) OnWritingFile(this, new ModuleSaverWriteEventArgs(state.File, false)); } fileIndex = filesToSave.Length; if (OnProgressUpdated != null) OnProgressUpdated(this, EventArgs.Empty); }
public void FilesCreationStarted(FileProgress progress) { ProgressValue = 0; ProgressInfo = $"Creating {progress.TotalCount} files with size: {Misc.BytesToString(progress.TotalSize)}"; }
public static async Task <FileResponse> DownloadFileAsync(Uri uri, string path, Action <HttpRequestMessage>?configureRequest, IProgress <FileProgress>?progress, CancellationToken token) { if (String.IsNullOrWhiteSpace(path)) { throw new ArgumentException("path cannot be NullOrWhiteSpace", nameof(path)); } if (File.Exists(path)) { return(new FileResponse(uri, path, Reason.FileExists)); } string inProgressPath = GetExtension(path, "inprogress"); FileResponse?fileResponse = null; HttpRequestMessage request = new HttpRequestMessage() { RequestUri = uri }; configureRequest?.Invoke(request); HttpResponseMessage?response = null; int receiveBufferSize = 1024 * 1024; // 1024 * 1024 = 1 MiB int saveBufferSize = 4096 * 16; Stream?receive = null; Stream?save = new FileStream(inProgressPath, FileMode.CreateNew, FileAccess.Write, FileShare.None, saveBufferSize, FileOptions.Asynchronous); int bytesRead = 0; byte[] buffer = new byte[receiveBufferSize]; Int64 totalBytesWritten = 0L; Int64 prevTotalBytesWritten = 0L; Int64 progressReportThreshold = 1024L * 100L; // 1024 * 100 = 100 KiB try { response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, token).ConfigureAwait(false); response.EnsureSuccessStatusCode(); Int64?contentLength = response.Content.Headers.ContentLength; receive = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); while ((bytesRead = await receive.ReadAsync(buffer, 0, buffer.Length, token).ConfigureAwait(false)) > 0) { await save.WriteAsync(buffer, 0, bytesRead, token).ConfigureAwait(false); totalBytesWritten += bytesRead; if (progress != null) { if ((totalBytesWritten - prevTotalBytesWritten) > progressReportThreshold) { FileProgress fileProgress = new FileProgress(bytesRead, totalBytesWritten, contentLength); progress.Report(fileProgress); prevTotalBytesWritten = totalBytesWritten; } } } await save.FlushAsync().ConfigureAwait(false); fileResponse = new FileResponse(uri, path, Reason.Success) { Status = response.StatusCode }; } catch (HttpRequestException ex) { fileResponse = new FileResponse(uri, path, Reason.WebError) { Status = response?.StatusCode ?? null, Exception = ex }; } catch (IOException ex) { // IOException is thrown by FileStream with FileMode.CreateNew if the file already exists // it might be thrown by other things as well fileResponse = new FileResponse(uri, path, Reason.FileExists) { Exception = ex }; } catch (TaskCanceledException ex) { fileResponse = new FileResponse(uri, path, Reason.Canceled) { Exception = ex }; } catch (OperationCanceledException ex) { fileResponse = new FileResponse(uri, path, Reason.WebError) { Exception = ex }; } finally { request?.Dispose(); response?.Dispose(); receive?.Dispose(); save?.Dispose(); } // probably unnecessary to wait beyond the finally, but eh, why not? await Task.Delay(TimeSpan.FromMilliseconds(150d), token).ConfigureAwait(false); string finalPath = fileResponse.Reason switch { Reason.Success => path, Reason.WebError => GetExtension(path, "error"), Reason.Canceled => GetExtension(path, "canceled"), _ => GetExtension(path, "failed") }; File.Move(inProgressPath, finalPath); return(fileResponse); }
private void lvJobFiles_DrawSubItem(object sender, DrawListViewSubItemEventArgs e) { Rectangle rectangle; Brush gradientActiveCaption; BitsFile tag = (BitsFile)e.Item.Tag; BitsJob bitsJob = base.wrapper.BitsJob; Color windowText = SystemColors.WindowText; Font prototype = this.lvJobFiles.Font; TextFormatFlags flags = TextFormatFlags.LeftAndRightPadding | TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter; if (tag == null) { if ((bitsJob.State == JobState.Suspended) || (bitsJob.State == JobState.Queued)) { if (e.Item.Selected) { windowText = SystemColors.ActiveCaptionText; } else { windowText = SystemColors.InactiveCaptionText; } prototype = new Font(prototype, FontStyle.Italic); } else if ((bitsJob.State == JobState.Error) || (bitsJob.State == JobState.TransientError)) { windowText = Color.Red; } else if ((bitsJob.State == JobState.Transferred) || (bitsJob.State == JobState.Acknowledged)) { windowText = Settings.Default.ProgressCompletedColor; prototype = new Font(prototype, FontStyle.Strikeout); } else if (bitsJob.State == JobState.Canceled) { if (e.Item.Selected) { windowText = SystemColors.ActiveCaptionText; } else { windowText = SystemColors.InactiveCaptionText; } prototype = new Font(prototype, FontStyle.Strikeout); } else if (bitsJob.State == JobState.Connecting) { windowText = Color.YellowGreen; } else if (bitsJob.State == JobState.Transferring) { windowText = Settings.Default.ProgressDoneColor; } } if (e.Item.Selected) { int num = (e.ColumnIndex == 0) ? 1 : 0; rectangle = new Rectangle(e.Bounds.X + num, e.Bounds.Y + 1, e.Bounds.Width - num, e.Bounds.Height - 2); if (this.lvJobFiles.Focused) { gradientActiveCaption = SystemBrushes.GradientActiveCaption; } else { gradientActiveCaption = SystemBrushes.GradientInactiveCaption; } e.Graphics.FillRectangle(gradientActiveCaption, rectangle); } if (e.SubItem.Name.Equals("BytesTotal")) { TextRenderer.DrawText(e.Graphics, e.SubItem.Text, prototype, e.Bounds, windowText, flags | TextFormatFlags.Right); } else if (e.SubItem.Name.Equals("BytesTransfered")) { TextRenderer.DrawText(e.Graphics, e.SubItem.Text, prototype, e.Bounds, windowText, flags | TextFormatFlags.Right); } else if (e.SubItem.Name == "Progress") { Rectangle rectangle2; StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; gradientActiveCaption = new SolidBrush(Settings.Default.ProgressLeftColor); rectangle = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height); e.Graphics.FillRectangle(gradientActiveCaption, rectangle); JobProgress progress = null; if (tag != null) { FileProgress progress2 = null; progress2 = tag.Progress; rectangle2 = CalcProgress(e.Bounds, progress2.BytesTotal, progress2.BytesTransferred); if (progress2.BytesTransferred > 0L) { if (progress2.Completed || (progress2.BytesTotal == progress2.BytesTransferred)) { gradientActiveCaption = new SolidBrush(Settings.Default.ProgressCompletedColor); } else { gradientActiveCaption = new LinearGradientBrush(rectangle, Settings.Default.ProgressDoneColor, Settings.Default.ProgressCompletedColor, LinearGradientMode.Horizontal); } } rectangle2 = Utils.CalculateProgress(rectangle, progress2.BytesTotal, progress2.BytesTransferred); } else { progress = bitsJob.Progress; rectangle2 = CalcProgress(e.Bounds, progress.BytesTotal, progress.BytesTransferred); if (progress.BytesTransferred > 0L) { if ((bitsJob.State == JobState.Transferred) || (bitsJob.State == JobState.Acknowledged)) { gradientActiveCaption = new SolidBrush(Settings.Default.ProgressCompletedColor); } else { gradientActiveCaption = new LinearGradientBrush(rectangle, Settings.Default.ProgressDoneColor, Settings.Default.ProgressCompletedColor, LinearGradientMode.Horizontal); } } rectangle2 = Utils.CalculateProgress(rectangle, progress.BytesTotal, progress.BytesTransferred); } e.Graphics.FillRectangle(gradientActiveCaption, rectangle2); ControlPaint.DrawBorder(e.Graphics, rectangle, SystemColors.ActiveBorder, ButtonBorderStyle.Solid); windowText = SystemColors.InactiveCaptionText; TextRenderer.DrawText(e.Graphics, e.SubItem.Text, prototype, rectangle, windowText, TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter); } else { TextRenderer.DrawText(e.Graphics, e.SubItem.Text, prototype, e.Bounds, windowText, flags); } }
/// <summary> /// Download file from server /// </summary> /// <param name="apiSession">api session</param> /// <param name="remoteFilePath"> Path to the remote file. Like /some/folder/file.txt </param> /// <param name="handleFile">delegate to check file info before accessing to the file stream</param> /// <param name="streamToWriteTo">stream for writing. Writing will be executed only if handleFile delegate returns true</param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task DownloadFileAsync(ApiSession apiSession, string remoteFilePath, Func <DownloadFileInfo, bool> handleFile, Stream streamToWriteTo, CancellationToken cancellationToken) { if (apiSession == null) { throw new ArgumentNullException(nameof(apiSession)); } var spaceName = apiSession.SpaceName; var nvc = new NameValueCollection(); nvc.Add("_", DateTime.Now.Ticks.ToString()); var url = UrlHelper.JoinUrl("space", spaceName, "files", remoteFilePath) + nvc.ToQueryString(); // it's necessary to add HttpCompletionOption.ResponseHeadersRead to disable caching using (HttpResponseMessage response = await GetHttpClient() .SendAsync(BuildHttpRequestMessage(HttpMethod.Get, url, null, apiSession), HttpCompletionOption.ResponseHeadersRead, cancellationToken)) if (response.IsSuccessStatusCode) { using (Stream streamToReadFrom = await response.Content.ReadAsStreamAsync()) { var contentDisposition = response.Content.Headers.ContentDisposition; DownloadFileInfo dfi = null; if (contentDisposition != null) { dfi = new DownloadFileInfo { // need to fix double quotes, that may come from server response // FileNameStar contains file name encoded in UTF8 FileName = (contentDisposition.FileNameStar ?? contentDisposition.FileName).TrimStart('\"').TrimEnd('\"') }; } var contentLength = response.Content.Headers.ContentLength; var fileProgress = new FileProgress(dfi.FileName, contentLength.Value); fileProgress.StateChanged += DownloadProgress_StateChanged; var bufferSize = 4096; if (handleFile(dfi)) { var buffer = new byte[bufferSize]; var size = contentLength.Value; var processed = 0; var lastUpdate = DateTime.MinValue; fileProgress.ChangeState(FileProgressState.Starting); while (true) { // cancel download if cancellation token triggered if (cancellationToken.IsCancellationRequested) { fileProgress.ChangeState(FileProgressState.Cancelled); throw new OperationCanceledException(); } var length = await streamToReadFrom.ReadAsync(buffer, 0, buffer.Length); if (length <= 0) { break; } await streamToWriteTo.WriteAsync(buffer, 0, length); processed += length; if (DateTime.Now - lastUpdate > TimeSpan.FromMilliseconds(250)) { fileProgress.SetProcessedBytes(processed); fileProgress.ChangeState(FileProgressState.Processing); lastUpdate = DateTime.Now; } } fileProgress.ChangeState(FileProgressState.Finishing); } } } else { // TODO: check await HandleErrorResponse(response); } }
/// <summary> /// Processes the file. /// </summary> /// <param name="fileProgress">The file progress.</param> private void ProcessFile(FileProgress fileProgress) { try { using (var fs = new FileStream(fileProgress.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (var inputFile = new StreamReader(fs)) { // search for the header line string inputLine; var foundHeader = false; while ((inputLine = inputFile.ReadLine()) != null) { fileProgress.LinesDone++; fileProgress.PercentComplete = (fileProgress.LinesDone * 100) / fileProgress.TotalLines; AppendLogLinesDone(fileProgress.LinesDone); if (inputLine.Replace(" ", null).ToUpperInvariant().StartsWith(_headerLine.Replace(" ", null).ToUpperInvariant())) { var headerColumns = new[] { //new DataColumn(DB_COLUMN_ID, typeof(int)){ AllowDBNull = false}, new DataColumn(DB_COLUMN_DRG_ID, typeof(int)) { AllowDBNull = false }, new DataColumn(DB_COLUMN_DRG, typeof(string)) { AllowDBNull = false }, new DataColumn(DB_COLUMN_PROVIDER_ID, typeof(string)) { AllowDBNull = false }, //new DataColumn(DB_COLUMN_PROVIDER_STATE_ABBREV, typeof(string)) { AllowDBNull = true }, new DataColumn(DB_COLUMN_TOTAL_DISCHARGES, typeof(int)) { AllowDBNull = true }, new DataColumn(DB_COLUMN_AVERAGE_COVERED_CHARGES, typeof(double)) { AllowDBNull = true }, new DataColumn(DB_COLUMN_AVERAGE_TOTAL_PAYMENTS, typeof(double)) { AllowDBNull = true }, new DataColumn(DB_COLUMN_DATASET_ID, typeof(int)) { AllowDBNull = true } }; NHCAHPSSurveyDataTable.Columns.Clear(); NHCAHPSSurveyDataTable.Columns.AddRange(headerColumns); // Found the file token in the header. Ready to start import. foundHeader = true; break; } if (fileProgress.LinesDone > 50) { break; } } if (!foundHeader) { // Didn't find file token in input file before EOF MessageBox.Show(AppendLog(string.Format("The input file [{0}] does not appear to be of the correct file type.", fileProgress.FileName)), "Error importing file"); return; } AppendLog(string.Format("Header row found. Continuing from line {0}.", fileProgress.LinesDone)); // Get Version info (using file CreationDate) DateTime?dbCreationDate = File.GetCreationTime(fileProgress.FileName); DataContextObject.DatasetItem.VersionMonth = dbCreationDate.Value.ToString("MMMM"); DataContextObject.DatasetItem.VersionYear = dbCreationDate.Value.ToString("yyyy"); // save owner before inserting all rows DataContextObject.CurrentImportType = DataContextObject.GetTargetByName(DataContextObject.SelectedDataType.DataTypeName); // import each line var lineCount = 0; while ((inputLine = inputFile.ReadLine()) != null) { try { if (_lineFunction(inputLine)) { fileProgress.LinesProcessed++; lineCount++; } //else // fileProgress.LinesDuplicated++; if (lineCount == BATCH_SIZE || _totalFileLines == fileProgress.LinesProcessed) { BulkInsert(NHCAHPSSurveyDataTable); NHCAHPSSurveyDataTable.Rows.Clear(); lineCount = 0; } } catch (Exception e) { fileProgress.LinesErrors++; AppendLog("Error : " + e.Message); } finally { fileProgress.LinesDone++; AppendLogLinesDone(fileProgress.LinesDone); } } } } AppendLog(fileProgress.LinesDone + " lines processed."); } catch (Exception e) { MessageBox.Show(AppendLog("Error : " + e.Message), string.Format("Error importing file [{0}]", fileProgress.FileName)); } }
public void FileCreated(FileProgress progress) { ProgressInfo = $"File: {progress.CurrentCount}/{progress.TotalCount}, size: {Misc.BytesToString(progress.CurrentSize)}/{Misc.BytesToString(progress.TotalSize)}"; ProgressValue = Math.Round(progress.CurrentCount * 100d / progress.TotalCount); }