public void CloneTest()
        {
            // arrange
            var configProperties = typeof(DownloadConfiguration).GetProperties();
            var config           = new DownloadConfiguration()
            {
                MaxTryAgainOnFailover = 100,
                ParallelDownload      = true,
                ChunkCount            = 1,
                Timeout                     = 150,
                OnTheFlyDownload            = true,
                BufferBlockSize             = 2048,
                MaximumBytesPerSecond       = 1024,
                RequestConfiguration        = new RequestConfiguration(),
                TempDirectory               = Path.GetTempPath(),
                CheckDiskSizeBeforeDownload = false
            };

            // act
            var cloneConfig = config.Clone() as DownloadConfiguration;

            // assert
            foreach (PropertyInfo property in configProperties)
            {
                Assert.AreEqual(property.GetValue(config), property.GetValue(cloneConfig));
            }
        }
        public async void start()
        {
            await Task.Delay(5000);

            await Task.Run(async() =>
            {
                var downloadOpt = new DownloadConfiguration()
                {
                    ParallelDownload      = true,         // download parts of file as parallel or not
                    BufferBlockSize       = 10240,        // usually, hosts support max to 8000 bytes
                    ChunkCount            = 8,            // file parts to download
                    MaxTryAgainOnFailover = int.MaxValue, // the maximum number of times to fail.
                    OnTheFlyDownload      = true,         // caching in-memory mode
                    Timeout = 1000                        // timeout (millisecond) per stream block reader
                };
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                                                                              async() =>
                {
                    var downloader = new DownloadService(downloadOpt);
                    downloader.DownloadProgressChanged += OnDownloadProgressChangedAsync;

                    downloader.DownloadFileCompleted += OnDownloadFileCompleted;
                    var file = @"Downloads\Annotation-2019-10-04-194747.png";
                    await downloader.DownloadFileTaskAsync("https://i.ibb.co/YDHN1HY/Annotation-2019-10-04-194747.png", file);
                });
            });
        }
Esempio n. 3
0
        public void CompletesWithErrorWhenBadUrlTest()
        {
            // arrange
            Exception onCompletionException = null;
            string    address = "https://nofile";
            FileInfo  file    = new FileInfo(Path.GetTempFileName());

            Options = new DownloadConfiguration {
                BufferBlockSize       = 1024,
                ChunkCount            = 8,
                ParallelDownload      = true,
                MaxTryAgainOnFailover = 0,
                OnTheFlyDownload      = true
            };
            DownloadFileCompleted += delegate(object sender, AsyncCompletedEventArgs e) {
                onCompletionException = e.Error;
            };

            // act
            DownloadFileTaskAsync(address, file.FullName).Wait();

            // assert
            Assert.IsFalse(IsBusy);
            Assert.IsNotNull(onCompletionException);
            Assert.AreEqual(typeof(WebException), onCompletionException.GetType());

            Clear();
            file.Delete();
        }
        public downloader()
        {
            System.Net.ServicePointManager.DefaultConnectionLimit = 5;
            var downloadOpt = new DownloadConfiguration()
            {
                BufferBlockSize       = 10240,         // usually, hosts support max to 8000 bytes, default values is 8000
                ChunkCount            = 1,             // file parts to download, default value is 1
                MaximumBytesPerSecond = 10240 * 10240, // download speed limited to 1MB/s, default values is zero or unlimited
                MaxTryAgainOnFailover = int.MaxValue,  // the maximum number of times to fail
                OnTheFlyDownload      = false,         // caching in-memory or not? default values is true
                ParallelDownload      = false,         // download parts of file as parallel or not. Default value is false
                TempDirectory         = "C:\\temp",    // Set the temp path for buffering chunk files, the default path is Path.GetTempPath()
                Timeout = 1000,                        // timeout (millisecond) per stream block reader, default values is 1000
                RequestConfiguration =                 // config and customize request headers
                {
                    Accept                 = "*/*",
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
                    CookieContainer        = new CookieContainer(),    // Add your cookies
                    Headers               = new WebHeaderCollection(), // Add your custom headers
                    KeepAlive             = false,
                    ProtocolVersion       = HttpVersion.Version11,     // Default value is HTTP 1.1
                    UseDefaultCredentials = false,
                    UserAgent             = $"DownloaderSample/{Assembly.GetExecutingAssembly().GetName().Version.ToString(3)}"
                }
            };

            _downloader = new DownloadService(downloadOpt);
            // Provide any information about download progress, like progress percentage of sum of chunks, total speed, average speed, total received bytes and received bytes array to live streaming.
            _downloader.DownloadProgressChanged += OnDownloadProgressChanged;
            // Download completed event that can include occurred errors or cancelled or download completed successfully.
            _downloader.DownloadFileCompleted += OnDownloadFileCompleted;
        }
Esempio n. 5
0
        public void DownloadIntoFolderTest()
        {
            string        targetFolderPath = Path.Combine(Path.GetTempPath(), "downloader test folder");
            DirectoryInfo targetFolder     = new DirectoryInfo(targetFolderPath);

            DownloadConfiguration config = new DownloadConfiguration {
                BufferBlockSize       = 1024,
                ChunkCount            = 1,
                ParallelDownload      = false,
                MaxTryAgainOnFailover = 100,
                OnTheFlyDownload      = true
            };
            DownloadService downloader = new DownloadService(config);

            downloader.DownloadFileAsync(DownloadTestHelper.File1KbUrl, targetFolder).Wait();
            Assert.AreEqual(DownloadTestHelper.FileSize1Kb, downloader.Package.TotalFileSize);
            downloader.Clear();
            downloader.DownloadFileAsync(DownloadTestHelper.File150KbUrl, targetFolder).Wait();
            Assert.AreEqual(DownloadTestHelper.FileSize150Kb, downloader.Package.TotalFileSize);
            downloader.Clear();

            Assert.IsTrue(targetFolder.Exists);
            FileInfo[] downloadedFiles = targetFolder.GetFiles();
            long       totalSize       = downloadedFiles.Sum(file => file.Length);

            Assert.AreEqual(DownloadTestHelper.FileSize1Kb + DownloadTestHelper.FileSize150Kb, totalSize);
            Assert.IsTrue(downloadedFiles.Any(file => file.Name == DownloadTestHelper.File1KbName));
            Assert.IsTrue(downloadedFiles.Any(file => file.Name == DownloadTestHelper.File150KbName));

            targetFolder.Delete(true);
        }
Esempio n. 6
0
        public void CancelAsyncTest()
        {
            // arrange
            AsyncCompletedEventArgs eventArgs = null;
            string address = DummyFileHelper.GetFileUrl(DummyFileHelper.FileSize16Kb);

            Options = new DownloadConfiguration {
                BufferBlockSize       = 1024,
                ChunkCount            = 8,
                ParallelDownload      = true,
                MaxTryAgainOnFailover = 100,
                OnTheFlyDownload      = true
            };
            DownloadStarted       += (s, e) => CancelAsync();
            DownloadFileCompleted += (s, e) => eventArgs = e;

            // act
            DownloadFileTaskAsync(address).Wait();

            // assert
            Assert.IsTrue(IsCancelled);
            Assert.IsNotNull(eventArgs);
            Assert.IsTrue(eventArgs.Cancelled);
            Assert.AreEqual(typeof(OperationCanceledException), eventArgs.Error.GetType());

            Clear();
        }
Esempio n. 7
0
        public void DownloadPdf150KTest()
        {
            var expectedFileSize = DownloadTestHelper.FileSize150Kb; // real bytes size
            var address          = DownloadTestHelper.File150KbUrl;
            var file             = new FileInfo(Path.GetTempFileName());
            var config           = new DownloadConfiguration()
            {
                BufferBlockSize       = 1024,
                ChunkCount            = 1,
                ParallelDownload      = false,
                MaxTryAgainOnFailover = 100,
                OnTheFlyDownload      = true
            };
            var progressCount = config.ChunkCount * (int)Math.Ceiling((double)expectedFileSize / config.ChunkCount / config.BufferBlockSize);
            var downloader    = new DownloadService(config);

            downloader.DownloadProgressChanged += delegate { Interlocked.Decrement(ref progressCount); };

            downloader.DownloadFileAsync(address, file.FullName).Wait();
            Assert.IsTrue(file.Exists);
            Assert.AreEqual(expectedFileSize, downloader.Package.TotalFileSize);
            Assert.AreEqual(expectedFileSize, file.Length);
            Assert.IsTrue(progressCount <= 0);

            file.Delete();
        }
 public override void Initial()
 {
     Configuration = new DownloadConfiguration()
     {
         OnTheFlyDownload = true
     };
     base.Initial();
 }
        static void PreDownload(DownloadConfiguration configuration)
        {
            StartDriveDownloadCheckConfiguration dcc = configuration as StartDriveDownloadCheckConfiguration;

            if (dcc != null)
            {
                dcc.Checked = true;
            }
        }
Esempio n. 10
0
        static async Task Main(string[] args)
        {
            var chunkCount = 8;

            DownloadList = File.Exists(DownloadListFile)
                ? JsonConvert.DeserializeObject <List <DownloadItem> >(File.ReadAllText(DownloadListFile))
                : null;

            DownloadList ??= new List <DownloadItem>
            {
                new DownloadItem {
                    FileName = Path.Combine(Path.GetTempPath(), "100MB.zip"), Url = "http://ipv4.download.thinkbroadband.com/100MB.zip"
                }
            };

            var options = new ProgressBarOptions
            {
                ForegroundColor     = ConsoleColor.Green,
                ForegroundColorDone = ConsoleColor.DarkGreen,
                BackgroundColor     = ConsoleColor.DarkGray,
                BackgroundCharacter = '\u2593'
            };

            ChildOption = new ProgressBarOptions
            {
                ForegroundColor   = ConsoleColor.Yellow,
                BackgroundColor   = ConsoleColor.DarkGray,
                ProgressCharacter = '─'
            };

            var downloadOpt = new DownloadConfiguration()
            {
                ParallelDownload      = true,         // download parts of file as parallel or not
                BufferBlockSize       = 10240,        // usually, hosts support max to 8000 bytes
                ChunkCount            = chunkCount,   // file parts to download
                MaxTryAgainOnFailover = int.MaxValue, // the maximum number of times to fail.
                OnTheFlyDownload      = true,         // caching in-memory mode
                Timeout = 1000                        // timeout (millisecond) per stream block reader
            };
            var ds = new DownloadService(downloadOpt);

            ds.ChunkDownloadProgressChanged += OnChunkDownloadProgressChanged;
            ds.DownloadProgressChanged      += OnDownloadProgressChanged;
            ds.DownloadFileCompleted        += OnDownloadFileCompleted;

            foreach (var downloadItem in DownloadList)
            {
                Console.Clear();
                ConsoleProgress        = new ProgressBar(10000, $"Downloading {Path.GetFileName(downloadItem.FileName)} file", options);
                ChildConsoleProgresses = new ConcurrentDictionary <string, ChildProgressBar>();

                await ds.DownloadFileAsync(downloadItem.Url, downloadItem.FileName).ConfigureAwait(false);

                ds.Clear();
            }
        }
 public override void InitialTest()
 {
     Config = new DownloadConfiguration {
         ParallelDownload      = true,
         OnTheFlyDownload      = false,
         BufferBlockSize       = 1024,
         ChunkCount            = 8,
         MaxTryAgainOnFailover = 100
     };
 }
Esempio n. 12
0
 public void InitialTests()
 {
     _configuration = new DownloadConfiguration()
     {
         Timeout = 100,
         MaxTryAgainOnFailover = 100,
         BufferBlockSize       = 1024,
         OnTheFlyDownload      = true
     };
 }
Esempio n. 13
0
 public void Initial()
 {
     _configuration = new DownloadConfiguration {
         BufferBlockSize       = 1024,
         ChunkCount            = 16,
         ParallelDownload      = true,
         MaxTryAgainOnFailover = 100,
         Timeout          = 100,
         OnTheFlyDownload = true
     };
 }
Esempio n. 14
0
 public ChunkDownloaderTest()
     : base(null, null)
 {
     Configuration = new DownloadConfiguration {
         BufferBlockSize       = 1024,
         ChunkCount            = 32,
         ParallelDownload      = true,
         MaxTryAgainOnFailover = 100,
         Timeout          = 100,
         OnTheFlyDownload = true
     };
 }
Esempio n. 15
0
        public void SpeedLimitTest()
        {
            var speedPerSecondsHistory = new ConcurrentBag <long>();
            var lastTick         = 0L;
            var expectedFileSize = DownloadTestHelper.FileSize10Mb; // real bytes size
            var address          = DownloadTestHelper.File10MbUrl;
            var file             = new FileInfo(Path.GetTempFileName());
            var config           = new DownloadConfiguration()
            {
                BufferBlockSize       = 1024,
                ChunkCount            = 8,
                ParallelDownload      = true,
                MaxTryAgainOnFailover = 100,
                OnTheFlyDownload      = true,
                MaximumBytesPerSecond = 1024 * 1024 // 1MB/s
            };
            var progressCount = config.ChunkCount * (int)Math.Ceiling((double)expectedFileSize / config.ChunkCount / config.BufferBlockSize);
            var downloader    = new DownloadService(config);

            downloader.DownloadProgressChanged += (s, e) =>
            {
                Interlocked.Decrement(ref progressCount);
                if (Environment.TickCount64 - lastTick >= 1000)
                {
                    speedPerSecondsHistory.Add(e.BytesPerSecondSpeed);
                    lastTick = Environment.TickCount64;
                }
            };

            downloader.DownloadFileAsync(address, file.FullName).Wait(); // wait to download stopped!
            var avgSpeed = (long)speedPerSecondsHistory.Average();

            Assert.IsTrue(file.Exists);
            Assert.AreEqual(expectedFileSize, downloader.Package.TotalFileSize);
            Assert.AreEqual(expectedFileSize, file.Length);
            Assert.IsTrue(progressCount <= 0);
            Assert.IsTrue(avgSpeed <= config.MaximumBytesPerSecond);

            if (File.Exists(file.FullName))
            {
                try
                {
                    file.Delete();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
        }
Esempio n. 16
0
        public void StopResumeOnTheFlyDownloadTest()
        {
            var stopCount = 0;
            var downloadCompletedSuccessfully = false;
            var expectedFileSize = DownloadTestHelper.FileSize150Kb; // real bytes size
            var address          = DownloadTestHelper.File150KbUrl;
            var file             = new FileInfo(Path.GetTempFileName());
            var config           = new DownloadConfiguration()
            {
                BufferBlockSize       = 1024,
                ChunkCount            = 8,
                ParallelDownload      = false,
                MaxTryAgainOnFailover = 100,
                OnTheFlyDownload      = true
            };
            var progressCount = config.ChunkCount * (int)Math.Ceiling((double)expectedFileSize / config.ChunkCount / config.BufferBlockSize);
            var downloader    = new DownloadService(config);

            downloader.DownloadProgressChanged += delegate { Interlocked.Decrement(ref progressCount); };
            downloader.DownloadFileCompleted   += (s, e) =>
            {
                if (e.Cancelled)
                {
                    Interlocked.Increment(ref stopCount);
                }
                else if (e.Error == null)
                {
                    downloadCompletedSuccessfully = true;
                }
            };

            downloader.CancelAfterDownloading(10);                       // Stopping after start of downloading.
            downloader.DownloadFileAsync(address, file.FullName).Wait(); // wait to download stopped!
            Assert.AreEqual(1, stopCount);
            downloader.CancelAfterDownloading(10);                       // Stopping after resume of downloading.
            downloader.DownloadFileAsync(downloader.Package).Wait();     // resume download from stooped point.
            Assert.AreEqual(2, stopCount);
            Assert.IsFalse(downloadCompletedSuccessfully);
            downloader.DownloadFileAsync(downloader.Package).Wait(); // resume download from stooped point, again.

            Assert.IsTrue(file.Exists);
            Assert.AreEqual(expectedFileSize, downloader.Package.TotalFileSize);
            Assert.AreEqual(expectedFileSize, file.Length);
            Assert.IsTrue(progressCount <= 0);
            Assert.AreEqual(2, stopCount);
            Assert.IsTrue(downloadCompletedSuccessfully);

            file.Delete();
        }
Esempio n. 17
0
        public static async void DowloadServer(string url, string fileName)
        {
            var downloadOpt = new DownloadConfiguration()
            {
                BufferBlockSize       = 10240,        // usually, hosts support max to 8000 bytes, default values is 8000
                ChunkCount            = 8,            // file parts to download, default value is 1
                MaximumBytesPerSecond = int.MaxValue, // download speed limited to 1MB/s, default values is zero or unlimited
                MaxTryAgainOnFailover = int.MaxValue, // the maximum number of times to fail
                OnTheFlyDownload      = false,        // caching in-memory or not? default values is true
                ParallelDownload      = true,         // download parts of file as parallel or not. Default value is false
                TempDirectory         = "C:\\temp",   // Set the temp path for buffering chunk files, the default path is Path.GetTempPath()
                Timeout = 1000,                       // timeout (millisecond) per stream block reader, default values is 1000
                RequestConfiguration =                // config and customize request headers
                {
                    Accept                 = "*/*",
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
                    CookieContainer        = new CookieContainer(),    // Add your cookies
                    Headers               = new WebHeaderCollection(), // Add your custom headers
                    KeepAlive             = false,
                    ProtocolVersion       = HttpVersion.Version11,     // Default value is HTTP 1.1
                    UseDefaultCredentials = false,
                    UserAgent             = $"DownloaderSample/{Assembly.GetExecutingAssembly().GetName().Version.ToString(3)}"
                }
            };

            DownloadService downloader = new DownloadService(downloadOpt);

            downloader.DownloadStarted         += OnDowloadStarted;
            downloader.DownloadFileCompleted   += OnDownloadFileCompleted;
            downloader.DownloadProgressChanged += OnDowloadProgresChanged;

            await downloader.DownloadFileTaskAsync(url, fileName);


            void OnDowloadStarted(object sender, DownloadStartedEventArgs e)
            {
                UCLogsViewModel.TextLogs.Value += "\nDowload started";
            }

            void OnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
            {
                DowloadInfoUpdater.OnStopUpdateInfo();
            }

            void OnDowloadProgresChanged(object sender, DownloadProgressChangedEventArgs e)
            {
                DowloadInfoUpdater.OnNewUpdateInfo(e);
            }
        }
Esempio n. 18
0
        public void MaximumSpeedPerChunkTest()
        {
            // arrange
            var configuration =
                new DownloadConfiguration {
                MaximumBytesPerSecond = 10240,
                ParallelDownload      = true,
                ChunkCount            = 10
            };

            // act
            var maxSpeed = configuration.MaximumSpeedPerChunk();

            // assert
            Assert.AreEqual(configuration.MaximumBytesPerSecond / configuration.ChunkCount, maxSpeed);
        }
        public void BufferBlockSizeTest()
        {
            // arrange
            var configuration =
                new DownloadConfiguration {
                MaximumBytesPerSecond = 10240,
                ParallelDownload      = true,
                ChunkCount            = 10
            };

            // act
            configuration.BufferBlockSize = 10240 * 2;

            // assert
            Assert.AreEqual(configuration.BufferBlockSize, configuration.MaximumSpeedPerChunk);
        }
Esempio n. 20
0
        public void DownloadJson1KTest()
        {
            var downloadCompletedSuccessfully = false;
            var expectedFileSize = DownloadTestHelper.FileSize1Kb; // real bytes size
            var address          = DownloadTestHelper.File1KbUrl;
            var file             = new FileInfo(Path.GetTempFileName());
            var config           = new DownloadConfiguration()
            {
                AllowedHeadRequest    = false,
                BufferBlockSize       = 1024,
                ChunkCount            = 16,
                ParallelDownload      = false,
                MaxTryAgainOnFailover = 100,
                OnTheFlyDownload      = true
            };
            var progressCount = config.ChunkCount * (int)Math.Ceiling((double)expectedFileSize / config.ChunkCount / config.BufferBlockSize);
            var downloader    = new DownloadService(config);

            downloader.DownloadProgressChanged += delegate
            {
                Interlocked.Decrement(ref progressCount);
            };

            downloader.DownloadFileCompleted += (s, e) =>
            {
                if (e.Cancelled == false && e.Error == null)
                {
                    downloadCompletedSuccessfully = true;
                }
            };

            downloader.DownloadFileAsync(address, file.FullName).Wait();
            Assert.IsTrue(file.Exists);
            Assert.AreEqual(expectedFileSize, downloader.Package.TotalFileSize);
            Assert.AreEqual(expectedFileSize, file.Length);
            Assert.AreEqual(0, progressCount);

            using (var reader = file.OpenText())
            {
                var json = reader.ReadToEnd();
                var obj  = JsonConvert.DeserializeObject(json);
                Assert.IsNotNull(obj);
            }

            Assert.IsTrue(downloadCompletedSuccessfully);
            file.Delete();
        }
Esempio n. 21
0
        private void InitDownloader()
        {
            var config = new DownloadConfiguration()
            {
                ChunkCount                  = Model.ChunksCount,
                ParallelDownload            = Model.ParallelDownload,
                OnTheFlyDownload            = Model.DownloadOnTheFly,
                MaximumBytesPerSecond       = Model.Speed,
                TempDirectory               = Model.TempDirectory,
                CheckDiskSizeBeforeDownload = true
            };

            _downloader?.Dispose();
            _downloader = new DownloadService(config);
            _downloader.DownloadStarted         += OnDownloadStarted;
            _downloader.DownloadProgressChanged += OnProgressChanged;
            _downloader.DownloadFileCompleted   += OnDownloadCompleted;
        }
Esempio n. 22
0
        public static void Init()
        {
            string downloadConfigFilePath = GetDownloadConfigFilePath();

            if (File.Exists(downloadConfigFilePath))
            {
                DownloadConfiguration = Utilities.DeserializeJson <DownloadConfiguration>(File.ReadAllText(downloadConfigFilePath));
            }
            else
            {
                if (!Directory.Exists(Path.GetDirectoryName(downloadConfigFilePath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(downloadConfigFilePath));
                }
                DownloadConfiguration = new DownloadConfiguration();
                File.WriteAllText(downloadConfigFilePath, Utilities.SerializeJson(new DownloadConfiguration()));
            }
        }
Esempio n. 23
0
        private static async Task Main()
        {
            try
            {
                Initial();
                List <DownloadItem> downloadList = await GetDownloadItems();

                DownloadConfiguration downloadOpt = GetDownloadConfiguration();
                await DownloadList(downloadList, downloadOpt);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e);
                Debugger.Break();
            }

            Console.WriteLine("END");
            Console.Read();
        }
Esempio n. 24
0
        private static async Task <DownloadService> DownloadFile(DownloadItem downloadItem)
        {
            _currentDownloadConfiguration = GetDownloadConfiguration();
            _currentDownloadService       = new DownloadService(_currentDownloadConfiguration);
            _currentDownloadService.ChunkDownloadProgressChanged += OnChunkDownloadProgressChanged;
            _currentDownloadService.DownloadProgressChanged      += OnDownloadProgressChanged;
            _currentDownloadService.DownloadFileCompleted        += OnDownloadFileCompleted;
            _currentDownloadService.DownloadStarted += OnDownloadStarted;

            if (string.IsNullOrWhiteSpace(downloadItem.FileName))
            {
                await _currentDownloadService.DownloadFileTaskAsync(downloadItem.Url, new DirectoryInfo(downloadItem.FolderPath)).ConfigureAwait(false);
            }
            else
            {
                await _currentDownloadService.DownloadFileTaskAsync(downloadItem.Url, downloadItem.FileName).ConfigureAwait(false);
            }

            return(_currentDownloadService);
        }
Esempio n. 25
0
        /// <summary>
        /// Instance Downloader with base constructor
        /// </summary>
        public BlackVueDownloader() : this(new FileSystemHelper())
        {
            var config = new DownloadConfiguration
            {
                MaxTryAgainOnFailover = 3,
                ChunkCount            = 1,
                ParallelDownload      = true,
                Timeout = 2500,
                RequestConfiguration =
                {
                    KeepAlive = true
                }
            };

            downloader = new DownloadService();


            downloader.DownloadStarted       += OnDownloadStarted;
            downloader.DownloadFileCompleted += OnDownloadFileCompleted;
            //downloader.ChunkDownloadProgressChanged += OnChunkDownloadProgressChanged;
            downloader.DownloadProgressChanged += OnDownloadProgressChanged;;
        }
Esempio n. 26
0
        private static async Task <DownloadService> DownloadFile(DownloadItem downloadItem,
                                                                 DownloadConfiguration downloadOpt)
        {
            DownloadService ds = new DownloadService(downloadOpt);

            ds.ChunkDownloadProgressChanged += OnChunkDownloadProgressChanged;
            ds.DownloadProgressChanged      += OnDownloadProgressChanged;
            ds.DownloadFileCompleted        += OnDownloadFileCompleted;
            ds.DownloadStarted += OnDownloadStarted;

            if (string.IsNullOrWhiteSpace(downloadItem.FileName))
            {
                await ds.DownloadFileAsync(downloadItem.Url, new DirectoryInfo(downloadItem.FolderPath))
                .ConfigureAwait(false);
            }
            else
            {
                await ds.DownloadFileAsync(downloadItem.Url, downloadItem.FileName).ConfigureAwait(false);
            }

            return(ds);
        }
Esempio n. 27
0
        public void DownloadJson1KTest()
        {
            var expectedFileSize = 20471; // real bytes size
            var address          = "https://file-examples.com/wp-content/uploads/2017/02/file_example_JSON_1kb.json";
            var file             = new FileInfo(Path.GetTempFileName());
            var config           = new DownloadConfiguration()
            {
                BufferBlockSize       = 1024,
                ChunkCount            = 16,
                ParallelDownload      = false,
                MaxTryAgainOnFailover = 100,
                OnTheFlyDownload      = true
            };
            var progressCount = config.ChunkCount * (int)Math.Ceiling((double)expectedFileSize / config.ChunkCount / config.BufferBlockSize);
            var downloader    = new DownloadService(config);

            downloader.DownloadProgressChanged += delegate
            {
                Interlocked.Decrement(ref progressCount);
            };

            downloader.DownloadFileAsync(address, file.FullName).Wait();
            Assert.IsTrue(file.Exists);
            Assert.AreEqual(expectedFileSize, downloader.Package.TotalFileSize);
            Assert.AreEqual(expectedFileSize, file.Length);
            Assert.AreEqual(0, progressCount);

            using (var reader = file.OpenText())
            {
                var json = reader.ReadToEnd();
                var obj  = JsonConvert.DeserializeObject(json);
                Assert.IsNotNull(obj);
            }

            file.Delete();
        }
Esempio n. 28
0
 public ResourceUpdater(Resource resource, DownloadConfiguration config)
 {
     m_resource = resource;
     m_uploadURL = config.UploadURL;
     m_baseURL = config.BaseURL;
 }
        /// <summary>
        /// Process any updates to moduledownloaderresources.xml.
        /// </summary>
        /// <param name="Database">Supplies the database object.</param>
        /// <param name="Script">Supplies the script object.</param>
        /// <returns>True if a patch was applied and a reboot is required for
        /// it to take effect.</returns>
        public static bool ProcessModuleDownloaderResourcesUpdates(ALFA.Database Database, ACR_ServerCommunicator Script)
        {
            bool ContentChanged = false;

            //
            // Check the database for the expected download server configurations.
            //

            Database.ACR_SQLQuery("SELECT `Hash`, `DownloadHash`, `Name`, `DLSize`, `Size` FROM `content_download_config`");

            List<DownloadConfiguration> hakConfigs = new List<DownloadConfiguration>();

            //
            // Build a list of expected download configurations.
            //

            while (Database.ACR_SQLFetch())
            {
                DownloadConfiguration downloadConfig = new DownloadConfiguration();

                downloadConfig.Hash = Database.ACR_SQLGetData(0);
                downloadConfig.DownloadHash = Database.ACR_SQLGetData(1);
                downloadConfig.Name = Database.ACR_SQLGetData(2);
                downloadConfig.DLSize = Database.ACR_SQLGetData(3);
                downloadConfig.Size = Database.ACR_SQLGetData(4);

                if (String.IsNullOrEmpty(downloadConfig.Hash) ||
                    String.IsNullOrEmpty(downloadConfig.DownloadHash) ||
                    String.IsNullOrEmpty(downloadConfig.Name) ||
                    String.IsNullOrEmpty(downloadConfig.DLSize) ||
                    String.IsNullOrEmpty(downloadConfig.Size))
                {
                    continue;
                }

                hakConfigs.Add(downloadConfig);
            }

            //
            // If we have any configuration to do, we loop through and compare the 
            // configuration on the database with the configuration on the server,
            // updating the downloadresources xml if it is new.
            //

            if (hakConfigs.Count > 0)
            {
                XmlDocument moduleDownloadResources = new XmlDocument();
                moduleDownloadResources.Load(ALFA.SystemInfo.GetModuleDirectory() + "\\moduledownloaderresources.xml");
                XmlElement downloadResources = moduleDownloadResources.DocumentElement;

                foreach (DownloadConfiguration config in hakConfigs)
                {
                    foreach (XmlNode node in downloadResources.ChildNodes)
                    {
                        if (node.Attributes["name"].Value == config.Name)
                        {
                            if (node.Attributes["hash"].Value != config.Hash)
                            {
                                node.Attributes["hash"].Value = config.Hash;
                                ContentChanged = true;
                            }
                            if (node.Attributes["downloadHash"].Value != config.DownloadHash)
                            {
                                node.Attributes["downloadHash"].Value = config.DownloadHash;
                                ContentChanged = true;
                            }
                            if (node.Attributes["dlsize"].Value != config.DLSize)
                            {
                                node.Attributes["dlsize"].Value = config.DLSize;
                                ContentChanged = true;
                            }
                            if (node.Attributes["size"].Value != config.Size)
                            {
                                node.Attributes["size"].Value = config.Size;
                                ContentChanged = true;
                            }

                            if (ContentChanged)
                            {
                                Script.WriteTimestampedLogEntry(String.Format(
                                    "ModuleContentPatcher.ProcessModuleDownloaderResourcesUpdates: Updated downloader resource {0} (hash {1}).",
                                    config.Name,
                                    config.Hash));
                            }
                        }
                    }
                }

                if (ContentChanged)
                    moduleDownloadResources.Save(ALFA.SystemInfo.GetModuleDirectory() + "\\moduledownloaderresources.xml");
            }

            return ContentChanged;
        }
Esempio n. 30
0
        private async Task _downLoadMp3(string url, string name, string Savepath, string Ip, Action <DownloadService, DownloadProgressChangedEventArgs> TotalProgressChanged, Action <DownloadService, AsyncCompletedEventArgs> FileDownloadFinished)
        {
            try
            {
                Directory.CreateDirectory(Savepath);

                DownloadConfiguration configuration = new DownloadConfiguration();
                configuration.SaveDirectory = Savepath;
                configuration.RequestConfiguration.Headers.Add("X-Forwarded-For", Ip);
                configuration.RequestConfiguration.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
                configuration.RequestConfiguration.Host = "e1.ixinmo.com";
                configuration.RequestConfiguration.Headers.Add("Cookie", "__cfduid=d8407223173229c4c785f13d7eec632fe1602592716");
                configuration.RequestConfiguration.Referer     = "http://m.ixinmo.com/";
                configuration.RequestConfiguration.UserAgent   = "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1";
                configuration.RequestConfiguration.Accept      = "*/*";
                configuration.RequestConfiguration.ContentType = null;
                configuration.RequestConfiguration.KeepAlive   = false;
                configuration.RequestConfiguration.Timeout     = 30 * 1000;


                DownloadService dService = new DownloadService(configuration);
                dService.DownloadProgressChanged += (sender, e) => {
                    TotalProgressChanged((DownloadService)sender, e);
                };
                dService.DownloadFileCompleted += (sender, e) =>
                {
                    FileDownloadFinished((DownloadService)sender, e);
                };
                await dService.DownloadFileAsync(url, name);



                //string packageID = DownloadManager.AddTask(url, name, Savepath, 4, 1);
                //DownloadManager.UpdatePackage(packageID, (package) =>
                //{
                //    package.Options.RequestConfiguration.Headers.Add("X-Forwarded-For", Ip);
                //    package.Options.RequestConfiguration.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
                //    package.Options.RequestConfiguration.Host = "e1.ixinmo.com";
                //    package.Options.RequestConfiguration.Headers.Add("Cookie", "__cfduid=d8407223173229c4c785f13d7eec632fe1602592716");
                //    package.Options.RequestConfiguration.Referer = "http://m.ixinmo.com/";
                //    package.Options.RequestConfiguration.UserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1";
                //    package.Options.RequestConfiguration.Accept = "*/*";
                //    package.Options.RequestConfiguration.ContentType = null;
                //    package.Options.RequestConfiguration.KeepAlive = false;
                //    package.Options.RequestConfiguration.Timeout = 30 * 1000;

                //});
                //var mtd = new MultiThreadDownloader(url, Path.Combine(Savepath, name), 3);
                //mtd.Configure(item =>
                //{
                //    item.Headers.Add("X-Forwarded-For", Ip);
                //    item.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9");
                //    item.Host = "e1.ixinmo.com";
                //    //CookieContainer cookie= new CookieContainer();
                //    item.Headers.Add("Cookie", "__cfduid=d8407223173229c4c785f13d7eec632fe1602592716");
                //    //item.CookieContainer.Add(new System.Net.Cookie("__cfduid", "d8407223173229c4c785f13d7eec632fe1602592716"));
                //    item.Referer = "http://m.ixinmo.com/";
                //    item.UserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1";
                //    item.Accept = "*/*";
                //    item.ContentType = null;
                //    item.KeepAlive = false;
                //    item.Timeout = 30 * 1000;

                //});
                ////mtd.TotalProgressChanged = TotalProgressChanged;
                //mtd.TotalProgressChanged += (sender) =>
                //{
                //    if (TotalProgressChanged != null)
                //    {
                //        TotalProgressChanged(sender);
                //    }
                //    //var downloader = sender;
                //    //Console.WriteLine("下载进度:" + downloader.TotalProgress + "%");
                //    //Console.WriteLine("下载速度:" + downloader.TotalSpeedInBytes / 1024 / 1024 + "MBps");
                //};
                //mtd.FileDownloadFinished += (sender, e) =>
                //{
                //    if (FileDownloadFinished != null)
                //    {
                //        FileDownloadFinished((MultiThreadDownloader)sender);
                //    }
                //    //Console.WriteLine($"下载完成 {e}");
                //};
                //mtd.Start();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 31
0
        private static async Task DownloadList(IEnumerable <DownloadItem> downloadList, DownloadConfiguration config)
        {
            foreach (DownloadItem downloadItem in downloadList)
            {
                // begin download from url
                DownloadService ds = await DownloadFile(downloadItem, config);

                await Task.Delay(1000);

                // clear download to order new of one
                ds.Clear();
            }
        }
Esempio n. 32
0
    public static async Task <string> MultiThreadDownloadFileAsync(
        this string url,
        string tempDir,
        string fileName = ""
        )
    {
        var throttleDispatcher = new ThrottleDispatcher(2000);

        var pathFileName = Path.GetFileName(url);

        if (fileName.IsNotNullOrEmpty())
        {
            pathFileName = fileName;
        }

        var paths = Path.Combine(
            "Storage/Caches/",
            tempDir,
            pathFileName
            );

        var downloadOpt = new DownloadConfiguration()
        {
            ChunkCount            = 8,
            ParallelDownload      = true,
            MaxTryAgainOnFailover = 10,
            OnTheFlyDownload      = false,
            RequestConfiguration  = // config and customize request headers
            {
                Accept                 = "*/*",
                AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
                CookieContainer        = new CookieContainer(),    // Add your cookies
                Headers               = new WebHeaderCollection(), // Add your custom headers
                KeepAlive             = false,                     // default value is false
                ProtocolVersion       = HttpVersion.Version11,     // Default value is HTTP 1.1
                UseDefaultCredentials = false,
                UserAgent             = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36 Edg/101.0.1210.53"
            }
        };

        var downloader = new DownloadService(downloadOpt);

        downloader.DownloadStarted += (
            sender,
            args
            ) => {
            Log.Information(
                "Downloading File. FileName: {FileName}, Size: {Size}",
                args.FileName,
                args.TotalBytesToReceive
                );
        };

        downloader.DownloadProgressChanged += (
            sender,
            args
            ) => {
            var downloadService = sender as DownloadService;
            if (downloadService == null)
            {
                return;
            }

            var downloadPackage = downloadService.Package;
            throttleDispatcher.Throttle(
                () => {
                Log.Debug(
                    "Downloading URL: {FileName}. {Run}/{Size} - {Speed} ({Progress}%)",
                    downloadPackage.Address,
                    downloadPackage.ReceivedBytesSize.SizeFormat(),
                    downloadPackage.TotalFileSize.SizeFormat(),
                    args.AverageBytesPerSecondSpeed.SizeFormat("/s"),
                    args.ProgressPercentage.ToString("N2")
                    );
            }
                );
        };

        downloader.DownloadFileCompleted += (
            sender,
            args
            ) => {
            var downloadService = sender as DownloadService;
            if (downloadService == null)
            {
                return;
            }

            var downloadPackage = downloadService.Package;

            Log.Information(
                "Download completed. Url: {Address}. Size: {Size}",
                downloadPackage.Address,
                downloadPackage.TotalFileSize.SizeFormat()
                );
        };

        await downloader.DownloadFileTaskAsync(url, paths);

        return(paths);
    }