예제 #1
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);
        }
예제 #2
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);
        }