Esempio n. 1
0
        internal async Task DownloadFileAsync(DownloadFile file)
        {
            string?directoryName = Path.GetDirectoryName(file.Path);

            if (!string.IsNullOrEmpty(directoryName))
            {
                Directory.CreateDirectory(directoryName);
            }

            using (var wc = new TimeoutWebClient())
            {
                long lastBytes = 0;

                wc.DownloadProgressChanged += (s, e) =>
                {
                    lock (locker)
                    {
                        var progressedBytes = e.BytesReceived - lastBytes;
                        if (progressedBytes < 0)
                        {
                            return;
                        }

                        lastBytes = e.BytesReceived;

                        var progress = new FileDownloadProgress(
                            file, e.TotalBytesToReceive, progressedBytes, e.BytesReceived, e.ProgressPercentage);
                        FileDownloadProgressChanged?.Invoke(this, progress);
                    }
                };
                await wc.DownloadFileTaskAsync(file.Url, file.Path)
                .ConfigureAwait(false);
            }
        }
Esempio n. 2
0
 public void OnUpdateDownloadProgressChanged(Update update, FileDownloadProgress progress)
 {
     _menuItem.Text =
         string.Format(
             "Downloading Update: {0:P}...",
             progress.PercentComplete / 100.0);
 }
 /// <summary>
 /// This method handles updating the UI with file processing progress.
 /// </summary>
 /// <param name="result"></param>
 private void DownloadProgress(FileDownloadProgress result)
 {
     Logger.Log("INFO", string.Format("Processing file {0} of {1}", result.CurrentFile, result.TotalFiles));
     this.Invoke((MethodInvoker) delegate {
         lblStatusValue.Text = string.Format("Processing file {0} of {1}", result.CurrentFile, result.TotalFiles);
     });
 }
Esempio n. 4
0
 private void DownloaderOnProgressChanged(FileDownloadProgress fileDownloadProgress)
 {
     if (DownloadProgressChanged != null)
     {
         DownloadProgressChanged(fileDownloadProgress);
     }
 }
Esempio n. 5
0
        private void _webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            if (!_updating)
            {
                return;
            }

            FileDownloadProgress?.Invoke(e.ProgressPercentage);
        }
Esempio n. 6
0
        private void Connection_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
        {
            FileDownloadInfo info = e.UserState as FileDownloadInfo;

            if (info == null)
            {
                return;
            }

            info.Args.Progress = e.ProgressPercentage * 0.01;

            FileDownloadProgress?.Invoke(this, info.Args);
        }
Esempio n. 7
0
        private void ProgressChanged(FileDownloadProgress progress)
        {
            var message =
                string.Format(
                    "Downloading version {0}: {1} of {2} @ {3} ({4:P})",
                    _updater.LatestUpdate.Version,
                    FileUtils.HumanFriendlyFileSize(progress.BytesDownloaded),
                    FileUtils.HumanFriendlyFileSize(progress.ContentLength),
                    progress.HumanSpeed,
                    progress.PercentComplete / 100.0);

            Logger.Debug(message);
            Notify(observer => observer.OnUpdateDownloadProgressChanged(_updater.LatestUpdate, progress));
        }
Esempio n. 8
0
        private void Connection_UploadValuesCompleted(object sender, UploadValuesCompletedEventArgs e)
        {
            FileDownloadInfo info = e.UserState as FileDownloadInfo;

            if (info == null)
            {
                return;
            }

            if (e.Error != null || e.Cancelled)
            {
                info.Args.Progress = -1;
                FileDownloadError?.Invoke(this, info.Args);
            }

            info.Args.Progress = 1;
            FileDownloadProgress?.Invoke(this, info.Args);

            lock (info.LocalFile.Directory)
            {
                if (!info.LocalFile.Directory.Exists)
                {
                    info.LocalFile.Directory.Create();
                }
            }

            var           fs = info.LocalFile.OpenWrite();
            var           ms = new MemoryStream(e.Result);
            DeflateStream df = new DeflateStream(ms, CompressionMode.Decompress);

            df.CopyTo(fs);
            df.Close();
            fs.Close();

            FileDownloadCompleted?.Invoke(this, info.Args);

            StartDLJob(info);
        }
Esempio n. 9
0
 /// <summary>
 /// Event für das Fortschritt des Datei-Downloads auslösen
 /// </summary>
 /// <param name="position"></param>
 protected virtual void OnFileDownloadProgress(long position)
 {
     FileDownloadProgress?.Invoke(this, new FileDownloadProgressEventArgs(position));
 }
        public async static Task <bool> DownloadFile(string fileName, int id, string authToken)
        {
            string tempfile = Path.Combine(Constants.API_FILE_DOWNLOAD_FOLDER, fileName);

            var request = new HttpRequestMessage(HttpMethod.Get, Constants.API_FILEHOST_GET_FILE);

            request.Headers.Add("File-Name", fileName);
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authToken);


            HttpResponseMessage response = await ApiClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);


            if (response.StatusCode != HttpStatusCode.OK)
            {
                // Something wrong
                FileDownloadResultEventArgs result_args = new FileDownloadResultEventArgs();
                result_args.Successful = false;
                result_args.FileName   = fileName;
                result_args.Reason     = response.ReasonPhrase;
                FileDownloadFailure?.Invoke(null, result_args);
                return(false);
            }

            var totalBytes = 0L;

            if (response.Headers.Contains("File-Size"))
            {
                totalBytes = Convert.ToInt64(response.Headers.GetValues("File-Size").FirstOrDefault());
            }

            Stream contentStream = await response.Content.ReadAsStreamAsync();

            FileStream fileStream = new FileStream(tempfile, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true);


            var totalRead    = 0L;
            var totalReads   = 0L;
            var buffer       = new byte[8192];
            var isMoreToRead = true;

            do
            {
                var read = await contentStream.ReadAsync(buffer, 0, buffer.Length);

                if (read == 0)
                {
                    isMoreToRead = false;
                }
                else
                {
                    await fileStream.WriteAsync(buffer, 0, read);

                    totalRead  += read;
                    totalReads += 1;

                    if (totalReads % 2000 == 0)
                    {
                        FileTransferProgressEventArgs progress_args = new FileTransferProgressEventArgs();
                        progress_args.FileName = fileName;
                        progress_args.ID       = id;
                        decimal pp = (decimal)totalRead / (decimal)totalBytes;

                        progress_args.PercentProgress = (int)Math.Round(pp * 100);

                        FileDownloadProgress?.Invoke(null, progress_args);


                        //Console.WriteLine(string.Format("total bytes downloaded so far: {0:n0}", totalRead));
                    }
                }
            }while (isMoreToRead);



            FileDownloadResultEventArgs args = new FileDownloadResultEventArgs();

            args.Successful = true;
            args.FileName   = fileName;
            args.ID         = id;

            FileDownloadSuccessful?.Invoke(null, args);

            return(true);
        }