예제 #1
0
        public async Task <bool> DoesPackageExistAsync(string id, NuGetVersion version)
        {
            var indexUrl = RegistrationUrlBuilder.GetIndexUrl(_endpoint, id);
            var index    = await _registrationClient.GetIndexOrNullAsync(indexUrl);

            if (index == null)
            {
                return(false);
            }

            var matchingPageItem = index
                                   .Items
                                   .FirstOrDefault(x => NuGetVersion.Parse(x.Lower) <= version && version <= NuGetVersion.Parse(x.Upper));

            if (matchingPageItem == null)
            {
                return(false);
            }

            var items = matchingPageItem.Items;

            if (items == null)
            {
                try
                {
                    var page = await _registrationClient.GetPageAsync(matchingPageItem.Url);

                    items = page.Items;
                }
                catch (SimpleHttpClientException ex) when(ex.StatusCode == HttpStatusCode.NotFound)
                {
                    return(false);
                }
            }

            var matchingItem = items
                               .FirstOrDefault(x => NuGetVersion.Parse(x.CatalogEntry.Version) == version);

            if (matchingItem == null)
            {
                return(false);
            }

            return(true);
        }
예제 #2
0
        public async Task CopyAsync(ICloudBlobContainer container, string oldBaseUrl, string newBaseUrl, string id, bool gzip)
        {
            var normalizedOldBaseUrl = oldBaseUrl.TrimEnd('/');
            var normalizedNewBaseUrl = newBaseUrl.TrimEnd('/');

            var indexUrl = RegistrationUrlBuilder.GetIndexUrl(normalizedOldBaseUrl, id);

            _logger.LogInformation("Downloading index {IndexUrl}...", indexUrl);

            await Throttle.WaitAsync();

            RegistrationIndex index;

            try
            {
                index = await _registrationClient.GetIndexOrNullAsync(indexUrl);
            }
            finally
            {
                Throttle.Release();
            }

            if (index == null)
            {
                return;
            }

            var pageUrls = new List <string>();
            var itemUrls = new List <string>();

            var pageTasks = index
                            .Items
                            .Select(async pageItem =>
            {
                await Task.Yield();
                await Throttle.WaitAsync();
                try
                {
                    List <RegistrationLeafItem> leafItems;
                    if (pageItem.Items != null)
                    {
                        leafItems = pageItem.Items;
                    }
                    else
                    {
                        pageUrls.Add(pageItem.Url);
                        _logger.LogInformation("Downloading page {PageUrl}...", pageItem.Url);
                        var page  = await _registrationClient.GetPageAsync(pageItem.Url);
                        leafItems = page.Items;
                    }

                    foreach (var leafItem in leafItems)
                    {
                        itemUrls.Add(leafItem.Url);
                    }
                }
                finally
                {
                    Throttle.Release();
                }
            })
                            .ToList();
            await Task.WhenAll(pageTasks);

            var copyTasks = itemUrls
                            .Concat(pageUrls)
                            .Concat(new[] { indexUrl })
                            .Select(async url =>
            {
                await Task.Yield();
                await CopyUrlAsync(container, normalizedOldBaseUrl, normalizedNewBaseUrl, url, gzip);
            })
                            .ToList();
            await Task.WhenAll(copyTasks);
        }