Ejemplo n.º 1
0
        public static void ConvertIfNecessary(Downloader d)
        {
            VideoFormat fmt = GetConvertOption(d);

            if (fmt == VideoFormat.None)
            {
                return;
            }

            try
            {
                d.StatusMessage = "converting video";

                Convert(d.LocalFile, fmt);
            }
            catch (ThreadAbortException)
            {
                throw;
            }
            catch (Exception ex)
            {
                d.LastError = new Exception("error converting video: " + ex.Message, ex);
            }
            finally
            {
                d.StatusMessage = null;
            }
        }
        public static IProtocolProvider CreateProvider(string uri, Downloader downloader)
        {
            IProtocolProvider provider = InternalGetProvider(uri);

            if (downloader != null)
            {
                provider.Initialize(downloader);
            }

            return provider;
        }
        public static IProtocolProvider CreateProvider(Type providerType, Downloader downloader)
        {
            IProtocolProvider provider = CreateFromType(providerType);

            if (ResolvingProtocolProvider != null)
            {
                ResolvingProtocolProviderEventArgs e = new ResolvingProtocolProviderEventArgs(provider, null);
                ResolvingProtocolProvider(null, e);
                provider = e.ProtocolProvider;
            }

            if (downloader != null)
            {
                provider.Initialize(downloader);
            }

            return provider;
        }
Ejemplo n.º 4
0
        public WebSpiderResource(SpiderContext context, ISpiderResource parent, string location)
        {
            this.context = context;
            this.location = location;
            this.parent = parent;

            UpdateDepth();

            string localFile = GetLocalFile();

            download = DownloadManager.Instance.Add(
                    ResourceLocation.FromURL(this.Location),
                    null,
                    localFile,
                    1,
                    false);

            download.StateChanged += new EventHandler(download_StateChanged);
        }
Ejemplo n.º 5
0
        private void UpdateSegmentsWithoutInsert(Downloader d)
        {
            for (int i = 0; i < d.Segments.Count; i++)
            {
                lvwSegments.Items[i].SubItems[0].Text = d.Segments[i].CurrentTry.ToString();
                lvwSegments.Items[i].SubItems[1].Text = String.Format("{0:0.##}%", d.Segments[i].Progress);
                lvwSegments.Items[i].SubItems[2].Text = ByteFormatter.ToString(d.Segments[i].Transfered);
                lvwSegments.Items[i].SubItems[3].Text = ByteFormatter.ToString(d.Segments[i].TotalToTransfer);
                lvwSegments.Items[i].SubItems[4].Text = ByteFormatter.ToString(d.Segments[i].InitialStartPosition);
                lvwSegments.Items[i].SubItems[5].Text = ByteFormatter.ToString(d.Segments[i].EndPosition);
                lvwSegments.Items[i].SubItems[6].Text = String.Format("{0:0.##}", d.Segments[i].Rate / 1024.0);
                lvwSegments.Items[i].SubItems[7].Text = TimeSpanFormatter.ToString(d.Segments[i].Left);
                if (d.Segments[i].LastError != null)
                {
                    lvwSegments.Items[i].SubItems[8].Text = d.Segments[i].State.ToString() + ", " + d.Segments[i].LastError.Message;
                }
                else
                {
                    lvwSegments.Items[i].SubItems[8].Text = d.Segments[i].State.ToString();
                }
                lvwSegments.Items[i].SubItems[9].Text = d.Segments[i].CurrentURL;

                this.blockedProgressBar1.BlockList[i].BlockSize = d.Segments[i].TotalToTransfer;
                this.blockedProgressBar1.BlockList[i].PercentProgress = (float)d.Segments[i].Progress;
            }

            this.blockedProgressBar1.Refresh();
        }
Ejemplo n.º 6
0
        private void AddDownload(Downloader d)
        {
            d.RestartingSegment += new EventHandler<SegmentEventArgs>(download_RestartingSegment);
            d.SegmentStoped += new EventHandler<SegmentEventArgs>(download_SegmentEnded);
            d.SegmentFailed += new EventHandler<SegmentEventArgs>(download_SegmentFailed);
            d.SegmentStarted += new EventHandler<SegmentEventArgs>(download_SegmentStarted);
            d.InfoReceived += new EventHandler(download_InfoReceived);
            d.SegmentStarting += new EventHandler<SegmentEventArgs>(Downloader_SegmentStarting);

            string ext = Path.GetExtension(d.LocalFile);

            ListViewItem item = new ListViewItem();
            //ListViewItem.ListViewSubItem subItem;

            item.ImageIndex = FileTypeImageList.GetImageIndexByExtention(ext);
            //lvwDownloads.Columns["columnFile"].ImageIndex = FileTypeImageList.GetImageIndexByExtention(ext);
            //lvwDownloads.Columns[1].ImageIndex = FileTypeImageList.GetImageIndexByExtention(ext);

            //item.Text = Path.GetFileName(d.LocalFile);
            //item.Text = d.Id.ToString();
            item.Text = (d.OrderedIndex + 1).ToString();

            // Id
            item.SubItems.Add(d.Id.ToString());
            //File
            item.SubItems.Add(Path.GetFileName(d.LocalFile));
            // size
            item.SubItems.Add(ByteFormatter.ToString(d.FileSize));
            // completed
            item.SubItems.Add(ByteFormatter.ToString(d.Transfered));
            // progress
            item.SubItems.Add(String.Format("{0:0.##}%", d.Progress));
            // left
            item.SubItems.Add(TimeSpanFormatter.ToString(d.Left));
            // rate
            item.SubItems.Add("0");
            // added
            item.SubItems.Add(d.CreatedDateTime.ToShortDateString() + " " + d.CreatedDateTime.ToShortTimeString());
            // state
            item.SubItems.Add(d.State.ToString());
            // resume
            item.SubItems.Add(GetResumeStr(d));
            // url
            item.SubItems.Add(d.ResourceLocation.URL);

            //mapDownloadToItem[d] = item;
            //mapItemToDownload[item] = d;

            lvwDownloads.Items.Add(item);
        }
        protected override void AbortInternal()
        {
            if (downloader != null )
            {
                if (IsRunning())
                {
                    DownloadManager.Instance.RemoveDownload(downloader);
                    downloader.WaitForConclusion();
                    status = ITaskStatus.FAIL;
                }

                // try to delete file if an error occurred in download
                if (IsFinished() && !IsSuccess())
                {
                    try
                    {
                        downloader.WaitForConclusion();
                        if (localFile != null && File.Exists(localFile))
                        {
                            try {
                                File.Delete(localFile);
                            }
                            catch (Exception)
                            {
                                try
                                {
                                    Thread.Sleep(100);
                                    File.Delete(localFile);
                                }
                                catch (Exception)
                                {
                                    ;
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                        ;
                    }
                }
            }
            downloader = null;
            status = ITaskStatus.FAIL;
        }
Ejemplo n.º 8
0
 public void Initialize(Downloader downloader)
 {
 }
        public Downloader Add(ResourceLocation rl, ResourceLocation[] mirrors, string localFile, int segments, bool autoStart)
        {
            Downloader d = new Downloader(rl, mirrors, localFile, segments);
            Add(d, autoStart);

            return d;
        }
Ejemplo n.º 10
0
 protected virtual void OnDownloadAdded(Downloader d, bool willStart)
 {
     if (DownloadAdded != null)
     {
         DownloadAdded(this, new DownloaderEventArgs(d, willStart));
     }
 }
Ejemplo n.º 11
0
 protected virtual void OnDownloadRemoved(Downloader d)
 {
     DownloadRemoved?.Invoke(this, new DownloaderEventArgs(d));
 }
Ejemplo n.º 12
0
 public SegmentEventArgs(Downloader d, Segment segment)
     : base(d)
 {
     this.segment = segment;
 }
Ejemplo n.º 13
0
 public DownloaderEventArgs(Downloader download, bool willStart) : this(download)
 {
     this.willStart = willStart;
 }
Ejemplo n.º 14
0
 public DownloaderEventArgs(Downloader download)
 {
     this.downloader = download;
 }
 public IProtocolProvider GetProtocolProvider(Downloader downloader)
 {
     return(BindProtocolProviderInstance(downloader));
 }
Ejemplo n.º 16
0
        // transfered to MyDownloader.Core\Core\Log.cs
        //public enum LogMode
        //{
        //    Error,
        //    Information
        //}

        public void Log(Downloader downloader, string msg, LogMode m)
        {
            try
            {
                this.BeginInvoke(
                    (MethodInvoker)
                  delegate()
                  {
                      int len = richLog.Text.Length;
                      if (len > 0)
                      {
                          richLog.SelectionStart = len;
                      }

                      if (m == LogMode.Error)
                      {
                          richLog.SelectionColor = Color.Red;
                      }
                      else
                      {
                          richLog.SelectionColor = Color.Blue;
                      }

                      richLog.AppendText(DateTime.Now + " - " + msg + Environment.NewLine);
                  }
              );
            }
            catch { }
        }
Ejemplo n.º 17
0
 public void Initialize(Downloader downloader)
 {
     proxy.Initialize(downloader);
 }
Ejemplo n.º 18
0
        public static void SetConvertOption(Downloader d, VideoFormat format)
        {
            if (format == VideoFormat.None)
            {
                if (d.ExtendedProperties.ContainsKey(VideoConverterFormatProperty))
                {
                    d.ExtendedProperties.Remove(VideoConverterFormatProperty);
                }
            }

            d.ExtendedProperties[VideoConverterFormatProperty] = format;
        }
Ejemplo n.º 19
0
        public void RemoveDownload(Downloader downloader)
        {
            if (downloader.State != DownloaderState.NeedToPrepare ||
                downloader.State != DownloaderState.Ended ||
                downloader.State != DownloaderState.Paused)
            {
                downloader.Pause();
            }

            using (LockDownloadList(true))
            {
                downloads.Remove(downloader);
            }

            OnDownloadRemoved(downloader);
        }
 public void Init(Downloader downloader)
 {
     queryMirrorCount = 0;
     this.downloader = downloader;
 }
Ejemplo n.º 21
0
 protected virtual void OnDownloadRemoved(Downloader d)
 {
     if (DownloadRemoved != null)
     {
         DownloadRemoved(this, new DownloaderEventArgs(d));
     }
 }
Ejemplo n.º 22
0
        public void RemoveDownload(Downloader downloader)
        {
            if (downloader.State != DownloaderState.NeedToPrepare ||
                downloader.State != DownloaderState.Ended ||
                downloader.State != DownloaderState.Paused)
            {
                downloader.Pause();
            }

            using (LockDownloadList(true))
            {
                //downloads.Remove(downloader);
                int id = orderedDownloads[downloader.OrderedIndex];
                if (downloader.Id != id)
                    throw new Exception(string.Format("error cant remove downloader id {0} ordered index {1}, downloader id in ordered list is {2}", downloader.Id, downloader.OrderedIndex, id));
                orderedDownloads.RemoveAt(downloader.OrderedIndex);
                downloads.Remove(downloader.Id);
                // refresh ordered index
                int i = 0;
                foreach (int id2 in orderedDownloads)
                    downloads[id2].OrderedIndex = i++;
            }

            OnDownloadRemoved(downloader);
        }
Ejemplo n.º 23
0
        public Downloader Add(ResourceLocation rl, ResourceLocation[] mirrors, string localFile, List<Segment> segments, RemoteFileInfo remoteInfo, int requestedSegmentCount, bool autoStart, DateTime createdDateTime)
        {
            Downloader d = new Downloader(rl, mirrors, localFile, segments, remoteInfo, requestedSegmentCount, createdDateTime);
            Add(d, autoStart);

            return d;
        }
Ejemplo n.º 24
0
        public void Add(Downloader downloader, bool autoStart)
        {
            downloader.StateChanged += new EventHandler(downloader_StateChanged);

            using (LockDownloadList(true))
            {
                //downloads.Add(downloader);
                downloader.OrderedIndex = orderedDownloads.Count;
                downloads.Add(downloader.Id, downloader);
                orderedDownloads.Add(downloader.Id);
            }

            OnDownloadAdded(downloader, autoStart);

            if (autoStart)
            {
                downloader.Start();
            }
        }
        /// <summary>
        /// class-internal method to perform a download with mirrors. Has blocking wait and sets ITask status to FAIL
        /// in case of failure.
        /// </summary>
        /// <param name="urlPath">full URL of file, optionally leaving out protocol http://</param>
        /// <param name="filename">local name under which to store the file</param>
        /// <param name="toLocalFolder">local folder where to store file</param>
        /// <param name="mirrors">optional set of mirrors for urlPath, may be empty string[] for none</param>
        /// <param name="overwriteExisting">if true, overwrites any existing file 'filename'</param>
        protected void InternalDoDownload(string urlPath, string filename, string toLocalFolder, bool overwriteExisting, string[] mirrors )
        {
            localFile = toLocalFolder + "\\" + filename;

            // check if file already there and overwriting is unwanted.
            if (File.Exists(localFile) && !overwriteExisting)
            {
                status = ITaskStatus.SUCCESS;
                return; // yes we're done! no download needed
            }

            // make sure protocol is specified
            if (!urlPath.Contains("://"))
                urlPath = "http://" + urlPath;

            // construct temp file names
            string tempFile;
            tempFile = Path.GetTempPath() + "IndiegameGarden_" + Path.GetRandomFileName();
            TryDeleteFile(tempFile);

            downloader = DownloadManager.Instance.Add(  ResourceLocation.FromURL(urlPath),
                                                        ResourceLocation.FromURLArray(mirrors),
                                                        tempFile, segmentsUsedInDownload, false);

            if (downloader != null)
            {
                downloader.MaxRetries = MaxRetries;
                downloader.Start();
                if (downloader != null)
                {

                    downloader.WaitForConclusion();
                    if (downloader == null)  // case may happen! (on basedownloader cleanup in other thread)
                    {
                        status = ITaskStatus.FAIL;
                        statusMsg = "Download aborted";
                    }
                    else if (!downloader.State.Equals(DownloaderState.Ended))
                    {
                        if (downloader.LastError != null)
                            statusMsg = downloader.LastError.Message;
                        else
                            statusMsg = "Download aborted or timed out";
                        status = ITaskStatus.FAIL;
                    }
                    else
                        status = ITaskStatus.SUCCESS;

                    // remove the temp file if failed
                    if (File.Exists(tempFile) && !IsSuccess())
                    {
                        TryDeleteFile(tempFile);
                    }
                    // move temp file to localFile on success
                    else if (IsSuccess())
                    {
                        try
                        {
                            TryDeleteFile(localFile);
                            File.Move(tempFile, localFile);
                            status = ITaskStatus.SUCCESS;
                        }
                        catch (Exception ex)
                        {
                            status = ITaskStatus.FAIL;
                            statusMsg = "Couldn't move downloaded file to " + localFile + ": " + ex.ToString();
                        }
                        finally {
                            TryDeleteFile(tempFile);
                        }
                    }
                }
            }
            else
            {
                status = ITaskStatus.FAIL;
                statusMsg = "failed to create downloader by DownloadManager.";
            }
        }
Ejemplo n.º 26
0
        private void UpdateSegmentsWithoutInsert(Downloader d)
        {
            for (int i = 0; i < d.Segments.Count; i++)
            {
            }

        }
        public override void Initialize(Downloader downloader)
        {
            base.Initialize(downloader);

            downloader.Ending += new EventHandler(downloader_Ending);
        }
 public static string GetZipEntryNameProperty(Downloader downloader)
 {
     return downloader.ExtendedProperties[ZipEntryNameProperty] as string;
 }
Ejemplo n.º 29
0
 private static string GetResumeStr(Downloader d)
 {
     return (d.RemoteFileInfo != null && d.RemoteFileInfo.AcceptRanges ? "Yes" : "No");
 }
 public static void SetZipEntryNameProperty(Downloader downloader, string entryName)
 {
     downloader.ExtendedProperties[ZipEntryNameProperty] = entryName;
 }
Ejemplo n.º 31
0
        private void UpdateSegmentsInserting(Downloader d, ListViewItem newSelection)
        {
            lastSelection = newSelection;

            lvwSegments.Items.Clear();

            List<Block> blocks = new List<Block>();

            for (int i = 0; i < d.Segments.Count; i++)
            {
                ListViewItem item = new ListViewItem();

                item.Text = d.Segments[i].CurrentTry.ToString();
                item.SubItems.Add(String.Format("{0:0.##}%", d.Segments[i].Progress));
                item.SubItems.Add(ByteFormatter.ToString(d.Segments[i].Transfered));
                item.SubItems.Add(ByteFormatter.ToString(d.Segments[i].TotalToTransfer));
                item.SubItems.Add(ByteFormatter.ToString(d.Segments[i].InitialStartPosition));
                item.SubItems.Add(ByteFormatter.ToString(d.Segments[i].EndPosition));
                item.SubItems.Add(String.Format("{0:0.##}", d.Segments[i].Rate / 1024.0));
                item.SubItems.Add(TimeSpanFormatter.ToString(d.Segments[i].Left));
                item.SubItems.Add(d.Segments[i].State.ToString());
                item.SubItems.Add(d.Segments[i].CurrentURL);

                lvwSegments.Items.Add(item);

                blocks.Add(new Block(d.Segments[i].TotalToTransfer, (float)d.Segments[i].Progress));
            }

            this.blockedProgressBar1.BlockList = blocks;
        }
 public void Initialize(Downloader downloader)
 {
     this.downloader = downloader;
 }
Ejemplo n.º 33
0
        //private static void LoadPersistedObjects(DownloadItem[] downloads)
        private static void LoadPersistedObjects(DownloadParameters downloadParameters)
        {
            string directory = downloadParameters.defaultDownloadDirectory;
            DownloadManager.Instance.DefaultDownloadDirectorySource = directory;
            if (!Path.IsPathRooted(directory))
                directory = Path.Combine(GetDatabaseDirectory(), directory);
            DownloadManager.Instance.DefaultDownloadDirectory = directory;
            DownloadManager.Instance.LastDownloadId = downloadParameters.lastDownloadId;
            DownloadItem[] downloads = downloadParameters.downloadItems;
            for (int i = 0; i < downloads.Length; i++)
            {
                List<Segment> segments = new List<Segment>();

                for (int j = 0; j < downloads[i].Segments.Length; j++)
                {
                    Segment seg = new Segment();
                    seg.Index = downloads[i].Segments[j].Index;
                    seg.InitialStartPosition = downloads[i].Segments[j].InitialStartPositon;
                    seg.StartPosition = downloads[i].Segments[j].StartPositon;
                    seg.EndPosition = downloads[i].Segments[j].EndPosition;

                    segments.Add(seg);
                }

                //Downloader d = DownloadManager.Instance.Add(
                //    downloads[i].rl,
                //    downloads[i].mirrors,
                //    downloads[i].LocalFile,
                //    segments,
                //    downloads[i].remoteInfo,
                //    downloads[i].requestedSegments,
                //    false,
                //    downloads[i].createdDateTime);

                Downloader d = new Downloader(downloads[i].id, downloads[i].rl, downloads[i].mirrors, downloads[i].LocalFile, segments, downloads[i].remoteInfo,
                    downloads[i].requestedSegments, downloads[i].createdDateTime);
                DownloadManager.Instance.Add(d, false);

                if (downloads[i].extendedProperties != null)
                {
                    SerializableDictionary<string, object>.Enumerator e = downloads[i].extendedProperties.GetEnumerator();

                    while (e.MoveNext())
                    {
                        d.ExtendedProperties.Add(e.Current.Key, e.Current.Value);
                    }
                }
            }
        } 
Ejemplo n.º 34
0
        public void Add(Downloader downloader, bool autoStart)
        {
            downloader.StateChanged += new EventHandler(downloader_StateChanged);

            using (LockDownloadList(true))
            {
                downloads.Add(downloader);
            }

            OnDownloadAdded(downloader, autoStart);

            if (autoStart)
            {
                downloader.Start();
            }
        }
 public virtual void Initialize(Downloader downloader)
 {
 }
Ejemplo n.º 36
0
 protected virtual void OnDownloadAdded(Downloader d, bool willStart)
 {
     DownloadAdded?.Invoke(this, new DownloaderEventArgs(d, willStart));
 }