public void Setup()
 {
     mockDownloadStrategy = new Mock <IDownloadStrategy>();
     dlArgs = new DownloadArguments {
         Target = "paket.exe"
     };
     mockFileProxy = new Mock <IFileSystemProxy>();
 }
        public static async Task DownloadAsync(DownloadArguments arguments)
        {
            var process = Enumeration.FromDisplayName <Process>(arguments.ProcessName);

            if (process.Type != Process.ProcessType.Download)
            {
                throw new ArgumentException($"Process {arguments.ProcessName} can not be downloaded.", nameof(process));
            }

            // Configure client to access the API.
            var apiEndpoint = new Uri(arguments.ApiEndpoint);

            using var client = GetClient(apiEndpoint, arguments.Token);

            // Setting value for If-Modified-Since-Header
            if (process.Equals(Process.Pe004) || process.Equals(Process.Pv004))
            {
                if (!arguments.SendIfModifiedSinceHeader && !string.IsNullOrEmpty(arguments.ModifiedSince))
                {
                    throw new ArgumentException("Whether set the --send-if-modified-header to false or give a timestamp for option --modified-since.");
                }

                var modifiedSince = DateTimeOffset.Now;
                if (!string.IsNullOrEmpty(arguments.ModifiedSince) && DateTimeOffset.TryParse(arguments.ModifiedSince, out var tmp))
                {
                    modifiedSince = tmp;
                }
                client.DefaultRequestHeaders.IfModifiedSince = modifiedSince;
            }

            var response = await client.GetAsync(process.Name, CancellationToken.None).ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                var filename = response.Content.Headers.ContentDisposition.FileName;

                await using (var fileStream = File.Create(filename))
                    await using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
                    {
                        await stream.CopyToAsync(fileStream, CancellationToken.None).ConfigureAwait(false);

                        Console.WriteLine($"File {filename} wrote to folder {Directory.GetCurrentDirectory()}.");
                    }

                Console.WriteLine(
                    $"Download file {filename} from API endpoint {new Uri(apiEndpoint, arguments.ProcessName)} was successfully.");
            }
            else
            {
                await PrintErrorAsync(response, new Uri(apiEndpoint, arguments.ProcessName), process.Type, CancellationToken.None)
                .ConfigureAwait(false);
            }
        }
Beispiel #3
0
        public void GetDownloadStrategy_NotCached()
        {
            //arrange

            //act
            var downloadArguments = new DownloadArguments(String.Empty, true, "any", "any", false, String.Empty, true, null);
            var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, false, false);

            //assert
            Assert.That(strategy, Is.TypeOf<GitHubDownloadStrategy>());
            Assert.That(strategy.FallbackStrategy, Is.TypeOf<NugetDownloadStrategy>());
            Assert.That(strategy.FallbackStrategy.FallbackStrategy, Is.Null);
        }
Beispiel #4
0
        public void GetDownloadStrategy_Cached_Nuget_Nothing_ForceOnly()
        {
            //arrange

            //act
            var downloadArguments =  new DownloadArguments(String.Empty, true, "any", "any", false, String.Empty, false, null);
            var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, false, true);

            //assert
            Assert.That(strategy, Is.TypeOf<CacheDownloadStrategy>());
            Assert.That(((CacheDownloadStrategy)strategy).EffectiveStrategy, Is.TypeOf<NugetDownloadStrategy>());
            Assert.That(strategy.FallbackStrategy, Is.Null);
        }
Beispiel #5
0
        public void GetDownloadStrategy_NotCached()
        {
            //arrange

            //act
            var downloadArguments = new DownloadArguments(String.Empty, true, "any", "any", false, String.Empty, true, null);
            var strategy          = Program.GetEffectiveDownloadStrategy(downloadArguments, false, false, TestHelper.ProductionProxyProvider);

            //assert
            Assert.That(strategy, Is.TypeOf <GitHubDownloadStrategy>());
            Assert.That(strategy.FallbackStrategy, Is.TypeOf <NugetDownloadStrategy>());
            Assert.That(strategy.FallbackStrategy.FallbackStrategy, Is.Null);
        }
Beispiel #6
0
        public void GetDownloadStrategy_Cached_Nuget_Nothing_ForceOnly()
        {
            //arrange

            //act
            var downloadArguments = new DownloadArguments(String.Empty, true, "any", "any", false, String.Empty, false, null);
            var strategy          = Program.GetEffectiveDownloadStrategy(downloadArguments, false, true);

            //assert
            Assert.That(strategy, Is.TypeOf <CacheDownloadStrategy>());
            Assert.That(((CacheDownloadStrategy)strategy).EffectiveStrategy, Is.TypeOf <NugetDownloadStrategy>());
            Assert.That(strategy.FallbackStrategy, Is.Null);
        }
Beispiel #7
0
        public void GetDownloadStrategy_TemporarilyIgnored_Cached_Nuget_Nothing()
        {
            //arrange

            //act
            var downloadArguments = new DownloadArguments(String.Empty, true, "any", "any", false, String.Empty, false, 10);
            var strategy          = Program.GetEffectiveDownloadStrategy(downloadArguments, true, true, TestHelper.ProductionProxyProvider);

            //assert
            Assert.That(strategy, Is.TypeOf <TemporarilyIgnoreUpdatesDownloadStrategy>());
            Assert.That(((TemporarilyIgnoreUpdatesDownloadStrategy)strategy).EffectiveStrategy, Is.TypeOf <CacheDownloadStrategy>());
            Assert.That(((CacheDownloadStrategy)((TemporarilyIgnoreUpdatesDownloadStrategy)strategy).EffectiveStrategy).EffectiveStrategy, Is.TypeOf <NugetDownloadStrategy>());
            Assert.That(strategy.FallbackStrategy, Is.Null);
        }
Beispiel #8
0
        public void GetDownloadStrategy_NotCached_TemporarilyIgnored()
        {
            //arrange

            //act
            var downloadArguments = new DownloadArguments(String.Empty, true, "any", "any", false, String.Empty, true, 10);
            var strategy          = Program.GetEffectiveDownloadStrategy(downloadArguments, false, false);

            //assert
            Assert.That(strategy, Is.TypeOf <TemporarilyIgnoreUpdatesDownloadStrategy>());
            var effectiveStrategy = ((TemporarilyIgnoreUpdatesDownloadStrategy)strategy).EffectiveStrategy;

            Assert.That(effectiveStrategy, Is.TypeOf <GitHubDownloadStrategy>());
            Assert.That(effectiveStrategy.FallbackStrategy, Is.TypeOf <NugetDownloadStrategy>());
            Assert.That(effectiveStrategy.FallbackStrategy.FallbackStrategy, Is.Null);
            Assert.That(strategy.FallbackStrategy, Is.Null);
        }
Beispiel #9
0
        public async Task CreateDownloader()
        {
            var encryption            = Config.Global.Get <EncryptionConfiguration>();
            var downloadConfiguration = Config.Global.Get <DownloadApplication>();
            var jsonConfigurator      = new JsonConfigurator();


            var destinationPath = Path.Combine("c:\\", "Temp", FolderName);
            await FileCopier.CopyFromOneFolderToAnother(downloadConfiguration.Location, destinationPath, true);

            jsonConfigurator.SaveConfiguration(ConfigurationKeys.Encryption, encryption, destinationPath);

            var downloadArguments = new DownloadArguments
            {
                DestinationPath   = "Extracted",
                EncryptedPassword = EncryptedPassword,
                FtpPath           = FtpPath,
                HostName          = HostName,
                UserName          = UserName
            };

            jsonConfigurator.SaveConfiguration(ConfigurationKeys.DownloadArguments, downloadArguments, destinationPath);
        }
Beispiel #10
0
        void DownloadWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            DownloadArguments args = (DownloadArguments)e.Argument;

            RemoteIndex remoteIndex = WebAPI.GetIndex();

            // determine files to delete
            AddonGroup addonGroup = args.AddonGroup;

            foreach (RemoteAddon webAddon in addonGroup.RemoteAddons)
            {
                Uuid webAddonUuid = webAddon.Uuid;
                if (!FileIndexer.Instance.addonUuidToLocalAddonMap.ContainsKey(webAddonUuid))
                {
                    continue;
                }

                RemoteAddon remAddon = remoteIndex.FilesIndex[webAddon.Uuid];

                List <LocalAddon> localAddons = FileIndexer.Instance.addonUuidToLocalAddonMap[webAddonUuid];
                foreach (LocalAddon localAddon in localAddons)
                {
                    List <FilePath> removals = new();
                    foreach (LocalFileIndex fileIndex in localAddon.Files.Values)
                    {
                        FilePath relativeFilepath = fileIndex.Relative_filepath;
                        if (!remAddon.Files.ContainsKey(relativeFilepath.Replace("\\", "/")))
                        {
                            FilePath filePath = fileIndex.Absolute_filepath;
                            Log.Debug("deleting " + filePath);
                            removals.Add(filePath);
                            File.Delete(@filePath.Value);
                        }
                    }
                    foreach (FilePath removal in removals)
                    {
                        localAddon.Files.Remove(removal);
                    }
                }
            }

            // determine files to download
            downloadManager = new DownloadManager(args.AddonGroup);
            downloadManager.StateChanged    += DownloadManager_StateChanged;
            downloadManager.ProgressChanged += DownloadManager_ProgressChanged;

            foreach (RemoteAddon webAddon in addonGroup.RemoteAddons)
            {
                RemoteAddon remoteAddon = FileIndexer.Instance.RemoteIndex.FilesIndex[webAddon.Uuid];
                Uuid        uuid        = webAddon.Uuid;
                string      name        = webAddon.Name;

                if (!remoteAddon.Uuid.Equals(uuid))
                {
                    throw new InvalidOperationException(string.Format("uuid {0} of local addon {1} does not match remote uuid {2} of addon {3}", uuid, name, remoteAddon.Uuid, remoteAddon.Name));
                }

                FilePath destinationFolder = args.DownloadDirectoryDict[webAddon];

                if (!FileIndexer.Instance.addonUuidToLocalAddonMap.ContainsKey(uuid))
                {
                    // download all
                    foreach (RemoteAddonFile remoteAddonFile in remoteAddon.Files.Values)
                    {
                        FilePath remoteFilePath      = remoteAddonFile.Path;
                        FilePath destinationFilePath = FilePath.Combine(destinationFolder, remoteFilePath.Replace("/", "\\"));
                        downloadManager.AddDownload(WebAPI.RepoUrl + "/" + remoteFilePath.OriginalValue, destinationFilePath, remoteAddonFile.Size);
                    }
                }
                else
                {
                    Dictionary <FilePath, LocalFileIndex> relativeFilePathToFileIndexMap = new();
                    List <LocalAddon> localAddons = FileIndexer.Instance.addonUuidToLocalAddonMap[uuid];
                    foreach (LocalAddon localAddon in localAddons)
                    {
                        foreach (LocalFileIndex fileIndex in localAddon.Files.Values)
                        {
                            relativeFilePathToFileIndexMap.Add(fileIndex.Relative_filepath, fileIndex);
                        }
                    }

                    // parts of the addon already exist, update existing and download missing
                    foreach (RemoteAddonFile remoteAddonFile in remoteAddon.Files.Values)
                    {
                        FilePath remoteFilePath = remoteAddonFile.Path.Replace("/", "\\");
                        string   remoteHash     = remoteAddonFile.Hash;

                        if (relativeFilePathToFileIndexMap.ContainsKey(remoteFilePath))
                        {
                            LocalFileIndex localFileIndex = relativeFilePathToFileIndexMap[remoteFilePath];
                            if (!remoteHash.Equals(localFileIndex.Hash))
                            {
                                FilePath destinationFilepath = FilePath.Combine(destinationFolder, remoteFilePath);
                                downloadManager.AddDownload(WebAPI.RepoUrl + "/" + remoteFilePath.OriginalValue, destinationFilepath, remoteAddonFile.Size);
                            }
                        }
                        else
                        {
                            FilePath destinationFilepath = FilePath.Combine(destinationFolder, remoteFilePath);
                            downloadManager.AddDownload(WebAPI.RepoUrl + "/" + remoteFilePath.OriginalValue, destinationFilepath, remoteAddonFile.Size);
                        }
                    }
                }
            }

            downloadManager.StartDownloads();
        }
Beispiel #11
0
        public void GetDownloadStrategy_NotCached_TemporarilyIgnored()
        {
            //arrange

            //act
            var downloadArguments = new DownloadArguments(String.Empty, true, "any", "any", false, String.Empty, true, 10);
            var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, false, false);

            //assert
            Assert.That(strategy, Is.TypeOf<TemporarilyIgnoreUpdatesDownloadStrategy>());
            var effectiveStrategy = ((TemporarilyIgnoreUpdatesDownloadStrategy)strategy).EffectiveStrategy;
            Assert.That(effectiveStrategy, Is.TypeOf<GitHubDownloadStrategy>());
            Assert.That(effectiveStrategy.FallbackStrategy, Is.TypeOf<NugetDownloadStrategy>());
            Assert.That(effectiveStrategy.FallbackStrategy.FallbackStrategy, Is.Null);
            Assert.That(strategy.FallbackStrategy, Is.Null);
        }
 public void Setup()
 {
     mockDownloadStrategy = new Mock<IDownloadStrategy>();
     dlArgs = new DownloadArguments { Target = "paket.exe" };
     mockFileProxy = new Mock<IFileProxy>();
 }