Exemplo n.º 1
0
        public void Test_Download_must_start_if_finished_file_doesnot_exist()
        {
            var path = Path.Combine(TempPath, Guid.NewGuid().ToString());

            using var storage = PortableStorage.Providers.FileStorgeProvider.CreateStorage(path, true, null);

            var uri       = new Uri("https://download.sysinternals.com/files/SysinternalsSuite-ARM64.zip");
            var dmOptions = new DownloadManagerOptions()
            {
                Storage = storage
            };

            using var dm = new DownloadManager(dmOptions);

            dm.Add("file1", uri, false);
            dm.Add("file1", uri, true);
            Assert.IsFalse(dm.IsIdle, "dowload is not started after second add");

            dm.Add("file1", uri);
            WaitForAllDownloads(dm);

            storage.DeleteStream("file1");

            dm.Add("file1", uri);
            WaitForAllDownloads(dm);
            Assert.IsTrue(storage.StreamExists("file1"));
        }
Exemplo n.º 2
0
        public static void ForReadME3()
        {
            // Create a portable storage
            using var storage = PortableStorage.Providers.FileStorgeProvider.CreateStorage(@"c:\temp", true, null);

            // Create a portable download manager
            var dmOptions = new DownloadManagerOptions()
            {
                Storage = storage, MaxOfSimultaneousDownloads = 3
            };

            using var dm = new DownloadManager(dmOptions);

            dm.Add("file1.zip", new Uri("https://abcd.com/file1.zip"));
            dm.Add("file2.zip", new Uri("https://abcd.com/file2.zip"));
            dm.Add("folder/file3.zip", new Uri("https://abcd.com/file3.zip"));
            dm.Add("folder/file4.zip", new Uri("https://abcd.com/file4.zip"));
            dm.Add("folder/file5.zip", new Uri("https://abcd.com/file5.zip"));

            // wait for downloads
            while (!dm.IsIdle)
            {
                Thread.Sleep(500);
            }

            // done
        }
Exemplo n.º 3
0
        //[TestMethod]
        public void Test_Foo()
        {
            var path = Path.Combine(TempPath, Guid.NewGuid().ToString());

            using var storage = PortableStorage.Providers.FileStorgeProvider.CreateStorage(path, true, null);

            var uri       = new Uri("https://az792536.vo.msecnd.net/vms/VMBuild_20190311/VirtualBox/MSEdge/MSEdge.Win10.VirtualBox.zip");
            var dmOptions = new DownloadManagerOptions()
            {
                Storage = storage,
                MaxOfSimultaneousDownloads = 1,
                PartSize      = 1000 * 1000000,
                MaxPartCount  = 1,
                AllowResuming = true,
            };

            using var dm = new DownloadManager(dmOptions);
            dm.Add("file1", uri);
            dm.Start("file1");

            while (!dm.IsIdle)
            {
                Debug.WriteLine(dm.GetItem().BytesPerSecond / 1000);
                Thread.Sleep(1000);
            }


            Assert.IsTrue(storage.EntryExists("file1"));
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            if (args.Length < 3)
            {
                ShowHelp();
                return;
            }

            var dmOptions = new DownloadManagerOptions()
            {
                RestoreLastList = false
            };
            var continueAfterRestart = false;

            var url  = args[0];
            var path = args[1];

            //process argument
            var lastKey = "";

            foreach (var item in args)
            {
                if (lastKey.Equals("/PartSize", StringComparison.InvariantCultureIgnoreCase))
                {
                    dmOptions.PartSize = long.Parse(item);
                }

                else if (lastKey.Equals("/MaxPartCount", StringComparison.InvariantCultureIgnoreCase))
                {
                    dmOptions.MaxPartCount = int.Parse(item);
                }

                else if (lastKey.Equals("/WriteBufferSize", StringComparison.InvariantCultureIgnoreCase))
                {
                    dmOptions.WriteBufferSize = int.Parse(item);
                }

                else if (item.Equals("/DisableResuming", StringComparison.InvariantCultureIgnoreCase))
                {
                    dmOptions.AllowResuming = false;
                }


                lastKey = item;
            }

            if (args.Contains("/t"))
            {
                DownloadByWebClient(url, path);
            }

            // download by portable
            DownloadByPortableDownloader(url, path, dmOptions, continueAfterRestart);
        }
Exemplo n.º 5
0
        public void Test_Storage_Download()
        {
            var path = Path.Combine(TempPath, Guid.NewGuid().ToString());

            using var storage = PortableStorage.Providers.FileStorgeProvider.CreateStorage(path, true, null);

            var uri       = new Uri("https://download.sysinternals.com/files/SysinternalsSuite-ARM64.zip");
            var dmOptions = new DownloadManagerOptions()
            {
                Storage = storage, MaxOfSimultaneousDownloads = 100
            };

            using (var dm = new DownloadManager(dmOptions))
            {
                dm.Add("file1", uri, false);
                dm.Add("file2", uri, false);
                dm.Add("folder1/file1", uri, false);
                dm.Add("folder1/file2", uri, false);
                dm.Add("folder1/file3", uri, false);

                // no item should be started
                Assert.AreEqual(0, dm.Items.Count(x => x.IsStarted));

                // check the number of added items
                dm.Start("folder1");
                Assert.AreEqual(3, dm.Items.Count(x => x.IsStarted));

                WaitForAllDownloads(dm);
                Assert.AreEqual(2, dm.Items.Count(x => x.State == DownloadState.Stopped), "invalid number of item with none state");
                Assert.AreEqual(3, dm.Items.Count(x => x.State == DownloadState.Finished), "invalid number of item with finish state");
            }

            Assert.IsTrue(storage.EntryExists($"folder1/file1"), "item has not been downloaded!");
            Assert.IsTrue(storage.EntryExists($"folder1/file2"), "item has not been downloaded!");
            Assert.IsTrue(storage.EntryExists($"folder1/file3"), "item has not been downloaded!");
        }
Exemplo n.º 6
0
        static void DownloadByPortableDownloader(string url, string path, DownloadManagerOptions options, bool continueAfterRestart)
        {
            var startTime = DateTime.Now;

            Console.WriteLine($"Download using PortableDownloader. \nMaxPartCount: {options.MaxPartCount}, \nPartSize: {options.PartSize}, \nAllowResuming: {options.AllowResuming}, \nWriteBufferSize: {options.WriteBufferSize}");
            Console.WriteLine();
            using var storage = PortableStorage.Providers.FileStorgeProvider.CreateStorage(Path.GetDirectoryName(path), true, null);
            options.Storage   = storage;
            using var dm      = new DownloadManager(options);
            var streamPath = Path.GetFileName(path);

            //delete old download
            if (!continueAfterRestart)
            {
                if (storage.StreamExists(streamPath))
                {
                    storage.DeleteStream(streamPath);
                }
                dm.Cancel(streamPath);
            }

            dm.Add(streamPath, new Uri(url));
            dm.Add(streamPath + "a", new Uri(url));
            dm.Add(streamPath + "2", new Uri("https://s3.amazonaws.com/bpacks/bbcpersian/bbcpersian.jpg"));
            dm.Add(streamPath + "3", new Uri("https://s3.amazonaws.com/bpacks/bbcpersian/bpack_meta.json"));
            while (!dm.IsIdle)
            {
                var item         = dm.GetItem();
                var totalSeconds = Math.Max(1, (int)(DateTime.Now - startTime).TotalSeconds);
                var speed        = ((float)item.BytesPerSecond / 1000000).ToString("0.00");
                Console.WriteLine($"Downloaded: {item.CurrentSize} / { item.TotalSize }, Timer: {totalSeconds} Seconds, Speed: {speed} MB/s ");
                Thread.Sleep(1000);
            }

            ReportSpeed(startTime, dm.GetItem().TotalSize);
        }
Exemplo n.º 7
0
        public void Test_Add_Start_Stop_Restore_Cancel()
        {
            var path = Path.Combine(TempPath, Guid.NewGuid().ToString());

            using var storage = PortableStorage.Providers.FileStorgeProvider.CreateStorage(path, true, null);

            var uri       = new Uri("https://download.sysinternals.com/files/SysinternalsSuite-ARM64.zip");
            var dmOptions = new DownloadManagerOptions()
            {
                Storage = storage, MaxOfSimultaneousDownloads = 100
            };

            using (var dm = new DownloadManager(dmOptions))
            {
                dm.Add("file1", uri, false);
                dm.Add("file2", uri, false);
                dm.Add("file3", uri, false);
                dm.Add("folder1/file1", uri, false);
                dm.Add("folder1/file2", uri, false);
                dm.Add("folder1/file3", uri, false);

                // check the number of added items
                Assert.AreEqual(6, dm.Items.Length, "Invalid number of added items");
                Assert.AreEqual(6, dm.GetItems("/").Length, "Invalid number of added items");
                Assert.IsTrue(dm.Items.All(x => x.State == DownloadState.Stopped), "all items but has been in none state");

                // start downloading
                dm.Start("file1");
                dm.Start("folder1/file1");
                dm.Stop("file3");

                // check number of started item
                Assert.AreEqual(2, dm.Items.Count(x => x.IsStarted), "invalid number of started items");
                WaitForAllDownloads(dm);

                // check for errors
                Assert.IsFalse(dm.Items.Any(x => x.State == DownloadState.Error), "there is an error in downloads");

                // check for downloaded stream
                Assert.IsTrue(storage.EntryExists("file1"));
                Assert.IsTrue(storage.EntryExists("folder1/file1"));
                Assert.AreEqual(2, dm.Items.Count(x => x.State == DownloadState.Finished), "2 items must has been finished");

                // check remote finished items
                dm.RemoveFinishedItems();
                Assert.AreEqual(dm.Items.Length, 4, "Invalid number of remained items");

                // download another item
                dm.Start("file3");

                // download another item and cancel it
                dm.Start("folder1/file3");
                dm.Cancel("folder1/file3");

                WaitForAllDownloads(dm);

                // check cancel result
                Assert.AreEqual(1, dm.Items.Count(x => x.State == DownloadState.Finished), "2 items must be finished");
                Assert.IsTrue(storage.EntryExists("file3"));
                Assert.IsFalse(storage.EntryExists($"folder1/file3{dmOptions.DownloadingExtension}"), "item hasn't deleted");
                Assert.IsFalse(storage.EntryExists($"folder1/file3{dmOptions.DownloadingInfoExtension}"), "item hasn't deleted");
            }

            //check restoring
            using (var dm = new DownloadManager(dmOptions))
            {
                Assert.AreEqual(3, dm.Items.Length, 3, "Invalid number of added items");
                Assert.AreEqual(1, dm.Items.Count(x => x.State == DownloadState.Finished), "invalid number of finished items");
                Assert.AreEqual(2, dm.Items.Count(x => x.State == DownloadState.Stopped), "invalid number of not started items");

                dm.Start("folder1/file2");
                Assert.AreEqual(1, dm.Items.Count(x => x.IsStarted), "invalid number of started items");
                Assert.AreEqual(2, dm.Items.Count(x => x.IsIdle), "invalid number of ilde items");
            }

            // restore downloads after restart
            using (var dm = new DownloadManager(dmOptions))
            {
                WaitForAllDownloads(dm);
                Assert.IsTrue(storage.EntryExists("folder1/file2"));
            }
        }