Exemplo n.º 1
0
        private void TriggerDownload(string url, Guid modID)
        {
            ImageCacheEntry e;

            lock (_entryLock)
            {
                if (!_entries.TryGetValue(url, out e))
                {
                    e             = new ImageCacheEntry();
                    e.Url         = url;
                    _entries[url] = e;
                }
            }

            e.Updated = DateTime.Now;
            string file = e.File;

            if (string.IsNullOrWhiteSpace(file))
            {
                file = GetCacheFileName(url);
            }

            string pathToTempDownload = Path.Combine(_folder, file + ".tmp");

            DownloadItem download = new DownloadItem()
            {
                Links = new List <string> {
                    LocationUtil.FormatHttpUrl(url)
                },
                SaveFilePath = pathToTempDownload,
                ItemName     = "Downloading preview image",
                Category     = DownloadCategory.Image
            };

            download.IProc = new Install.InstallProcedureCallback(ae =>
            {
                if (ae.Error == null)
                {
                    lock (_entryLock)
                    {
                        e.File = file;
                    }

                    try
                    {
                        try
                        {
                            // delete existing cache file and rename .tmp file to match cache file name
                            string f = Path.Combine(_folder, file);

                            if (File.Exists(f))
                            {
                                File.Delete(f);
                            }

                            File.Move(pathToTempDownload, f);
                        }
                        catch (IOException)
                        {
                            // this happens while the file is in-use. the file will be renamed and image cache entry updated to point to new file instead
                            file = GetCacheFileName(url);
                            File.Move(pathToTempDownload, file);
                            e.File = file;
                        }

                        Sys.PingInfoChange(modID);
                    }
                    catch (Exception ex)
                    {
                        Sys.Message(new WMessage("failed to get preview image", WMessageLogLevel.Error, ex));
                    }
                }
                else
                {
                    Sys.Message(new WMessage("ImageCache Download error: " + ae.Error.ToString(), WMessageLogLevel.LogOnly, ae.Error));
                }
            });

            Sys.Downloads.AddToDownloadQueue(download);
        }
Exemplo n.º 2
0
        public void Download(IEnumerable <string> links, string file, string description, Install.InstallProcedure iproc, Action onCancel)
        {
            string       link = links.First();
            LocationType type; string location;

            if (!LocationUtil.Parse(link, out type, out location))
            {
                return;
            }

            if (!this.Visible)
            {
                this.Show();
            }
            this.BringToFront();

            if (links.Count() > 1)
            {
                onCancel = () => {
                    Log.Write(String.Format("Downloading {0} - switching to backup url {1}", file, links.ElementAt(1)));
                    Download(links.Skip(1), file, description, iproc, onCancel);
                };
            }

            var lvi = lvDownloads.Items.Add(description);

            lvi.Tag = new DownloadItem()
            {
                IProc = iproc, LVI = lvi, OnCancel = onCancel
            };
            lvi.SubItems.Add("DummyProgress");
            lvi.SubItems.Add("Calculating...");
            switch (type)
            {
            case LocationType.Url:
                var wc = new System.Net.WebClient();
                (lvi.Tag as DownloadItem).PerformCancel = wc.CancelAsync;
                wc.DownloadProgressChanged += new System.Net.DownloadProgressChangedEventHandler(_wc_DownloadProgressChanged);
                wc.DownloadFileCompleted   += new AsyncCompletedEventHandler(_wc_DownloadFileCompleted);
                wc.DownloadFileAsync(new Uri(location), file, lvi.Tag);
                break;

            case LocationType.GDrive:
                var gd = new GDrive();
                (lvi.Tag as DownloadItem).PerformCancel = gd.CancelAsync;
                gd.DownloadProgressChanged += new System.Net.DownloadProgressChangedEventHandler(_wc_DownloadProgressChanged);
                gd.DownloadFileCompleted   += new AsyncCompletedEventHandler(_wc_DownloadFileCompleted);
                gd.Download(location, file, lvi.Tag);
                break;

            case LocationType.MegaSharedFolder:
                string[]           parts = location.Split(',');
                Iros.Mega.MegaIros mega;
                if (!_megaFolders.TryGetValue(parts[0], out mega) || mega.Dead)
                {
                    _megaFolders[parts[0]] = mega = new Mega.MegaIros(parts[0], String.Empty);
                }
                DownloadItem           item = (DownloadItem)lvi.Tag;
                Mega.MegaIros.Transfer tfr  = null;
                tfr = mega.Download(parts[1], parts[2], file, () => {
                    this.Invoke((Delegate)(Action)(() => {
                        switch (tfr.State)
                        {
                        case Mega.MegaIros.TransferState.Complete:
                            ProcessDownloadComplete(item, new AsyncCompletedEventArgs(null, false, item));
                            break;

                        case Mega.MegaIros.TransferState.Failed:
                            lvDownloads.Items.Remove(lvi);
                            Sys.Message(new WMessage()
                            {
                                Text = "Error downloading " + lvi.Text
                            });
                            if (onCancel != null)
                            {
                                onCancel();
                            }
                            break;

                        case Mega.MegaIros.TransferState.Canceled:
                            lvDownloads.Items.Remove(lvi);
                            Sys.Message(new WMessage()
                            {
                                Text = String.Format("{0} was canceled", lvi.Text)
                            });
                            break;

                        default:
                            DoDownloadProgress(item, (int)(100 * tfr.Complete / tfr.Size), tfr.Complete);
                            break;
                        }
                    }));
                });
                mega.ConfirmStartTransfer();
                (lvi.Tag as DownloadItem).PerformCancel = () => mega.CancelDownload(tfr);
                break;
            }
        }