Exemple #1
0
        private void OnDownloadProgress(object sender, DownloadOperation op)
        {
            Debug.WriteLine($"{op.RequestedUri}:{op.Progress.TotalBytesToReceive}");
            var info = NiconicoMediaManager.CacheRequestInfoFromFileName(op.ResultFile);

            DownloadProgress?.Invoke(this, info, op);
        }
Exemple #2
0
        private static async Task DownloadFileWithProgress(string url, string saveTo, CancellationToken ct = default)
        {
            var download_response = await httpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);

            download_response.EnsureSuccessStatusCode();
            var  filesize          = download_response.Content.Headers.ContentLength.GetValueOrDefault(-1);
            var  reprfilesize      = filesize > 0 ? FormatSize(filesize) : "unknown size";
            long bytes_transferred = 0;

            DownloadProgress?.Invoke(bytes_transferred, filesize);
            using var download_stream = await download_response.Content.ReadAsStreamAsync();

            using var fs = File.OpenWrite(saveTo);
            var buffer = ArrayPool <byte> .Shared.Rent(81920);

            while (true)
            {
                var len = await download_stream.ReadAsync(buffer, 0, buffer.Length, ct);

                if (len == 0)
                {
                    break;
                }
                bytes_transferred += len;
                await fs.WriteAsync(buffer, 0, len, ct);

                DownloadProgress?.Invoke(bytes_transferred, filesize);
            }
            DownloadProgress?.Invoke(bytes_transferred, filesize);
            DownloadProgress?.Invoke(-1, -1);
            ArrayPool <byte> .Shared.Return(buffer);
        }
Exemple #3
0
        private void beginResponse(CancellationToken cancellationToken)
        {
            using (var responseStream = response.Content.ReadAsStreamAsync().Result)
            {
                reportForwardProgress();
                Started?.Invoke();

                buffer = new byte[buffer_size];

                while (true)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    int read = responseStream.Read(buffer, 0, buffer_size);

                    reportForwardProgress();

                    if (read > 0)
                    {
                        ResponseStream.Write(buffer, 0, read);
                        responseBytesRead += read;
                        DownloadProgress?.Invoke(responseBytesRead, response.Content.Headers.ContentLength ?? responseBytesRead);
                    }
                    else
                    {
                        ResponseStream.Seek(0, SeekOrigin.Begin);
                        Complete();
                        break;
                    }
                }
            }
        }
 private void DownloadOutputDataReceived(DataReceivedEventArgs eventArgs, VideoConfig video)
 {
     _downloadLog += eventArgs.Data + "\r\n";
     Log.Debug(eventArgs.Data);
     ParseDownloadProgress(video, eventArgs);
     DownloadProgress?.Invoke(video);
 }
        internal static void OnDownloadProgress(Image image, int downloadPercentage)
        {
#if WPF
            image.RaiseEvent(new DownloadProgressEventArgs(image, downloadPercentage));
#elif WINRT || SILVERLIGHT
            DownloadProgress?.Invoke(image, new DownloadProgressEventArgs(downloadPercentage));
#endif
        }
Exemple #6
0
 public void OnDownloadProgress(DeploymentFile file, int count, int index)
 {
     DownloadProgress?.Invoke(new DownloadProgressEventArgs
     {
         File  = file,
         Count = count,
         Index = index
     });
 }
        private IEnumerator DownloadVideoCoroutine(VideoConfig video, VideoQuality.Mode quality)
        {
            Log.Info($"Starting download of {video.title}");

            _downloadLog = "";

            var downloadProcess = CreateDownloadProcess(video, quality);

            if (downloadProcess == null)
            {
                yield break;
            }

            video.DownloadState = DownloadState.Downloading;
            DownloadProgress?.Invoke(video);

            Log.Info(
                $"youtube-dl command: \"{downloadProcess.StartInfo.FileName}\" {downloadProcess.StartInfo.Arguments}");

            var timeout = new Timeout(5 * 60);

            downloadProcess.OutputDataReceived += (sender, e) =>
                                                  UnityMainThreadTaskScheduler.Factory.StartNew(delegate { DownloadOutputDataReceived(e, video); });

            downloadProcess.ErrorDataReceived += (sender, e) =>
                                                 UnityMainThreadTaskScheduler.Factory.StartNew(delegate { DownloadErrorDataReceived(e); });

            downloadProcess.Exited += (sender, e) =>
                                      UnityMainThreadTaskScheduler.Factory.StartNew(delegate { DownloadProcessExited((Process)sender, video); });

            downloadProcess.Disposed += (sender, e) =>
                                        UnityMainThreadTaskScheduler.Factory.StartNew(delegate { DownloadProcessDisposed((Process)sender, e); });

            StartProcessThreaded(downloadProcess);
            var startProcessTimeout = new Timeout(10);

            yield return(new WaitUntil(() => IsProcessRunning(downloadProcess) || startProcessTimeout.HasTimedOut));

            startProcessTimeout.Stop();

            yield return(new WaitUntil(() => !IsProcessRunning(downloadProcess) || timeout.HasTimedOut));

            if (timeout.HasTimedOut)
            {
                Log.Warn("Timeout reached, disposing download process");
            }
            else
            {
                //When the download is finished, wait for process to exit instead of immediately killing it
                yield return(new WaitForSeconds(2f));
            }

            timeout.Stop();
            _downloadLog = "";
            DisposeProcess(downloadProcess);
        }
Exemple #8
0
 public void DownloadComplete(DownloadData downloadData)
 {
     currentUpdateCount += 1;
     Message?.Invoke($"下载资源中 {currentUpdateCount} / {updateCount}");
     DownloadProgress?.Invoke(currentUpdateCount, updateCount);
     if (currentUpdateCount == updateCount)
     {
         CheckAssetsComplete();
     }
 }
Exemple #9
0
        private async void OnDownloadProgress(object sender, DownloadOperation op)
        {
            Debug.WriteLine($"{op.RequestedUri}:{op.Progress.TotalBytesToReceive}");
            var req      = VideoCacheManager.CacheRequestInfoFromFileName(op.ResultFile);
            var progress = await GetCacheProgress(req.RawVideoId, req.Quality);

            progress.DownloadOperation = op;
            DownloadProgress?.Invoke(this, progress);

            UpdateProgressToast(req, op);
        }
        internal void OnDownloadProgress(CachedImageEvents.DownloadProgressEventArgs e)
        {
            DownloadProgress?.Invoke(this, e);

            var downloadProgressCommand = DownloadProgressCommand;

            if (downloadProgressCommand != null && downloadProgressCommand.CanExecute(e))
            {
                downloadProgressCommand.Execute(e);
            }
        }
Exemple #11
0
 public Downloader()
 {
     _client = new WebClient
     {
         Encoding = Encoding.UTF8,
         Proxy    = GetProxy()
     };
     _client.DownloadProgressChanged += (_, e) =>
     {
         DownloadProgress?.Invoke(this, e);
     };
 }
Exemple #12
0
        /// <summary>
        /// キャッシュダウンロードのタスクから
        /// ダウンロードの情報を復元します
        /// </summary>
        /// <returns></returns>
        private async Task RestoreBackgroundDownloadTask()
        {
            // TODO: ユーザーのログイン情報を更新してダウンロードを再開する必要がある?
            // ユーザー情報の有効期限が切れていた場合には最初からダウンロードし直す必要があるかもしれません
            var tasks = await BackgroundDownloader.GetCurrentDownloadsForTransferGroupAsync(_NicoCacheVideoBGTransferGroup);

            foreach (var task in tasks)
            {
                NicoVideoCacheProgress info = null;
                try
                {
                    var _info = VideoCacheManager.CacheRequestInfoFromFileName(task.ResultFile);

                    var nicoVideo = new NicoVideo(_info.RawVideoId, _HohoemaApp.ContentProvider, _HohoemaApp.NiconicoContext, _HohoemaApp.CacheManager);

                    var session = await nicoVideo.CreateVideoStreamingSession(_info.Quality, forceDownload : true);

                    if (session?.Quality == _info.Quality)
                    {
                        continue;
                    }

                    info = new NicoVideoCacheProgress(_info, task, session);

                    await RestoreDonloadOperation(info, task);

                    Debug.WriteLine($"実行中のキャッシュBGDLを補足: {info.RawVideoId} {info.Quality}");
                }
                catch
                {
                    Debug.WriteLine(task.ResultFile + "のキャッシュダウンロード操作を復元に失敗しました");
                    continue;
                }


                try
                {
                    task.Resume();
                }
                catch
                {
                    if (task.Progress.Status != BackgroundTransferStatus.Running)
                    {
                        await RemoveDownloadOperation(info);

                        // ダウンロード再開に失敗したらキャッシュリクエストに積み直します
                        // 失敗の通知はここではなくバックグラウンドタスクの 後処理 として渡されるかもしれません
                        DownloadProgress?.Invoke(this, info);
                    }
                }
            }
        }
Exemple #13
0
        private void downloadWithFTP(string filename)
        {
            string host     = parts[1];
            string port     = parts[2];
            string username = parts[3];

            string hostName = "ftp://" + host + ":" + port;
            string url      = string.Format("{0}/{1}", hostName, filename);

            try
            {
                WebRequest sizeRequest = WebRequest.Create(url);
                sizeRequest.Credentials = new NetworkCredential(username, PasswordTBDownload.Text.ToString());
                sizeRequest.Method      = WebRequestMethods.Ftp.GetFileSize;
                int size = (int)sizeRequest.GetResponse().ContentLength;
                progressBar1.Invoke(
                    (MethodInvoker)(() => progressBar1.Maximum = size));
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(url));
                request.Method      = WebRequestMethods.Ftp.DownloadFile;
                request.Credentials = new NetworkCredential(username, PasswordTBDownload.Text.ToString());
                request.KeepAlive   = true;
                request.UsePassive  = true;
                //FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                //Stream responseStream = response.GetResponseStream();

                using (Stream ftpStream = request.GetResponse().GetResponseStream())
                    using (var fileStream = File.Create(getDownloadFolderPath() + @"\" + filename))
                    {
                        byte[] buffer = new byte[10240];
                        int    read;
                        while ((read = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            fileStream.Write(buffer, 0, read);
                            int position = (int)fileStream.Position;
                            int percent  = (int)(((float)read / (float)fileStream.Length) * 100);
                            progressBar1.Invoke(
                                (MethodInvoker)(() => progressBar1.Value = position));
                            DownloadProgress.Invoke(
                                (MethodInvoker)(() => DownloadProgress.Text = percent + "%"));
                        }
                        MessageBox.Show("Download Complete");
                        ftpStream.Close();
                    }
            }
            catch (Exception e)
            { /*
               * String status = ((FtpWebResponse)e.Response).StatusDescription;
               * MessageBox.Show(status, "Αn Εrror Οccurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
               */
                MessageBox.Show(e.Message, "Αn Εrror Οccurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /// <summary>
        /// Raises an event for download progress.
        /// </summary>
        private void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            DownloadProgressEventArgs args = null;

            lock (downloadedBytesLocker)
            {
                args = new DownloadProgressEventArgs(downloadedBytes + e.BytesReceived,
                                                     totalUpdateSize, e.BytesReceived, currentlyDownloadedFile.GetDownloadSize(),
                                                     currentlyDownloadedFile.FilePath);
            }

            DownloadProgress?.Invoke(this, args);
        }
        public void CancelDownload(VideoConfig video)
        {
            Log.Debug("Cancelling download");
            video.DownloadState = DownloadState.Cancelled;
            DownloadProgress?.Invoke(video);

            var success = _downloadProcesses.TryGetValue(video, out var process);

            if (success)
            {
                DisposeProcess(process);
            }
            VideoLoader.DeleteVideo(video);
        }
Exemple #16
0
        private void beginResponse()
        {
            try
            {
                response = request.GetResponse() as HttpWebResponse;
                Debug.Assert(response != null);

                Started?.Invoke(this);

                internalResponseStream = response.GetResponseStream();
                Debug.Assert(internalResponseStream != null);

#if !DEBUG
                checkCertificate();
#endif

                buffer = new byte[buffer_size];

                reportForwardProgress();

                while (true)
                {
                    int read = internalResponseStream.Read(buffer, 0, buffer_size);

                    reportForwardProgress();

                    if (read > 0)
                    {
                        ResponseStream.Write(buffer, 0, read);
                        responseBytesRead += read;
                        DownloadProgress?.Invoke(this, responseBytesRead, response.ContentLength);
                    }
                    else
                    {
                        ResponseStream.Seek(0, SeekOrigin.Begin);
                        Complete();
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Complete(e);
            }
        }
Exemple #17
0
        private void ExecuteDownloadClientPoll(object timerState)
        {
            var seen     = new List <string>();
            var complete = new List <DownloadEventArgs>();
            var progress = new List <DownloadEventArgs>();

            var downloadClients = _provider.GetServices <IDownloadClient>();

            foreach (var downloadClient in downloadClients.Where(d => d.Enabled))
            {
                var changes = downloadClient.Poll();
                foreach (var change in changes)
                {
                    var download = _downloads.FirstOrDefault(d => d.Client.DownloadEqual(change, d));
                    if (download == null)
                    {
                        continue;
                    }
                    switch (change.State)
                    {
                    case DownloadNotificationType.Done:
                        _downloads.Remove(download);
                        complete.Add(new DownloadEventArgs(download, DownloadNotificationType.Done));
                        break;

                    case DownloadNotificationType.Progress:
                        download.Percentage = change.Percentage;
                        download.TotalGb    = change.TotalGb;
                        progress.Add(new DownloadEventArgs(download, DownloadNotificationType.Progress));
                        break;
                    }
                }
            }

            DownloadProgress?.Invoke(this, progress);

            complete.AddRange(_downloads.Where(d => !seen.Contains(d.Id)).Select(d => new DownloadEventArgs(d, DownloadNotificationType.Abort)));
            foreach (var completedDownload in complete)
            {
                _downloads.Remove(completedDownload.Download);
            }
            DownloadFinished?.Invoke(this, complete);
        }
Exemple #18
0
        public void DownloadFile(Stream receiver, string relativePath, int bufferSize = RECOMMENDED_BUFFER_SIZE)
        {
            //var request = NewRequest(WebRequestMethods.Ftp.DownloadFile);
            var request = NewRequest(relativePath, FtpVerb.RETR);

            using (var response = request.GetResponse())
            {
                long received = 0;
                using (var stream = response.GetResponseStream())
                {
                    stream.Scan(bufferSize, (buffer, readLength) =>
                    {
                        receiver.Write(buffer, 0, readLength);
                        received += readLength;
                        DownloadProgress?.Invoke(this, received, response.ContentLength);
                    });
                }
            }
        }
Exemple #19
0
 public string Download(
     Stream receiver,
     string method, string enctype, string url,
     Dictionary <string, object> updata,
     Dictionary <string, object> upfiles,
     int bufferSize)
 {
     using (var response = GetLastResponse(method, enctype, url, updata, upfiles))
     {
         long received = 0;
         using (var stream = response.GetResponseStream())
         {
             stream.Scan(bufferSize, (buffer, readLength) =>
             {
                 receiver.Write(buffer, 0, readLength);
                 received += readLength;
                 DownloadProgress?.Invoke(this, url, received, response.ContentLength);
             });
         }
         return(response.Headers["Content-Disposition"]);
     }
 }
Exemple #20
0
        public string ListDirectoryDetails(string relativePath, int bufferSize = RECOMMENDED_BUFFER_SIZE)
        {
            //var request = NewRequest(WebRequestMethods.Ftp.ListDirectoryDetails);
            var request = NewRequest(relativePath, FtpVerb.LIST);

            var memory = new MemoryStream();

            using (var response = request.GetResponse())
            {
                long received = 0;
                using (var stream = response.GetResponseStream())
                {
                    stream.Scan(bufferSize, (buffer, readLength) =>
                    {
                        memory.Write(buffer, 0, readLength);
                        received += readLength;
                        DownloadProgress?.Invoke(this, received, response.ContentLength);
                    });
                }
            }

            return(memory.ToArray().String(StateContainer.Encoding));
        }
Exemple #21
0
 public void Download(string pageUrl, string name)
 {
     if (isDownloading)
     {
         MsgBox?.Invoke("已有任务正在下载中");
         return;
     }
     new System.Threading.Thread(() =>
     {
         isDownloading         = true;
         var url               = pageUrl;
         var CurrentThreadSize = 0;
         var client            = new WebClient();
         var pageSize          = this.GetPageCount(GetPageString(pageUrl));
         if (!Directory.Exists(name))
         {
             Directory.CreateDirectory(name);
         }
         Regex reg = new Regex(@"<div class=""gdtm"".*?<a href=""(.*?)"".*?</div>");
         //先读取所有的链接之后再进行下载,可以得到进度
         List <string> links = new List <string>();
         for (var i = 0; i < pageSize; i++)
         {
             var masterPageHtml = client.DownloadString(string.Format(url, i));
             if (reg.IsMatch(masterPageHtml))
             {
                 foreach (Match m in reg.Matches(masterPageHtml))
                 {
                     links.Add(m.Groups[1].Value);
                 }
             }
             while (CurrentThreadSize >= ThreadPoolSize)
             {
                 System.Threading.Thread.Sleep(500);
             }
         }
         //开始进行下载
         var ChildReg = new Regex(@"<div id=""i3"">.*?<img id=""img"" src=""(.*?)"".*?</div>");
         int max      = links.Count;
         int value    = 0;
         foreach (var x in links)
         {
             while (CurrentThreadSize > ThreadPoolSize)
             {
                 System.Threading.Thread.Sleep(500);
             }
             CurrentThreadSize++;
             Task.Factory.StartNew(() =>
             {
                 var downloadClient = new WebClient();
                 //解析LINK链接 从新的html中照到Image的地址并且下载图片
                 var ChildPageHtml = downloadClient.DownloadString(x);
                 if (ChildReg.IsMatch(ChildPageHtml))
                 {
                     Match m      = ChildReg.Match(ChildPageHtml);
                     var imgLink  = m.Groups[1].Value;
                     var tryNumer = 0;
                     while (tryNumer < 3)
                     {
                         try
                         {
                             downloadClient.DownloadFile(imgLink, name + "\\" + Path.GetFileName(imgLink));
                             break;
                         }
                         catch
                         {
                             //重试三次
                             tryNumer++;
                             System.Threading.Thread.Sleep(3000);
                         }
                     }
                 }
             }, TaskCreationOptions.LongRunning).ContinueWith((c) =>
             {
                 if (c.Status == TaskStatus.RanToCompletion)
                 {
                     CurrentThreadSize--;
                     DownloadProgress?.Invoke(value++, max);
                 }
             });
         }
         System.Threading.Thread.Sleep(5000);
         while (CurrentThreadSize > 0)
         {
             System.Threading.Thread.Sleep(1000);
         }
         DownloadProgress?.Invoke(max, max);
         MsgBox?.Invoke(name + "下载完成");
         isDownloading = false;
         DownloadProgress?.Invoke(0, max);
     })
     {
         IsBackground = true
     }.Start();
 }
Exemple #22
0
 private void RaiseDownloadProgress(DownloadProgressEventArgs args)
 {
     DownloadProgress?.Invoke(this, args);
 }
Exemple #23
0
 internal static void MakeProgress(ProgressJob job, int progressSinceLast)
 {
     DownloadProgress?.Invoke(job, progressSinceLast);
 }
Exemple #24
0
 private bool OnDownloadProgress(DownloadProgressEventArgs e)
 {
     DownloadProgress?.Invoke(this, e);
     return(e.Cancel);
 }
Exemple #25
0
 public static void OnDownloadProgress(object sender, ProgressEventArgs ev) => DownloadProgress?.Invoke(sender, ev);
Exemple #26
0
 protected virtual void UpdateProgress(DownloadChangedProgress e) => DownloadProgress?.Invoke(this, e);
Exemple #27
0
        private static IEnumerator UpdateModCoroutine(DependencyObject item, DownloadStart downloadStart,
                                                      DownloadProgress progress, DownloadFailed dlFail, DownloadFinish finish,
                                                      InstallFailed installFail, InstallFinish installFinish)
        { // (3.2)
            Logger.updater.Debug($"Release: {BeatSaber.ReleaseType}");

            var mod = new Ref <ApiEndpoint.Mod>(null);

            yield return(GetModInfo(item.Name, item.ResolvedVersion.ToString(), mod));

            try { mod.Verify(); }
            catch (Exception e)
            {
                Logger.updater.Error($"Error occurred while trying to get information for {item}");
                Logger.updater.Error(e);
                yield break;
            }

            var releaseName = BeatSaber.ReleaseType == BeatSaber.Release.Steam
                ? ApiEndpoint.Mod.DownloadsObject.TypeSteam : ApiEndpoint.Mod.DownloadsObject.TypeOculus;
            var platformFile = mod.Value.Downloads.First(f => f.Type == ApiEndpoint.Mod.DownloadsObject.TypeUniversal || f.Type == releaseName);

            string url = ApiEndpoint.BeatModBase + platformFile.Path;

            Logger.updater.Debug($"URL = {url}");

            const int maxTries = 3;
            int       tries    = maxTries;

            while (tries > 0)
            {
                if (tries-- != maxTries)
                {
                    Logger.updater.Debug("Re-trying download...");
                }

                using (var stream = new MemoryStream())
                    using (var request = UnityWebRequest.Get(url))
                        using (var taskTokenSource = new CancellationTokenSource())
                        {
                            var dlh = new StreamDownloadHandler(stream, (int i1, int i2, double d) => progress?.Invoke(item, i1, i2, d));
                            request.downloadHandler = dlh;

                            downloadStart?.Invoke(item);

                            Logger.updater.Debug("Sending request");
                            //Logger.updater.Debug(request?.downloadHandler?.ToString() ?? "DLH==NULL");
                            yield return(request.SendWebRequest());

                            Logger.updater.Debug("Download finished");

                            if (request.isNetworkError)
                            {
                                Logger.updater.Error("Network error while trying to update mod");
                                Logger.updater.Error(request.error);
                                dlFail?.Invoke(item, request.error);
                                taskTokenSource.Cancel();
                                continue;
                            }
                            if (request.isHttpError)
                            {
                                Logger.updater.Error("Server returned an error code while trying to update mod");
                                Logger.updater.Error(request.error);
                                dlFail?.Invoke(item, request.error);
                                taskTokenSource.Cancel();
                                continue;
                            }

                            finish?.Invoke(item);

                            stream.Seek(0, SeekOrigin.Begin); // reset to beginning

                            var downloadTask = Task.Run(() =>
                            { // use slightly more multi threaded approach than co-routines
                                // ReSharper disable once AccessToDisposedClosure
                                ExtractPluginAsync(stream, item, platformFile);
                            }, taskTokenSource.Token);

                            while (!(downloadTask.IsCompleted || downloadTask.IsCanceled || downloadTask.IsFaulted))
                            {
                                yield return(null); // pause co-routine until task is done
                            }
                            if (downloadTask.IsFaulted)
                            {
                                if (downloadTask.Exception != null && downloadTask.Exception.InnerExceptions.Any(e => e is BeatmodsInterceptException))
                                { // any exception is an intercept exception
                                    Logger.updater.Error($"BeatMods did not return expected data for {item.Name}");
                                }

                                Logger.updater.Error($"Error downloading mod {item.Name}");
                                Logger.updater.Error(downloadTask.Exception);

                                installFail?.Invoke(item, downloadTask.Exception);
                                continue;
                            }

                            break;
                        }
            }

            if (tries == 0)
            {
                Logger.updater.Warn($"Plugin download failed {maxTries} times, not re-trying");

                installFinish?.Invoke(item, true);
            }
            else
            {
                Logger.updater.Debug("Download complete");
                installFinish?.Invoke(item, false);
            }
        }
Exemple #28
0
 private void OnDownloadProgress(DownloadProgressEventArgs e)
 {
     try { DownloadProgress?.Invoke(this, e); } catch { }
 }
 public void Fire_DownloadProgress(DownloadProgress progress)
 {
     DownloadProgress?.Invoke(this, progress);
 }
Exemple #30
0
 private void VideoDownloader_DownloadProgressChanged(object sender, ProgressEventArgs e)
 {
     DownloadProgress?.Invoke(this, e);
 }