protected virtual FileStreamWriter GetFileStream(EventHandler <FileLoggerCallbackArgs> callback) { var wrapper = !FilePath.IsNullOrEmpty() && FileStreams.ContainsKey(FilePath) ? FileStreams[FilePath] as FileStreamWriter : null; if (wrapper != null) { if (wrapper.FileStream.Length < Settings.MaxFileSize) { wrapper.SaveToFile(Settings.LineDelimiter); return(wrapper); } FileStreams.Remove(wrapper.FilePath); wrapper.Close(); } FilePath = GetNewFilePath(); wrapper = new FileStreamWriter(FilePath, callback) { IsFileCompressionEnabled = Settings.UseGzipCompression, GzipFileExt = Settings.GzipFileExtension }; wrapper.Callback += OnFileStreamAction; FileStreams.Add(FilePath, wrapper); if (!Settings.OpeningLine.IsNullOrEmpty()) { wrapper.SaveToFile(Settings.OpeningLine); } return(wrapper); }
public void LoadFromFile(string filePath, EventHandler <FileLoggerCallbackArgs> callback) { try { if (!Settings.IsEnabled) { const string errMsg = "reading from log file is aborted. Logger is disabled.\r\nTo enable logger set 'Settings.IsEnabled = true'."; #if UNITY_3D || UNITY3D UnityEngine.Debug.LogWarning(errMsg); #else Debug.WriteLine(errMsg); #endif return; } ArgumentValidator.AssertNotNullOrEmpty(filePath, "filePath"); if (!File.Exists(filePath)) { throw new FileNotFoundException(filePath); } if (FileStreams.ContainsKey(filePath)) { throw new OperationCanceledException("File with path '" + filePath + "' is in process."); } var wrapper = new FileStreamReader(filePath, callback) { IsFileCompressionEnabled = Settings.UseGzipCompression }; wrapper.Callback += OnFileStreamAction; FileStreams.Add(filePath, wrapper); wrapper.LoadFromFile(); } catch (Exception e) { FileStreamWrapperBase.InvokeFileActionEvent(callback, null, filePath, null, null, FileLoggerAction.Read, FileLoggerResult.Error, e); if (callback == null) { throw; } } }
public void TestAssignInternetZoneIdentifierToFile() { var testFile = Path.Combine(Path.GetTempPath(), "ZoneTest" + Guid.NewGuid().ToString("N") + ".txt"); using (var s = FileHelper.OpenForWrite(testFile)) { s.WriteByte(61); s.WriteByte(62); } var FS = new FileStreams(testFile); //Remove Zone.Identifier if it already exists since we can't trust it //Not sure if this can happen. int i = FS.IndexOf("Zone.Identifier"); if (i != -1) { FS.Remove("Zone.Identifier"); } FS.Add("Zone.Identifier"); using (FileStream fs = FS["Zone.Identifier"].Open(FileMode.OpenOrCreate, FileAccess.Write)) { var writer = new StreamWriter(fs); writer.WriteLine("[ZoneTransfer]"); writer.WriteLine("ZoneId=3"); writer.Flush(); fs.Flush(); } FS = new FileStreams(testFile); i = FS.IndexOf("Zone.Identifier"); //TODO: why does this fail? //Assert.NotEqual(-1, i); }
/// <summary> /// Method used to handle the event. /// </summary> /// <param name="sender">The sender of the event.</param> /// <param name="e">The task information.</param> private void OnDownloadCompleted(object sender, TaskEventArgs e) { if (e.Task == null) { return; } string fileLocation = Path.Combine(e.Task.DownloadFilesBase, e.Task.DownloadItem.File.LocalName); string finalLocation = Path.Combine(e.Task.DownloadItem.TargetFolder, e.Task.DownloadItem.File.LocalName); _log.InfoFormat("Finished downloading enclosure '{0}' from item '{1}' in feed '{2}' to {3}", e.Task.DownloadItem.Enclosure.Url, e.Task.DownloadItem.OwnerItemId, e.Task.DownloadItem.OwnerFeedId, finalLocation); // We have a UI for managing enclosures we'll need to keep the task around ! //DownloadRegistryManager.Current.UnRegisterTask(e.Task); try { /* * Add Zone.Identifier to File to indicate that the file was downloaded from the Web * See http://geekswithblogs.net/ssimakov/archive/2004/08/17/9805.aspx for more details */ try { var FS = new FileStreams(fileLocation); //Remove Zone.Identifier if it already exists since we can't trust it //Not sure if this can happen. int i = FS.IndexOf("Zone.Identifier"); if (i != -1) { FS.Remove("Zone.Identifier"); } FS.Add("Zone.Identifier"); using (FileStream fs = FS["Zone.Identifier"].Open(FileMode.OpenOrCreate, FileAccess.Write)) { var writer = new StreamWriter(fs); writer.WriteLine("[ZoneTransfer]"); writer.WriteLine("ZoneId=3"); writer.Flush(); } } catch (Exception ex) { _log.ErrorFormat("Failed to assign Internet (Download) Zone to {0}: {1}", fileLocation, ex.Message); } if (!Directory.Exists(e.Task.DownloadItem.TargetFolder)) { Directory.CreateDirectory(e.Task.DownloadItem.TargetFolder); } /* Move file to TargetFolder from temporary download location*/ FileHelper.MoveFile(fileLocation, finalLocation, MoveFileFlag.CopyAllowed | MoveFileFlag.ReplaceExisting); e.Task.FileName = finalLocation; /* Initiate callback to waiting callers */ if (DownloadCompleted != null) { DownloadCompleted(this, new DownloadItemEventArgs(e.Task.DownloadItem)); } var downloader = sender as IDownloader; if (downloader != null) { Release(downloader); } } catch (Exception error) { OnDownloadError(this, new DownloadTaskErrorEventArgs(e.Task, error)); return; } e.Task.State = DownloadTaskState.Downloaded; DownloadRegistryManager.Current.UpdateTask(e.Task); }
public HttpParamers AddFile(string key, IFileItem stream) { FileStreams.Add(key, stream); return(this); }