Пример #1
0
        public DownloadItemViewModel Download(string link, string saveFilePath, string description, Action onCancel, Action onComplete)
        {
            DownloadLocationType type;
            string location;

            if (!AssetCatalog.TryParseDownloadUrl(link, out type, out location))
            {
                return(null);
            }

            Action onError = () =>
            {
            };


            DownloadItemViewModel newDownload = new DownloadItemViewModel()
            {
                ItemName      = description,
                OnCancel      = onCancel,
                OnError       = onError,
                OnComplete    = onComplete,
                DownloadSpeed = "Calculating ...",
                DownloadType  = DownloadType.Asset
            };

            switch (type)
            {
            case DownloadLocationType.Url:
                using (var wc = new System.Net.WebClient())
                {
                    newDownload.PerformCancel = () =>
                    {
                        wc.CancelAsync();
                        newDownload.OnCancel?.Invoke();
                    };
                    wc.DownloadProgressChanged += new System.Net.DownloadProgressChangedEventHandler(_wc_DownloadProgressChanged);
                    wc.DownloadFileCompleted   += new AsyncCompletedEventHandler(_wc_DownloadFileCompleted);
                    wc.DownloadFileAsync(new Uri(location), saveFilePath, newDownload);
                }

                break;

            case DownloadLocationType.GDrive:
                var gd = new GDrive();
                newDownload.PerformCancel = () =>
                {
                    gd.CancelAsync();
                    newDownload.OnCancel?.Invoke();
                };
                gd.DownloadProgressChanged += new System.Net.DownloadProgressChangedEventHandler(_wc_DownloadProgressChanged);
                gd.DownloadFileCompleted   += new AsyncCompletedEventHandler(_wc_DownloadFileCompleted);
                gd.Download(location, saveFilePath, newDownload);
                break;
            }

            newDownload.IsStarted = true;
            return(newDownload);
        }
        private void DownloadFileFromGDrive(DownloadItemViewModel newDownload, string fileId)
        {
            GDrive gd = new GDrive();

            newDownload.PerformCancel = () =>
            {
                gd.CancelAsync();
                newDownload.OnCancel?.Invoke();
            };
            gd.DownloadProgressChanged += new System.Net.DownloadProgressChangedEventHandler(_wc_DownloadProgressChanged);
            gd.DownloadFileCompleted   += new AsyncCompletedEventHandler(_wc_DownloadFileCompleted);
            gd.Download(fileId, newDownload.SaveFilePath, newDownload);
        }
Пример #3
0
        public void Download(DownloadItemViewModel newDownload)
        {
            DownloadLocationType type;
            string location;

            if (!AssetCatalog.TryParseDownloadUrl(newDownload.DownloadUrl, out type, out location))
            {
                return;
            }

            switch (type)
            {
            case DownloadLocationType.Url:
                using (var wc = new System.Net.WebClient())
                {
                    newDownload.PerformCancel = () =>
                    {
                        wc.CancelAsync();
                        newDownload.OnCancel?.Invoke();
                    };
                    wc.DownloadProgressChanged += new System.Net.DownloadProgressChangedEventHandler(_wc_DownloadProgressChanged);
                    wc.DownloadFileCompleted   += new AsyncCompletedEventHandler(_wc_DownloadFileCompleted);
                    wc.DownloadFileAsync(new Uri(location), newDownload.SaveFilePath, newDownload);
                }

                break;

            case DownloadLocationType.GDrive:
                var gd = new GDrive();
                newDownload.PerformCancel = () =>
                {
                    gd.CancelAsync();
                    newDownload.OnCancel?.Invoke();
                };
                gd.DownloadProgressChanged += new System.Net.DownloadProgressChangedEventHandler(_wc_DownloadProgressChanged);
                gd.DownloadFileCompleted   += new AsyncCompletedEventHandler(_wc_DownloadFileCompleted);
                gd.Download(location, newDownload.SaveFilePath, newDownload);
                break;
            }

            newDownload.IsStarted = true;
        }