Пример #1
0
        public async Task <Stream> GetPackageContentStreamOrNullAsync(
            string id,
            NuGetVersion version,
            CancellationToken cancellationToken = default)
        {
            // Allow read-through caching if it is configured.
            await _mirror.MirrorAsync(id, version, cancellationToken);

            if (!await _packages.AddDownloadAsync(id, version, cancellationToken))
            {
                return(null);
            }

            return(await _storage.GetPackageStreamAsync(id, version, cancellationToken));
        }
Пример #2
0
        public async Task <IActionResult> Get(string id, string version, CancellationToken cancellationToken)
        {
            if (!NuGetVersion.TryParse(version, out var nugetVersion))
            {
                return(NotFound());
            }

            // Allow read-through caching to happen if it is configured.
            await _mirror.MirrorAsync(id, nugetVersion, cancellationToken);

            var package = await _packages.FindOrNullAsync(id, nugetVersion, includeUnlisted : true);

            if (package == null)
            {
                return(NotFound());
            }

            var result = new RegistrationLeaf(
                type: RegistrationLeaf.DefaultType,
                registrationUri: Url.PackageRegistration(id, nugetVersion),
                listed: package.Listed,
                downloads: package.Downloads,
                packageContentUrl: Url.PackageDownload(id, nugetVersion),
                published: package.Published,
                registrationIndexUrl: Url.PackageRegistration(id));

            return(Json(result));
        }
Пример #3
0
        public async Task <IActionResult> Get(string id, CancellationToken cancellationToken)
        {
            // Allow read-through caching to happen if it is configured.
            await _mirror.MirrorAsync(id, cancellationToken);

            var packages = await _packages.FindAsync(id);

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

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

            // 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())
            })));
        }
        public async Task <RegistrationLeafResponse> GetRegistrationLeafOrNullAsync(
            string id,
            NuGetVersion version,
            CancellationToken cancellationToken = default)
        {
            // Allow read-through caching to happen if it is configured.
            await _mirror.MirrorAsync(id, version, cancellationToken);

            var package = await _packages.FindOrNullAsync(id, version, includeUnlisted : true, cancellationToken);

            if (package == null)
            {
                return(null);
            }

            return(new BaGetRegistrationLeafResponse
            {
                Type = RegistrationLeafResponse.DefaultType,
                Listed = package.Listed,
                Downloads = package.Downloads,
                Published = package.Published,
                RegistrationLeafUrl = _url.GetRegistrationLeafUrl(id, version),
                PackageContentUrl = _url.GetPackageDownloadUrl(id, version),
                RegistrationIndexUrl = _url.GetRegistrationIndexUrl(id)
            });
        }
Пример #5
0
        public async Task <IActionResult> DownloadPackage(string id, string version, CancellationToken cancellationToken)
        {
            if (!NuGetVersion.TryParse(version, out var nugetVersion))
            {
                return(NotFound());
            }

            // Allow read-through caching if it is configured.
            await _mirror.MirrorAsync(id, nugetVersion, cancellationToken);

            if (!await _packages.AddDownloadAsync(id, nugetVersion))
            {
                return(NotFound());
            }

            var packageStream = await _storage.GetPackageStreamAsync(id, nugetVersion, cancellationToken);

            return(File(packageStream, "application/octet-stream"));
        }
        public async Task <BaGetRegistrationLeafResponse> GetRegistrationLeafOrNullAsync(
            string id,
            NuGetVersion version,
            CancellationToken cancellationToken = default)
        {
            // Allow read-through caching to happen if it is configured.
            await _mirror.MirrorAsync(id, version, cancellationToken);

            var package = await _packages.FindOrNullAsync(id, version, includeUnlisted : true, cancellationToken);

            if (package == null)
            {
                return(null);
            }

            return(_builder.BuildLeaf(package));
        }
Пример #7
0
        public CacheRegistrationIndexModule(IMirrorService mirror)
        {
            this._mirror = mirror ?? throw new ArgumentNullException(nameof(mirror));
            this.Get("cache/v3/registration/{id}/index.json", async(req, res, routeData) =>
            {
                string id = routeData.As <string>("id");
                // Documentation: https://docs.microsoft.com/en-us/nuget/api/registration-base-url-resource
                var upstreamPackages = (await _mirror.FindUpstreamMetadataAsync(id, CancellationToken.None)).ToList();
                var versions         = upstreamPackages.Select(p => p.Identity.Version).ToList();

                if (!upstreamPackages.Any())
                {
                    res.StatusCode = 404;
                    return;
                }

                // 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
                await res.AsJson(new
                {
                    Count          = upstreamPackages.Count,
                    TotalDownloads = upstreamPackages.Sum(p => p.DownloadCount),
                    Items          = new[]
                    {
                        new RegistrationIndexItem(
                            packageId: id,
                            items: upstreamPackages.Select(p => ToRegistrationIndexLeaf(req, p)).ToList(),
                            lower: versions.Min().ToNormalizedString(),
                            upper: versions.Max().ToNormalizedString()
                            ),
                    }
                });
            });

            this.Get("cache/v3/registration/{id}/{version}.json", async(req, res, routeData) =>
            {
                string id      = routeData.As <string>("id");
                string version = routeData.As <string>("version");

                if (!NuGetVersion.TryParse(version, out var nugetVersion))
                {
                    res.StatusCode = 400;
                    return;
                }

                // Allow read-through caching to happen if it is confiured.
                await _mirror.MirrorAsync(id, nugetVersion, CancellationToken.None);

                var package = await _mirror.FindAsync(new PackageIdentity(id, nugetVersion));

                if (package == null)
                {
                    res.StatusCode = 404;
                    return;
                }

                // Documentation: https://docs.microsoft.com/en-us/nuget/api/registration-base-url-resource
                var result = new RegistrationLeaf(
                    registrationUri: req.PackageRegistration(id, nugetVersion, "cache"),
                    listed: package.IsListed,
                    downloads: package.DownloadCount.GetValueOrDefault(),
                    packageContentUri: req.PackageDownload(id, nugetVersion, "cache"),
                    published: package.Published.GetValueOrDefault(),
                    registrationIndexUri: req.PackageRegistration(id, "cache"));

                await res.AsJson(result);
            });
        }
Пример #8
0
        public CachePackageModule(IMirrorService mirror)
            : base("cache/v3/package")
        {
            _mirror = mirror ?? throw new ArgumentNullException(nameof(mirror));

            this.Get("/{id}/index.json", async(req, res, routeData) => {
                string id = routeData.As <string>("id");
                IReadOnlyList <string> upstreamVersions = await _mirror.FindUpstreamAsync(id, CancellationToken.None);
                if (upstreamVersions.Any())
                {
                    await res.AsJson(new
                    {
                        Versions = upstreamVersions.ToList()
                    });
                    return;
                }
                res.StatusCode = 404;
            });

            this.Get("/{id}/{version}/{idVersion}.nupkg", async(req, res, routeData) => {
                string id      = routeData.As <string>("id");
                string version = routeData.As <string>("version");

                if (!NuGetVersion.TryParse(version, out var nugetVersion))
                {
                    res.StatusCode = 400;
                    return;
                }

                // Allow read-through caching if it is configured.
                await _mirror.MirrorAsync(id, nugetVersion, CancellationToken.None);

                var identity      = new PackageIdentity(id, nugetVersion);
                var packageStream = await _mirror.GetPackageStreamAsync(identity);

                await res.FromStream(packageStream, "application/octet-stream");
            });

            this.Get("/{id}/{version}/{id2}.nuspec", async(req, res, routeData) => {
                string id      = routeData.As <string>("id");
                string version = routeData.As <string>("version");

                if (!NuGetVersion.TryParse(version, out var nugetVersion))
                {
                    res.StatusCode = 400;
                    return;
                }

                // Allow read-through caching if it is configured.
                await _mirror.MirrorAsync(id, nugetVersion, CancellationToken.None);

                if (!await _mirror.ExistsAsync(new PackageIdentity(id, nugetVersion)))
                {
                    res.StatusCode = 404;
                    return;
                }

                var identity = new PackageIdentity(id, nugetVersion);

                await res.FromStream(await _mirror.GetNuspecStreamAsync(identity), "text/xml");
            });

            this.Get("/{id}/{version}/readme", async(req, res, routeData) => {
                string id      = routeData.As <string>("id");
                string version = routeData.As <string>("version");

                if (!NuGetVersion.TryParse(version, out var nugetVersion))
                {
                    res.StatusCode = 400;
                    return;
                }

                // Allow read-through caching if it is configured.
                await _mirror.MirrorAsync(id, nugetVersion, CancellationToken.None);

                if (!await _mirror.ExistsAsync(new PackageIdentity(id, nugetVersion)))
                {
                    res.StatusCode = 404;
                    return;
                }

                var identity = new PackageIdentity(id, nugetVersion);

                await res.FromStream(await _mirror.GetReadmeStreamAsync(identity), "text/markdown");
            });
        }