Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
 protected virtual void Dispose(FileStreamWrapperBase wrapper)
 {
     wrapper.Callback -= OnFileStreamAction;
     if (FileStreams.ContainsKey(wrapper.FilePath))
     {
         FileStreams.Remove(wrapper.FilePath);
     }
     wrapper.Dispose();
 }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        /// <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);
        }