private async Task <IReadOnlyList <Package> > FindPackagesOrNullAsync(
            string packageId,
            CancellationToken cancellationToken)
        {
            var upstreamPackages = await _mirror.FindPackagesOrNullAsync(packageId, cancellationToken);

            var localPackages = await _packages.FindAsync(packageId, includeUnlisted : true, cancellationToken);

            if (upstreamPackages == null)
            {
                return(localPackages.Any()
                    ? localPackages
                    : null);
            }

            // Mrge the local packages into the upstream packages.
            var result = upstreamPackages.ToDictionary(p => new PackageIdentity(p.Id, p.Version));
            var local  = localPackages.ToDictionary(p => new PackageIdentity(p.Id, p.Version));

            foreach (var localPackage in local)
            {
                result[localPackage.Key] = localPackage.Value;
            }

            return(result.Values.ToList());
        }
Пример #2
0
        public async Task <IActionResult> Get(string id, CancellationToken cancellationToken)
        {
            // Find the packages that match the given package id from the upstream, if
            // one is configured. If these packages cannot be found on the upstream,
            // we'll return the local packages instead.
            var packages = await _mirror.FindPackagesOrNullAsync(id, cancellationToken);

            if (packages == null)
            {
                packages = await _packages.FindAsync(id, includeUnlisted : true);
            }

            if (!packages.Any())
            {
                return(NotFound());
            }

            var versions = packages.Select(p => p.Version).ToList();

            // TODO: Paging of registration items.
            // "Un-paged" example: https://api.nuget.org/v3/registration3/newtonsoft.json/index.json
            // Paged example: https://api.nuget.org/v3/registration3/fake/index.json
            return(Json(new RegistrationIndex(
                            count: packages.Count,
                            totalDownloads: packages.Sum(p => p.Downloads),
                            pages: new[]
            {
                new RegistrationIndexPage(
                    Url.PackageRegistration(packages.First().Id),
                    count: packages.Count(),
                    itemsOrNull: packages.Select(ToRegistrationIndexPageItem).ToList(),
                    lower: versions.Min(),
                    upper: versions.Max())
            })));
        }
Пример #3
0
        public async Task <BaGetRegistrationIndexResponse> GetRegistrationIndexOrNullAsync(
            string id,
            CancellationToken cancellationToken = default)
        {
            // Find the packages that match the given package id from the upstream, if
            // one is configured. If these packages cannot be found on the upstream,
            // we'll return the local packages instead.
            var packages = await _mirror.FindPackagesOrNullAsync(id, cancellationToken);

            if (packages == null)
            {
                packages = await _packages.FindAsync(id, includeUnlisted : true);
            }

            if (!packages.Any())
            {
                return(null);
            }

            var versions = packages.Select(p => p.Version).ToList();

            // TODO: Paging of registration items.
            // "Un-paged" example: https://api.nuget.org/v3/registration3/newtonsoft.json/index.json
            // Paged example: https://api.nuget.org/v3/registration3/fake/index.json
            return(new BaGetRegistrationIndexResponse
            {
                RegistrationIndexUrl = _url.GetRegistrationIndexUrl(id),
                Type = RegistrationIndexResponse.DefaultType,
                Count = 1,
                TotalDownloads = packages.Sum(p => p.Downloads),
                Pages = new[]
                {
                    new RegistrationIndexPage
                    {
                        RegistrationPageUrl = _url.GetRegistrationIndexUrl(packages.First().Id),
                        Count = packages.Count(),
                        Lower = versions.Min().ToNormalizedString().ToLowerInvariant(),
                        Upper = versions.Max().ToNormalizedString().ToLowerInvariant(),
                        ItemsOrNull = packages.Select(ToRegistrationIndexPageItem).ToList(),
                    }
                }
            });
        }