Пример #1
0
            public async Task <Uri> GetPackageUriAsync(string id, string version, CancellationToken cancellationToken)
            {
                var serviceIndex = await _sourceRepository.GetResourceAsync <ServiceIndexResourceV3>(cancellationToken);

                var packageBaseAddress = serviceIndex.GetServiceEntryUri(ServiceTypes.PackageBaseAddress);

                if (packageBaseAddress != null)
                {
                    return(new Uri(packageBaseAddress, $"{id}/{version}/{id}.{version}.nupkg"));
                }
                else
                {
                    _logger.LogDebug("Upstream repository does not support flat container, falling back to registration");
                    // If there is no flat container resource fall back to using the registration resource to find the download url.
                    using (var sourceCacheContext = new SourceCacheContext())
                    {
                        // Read the url from the registration information
                        var pid  = new PackageIdentity(id, NuGetVersion.Parse(version));
                        var blob = await _regResource.GetPackageMetadata(pid, sourceCacheContext, _loggerAdapter, cancellationToken);

                        if (blob != null && blob["packageContent"] != null)
                        {
                            return(new Uri(blob["packageContent"].ToString()));
                        }
                        else
                        {
                            throw new InvalidOperationException("Could not determine upstream url for download");
                        }
                    }
                }
            }
        private async Task <IEnumerable <CatalogIndexEntry> > FetchCatalogIndexEntriesFromRegistrationAsync(
            FeedPackageIdentity feedPackage,
            CancellationToken token)
        {
            var id       = feedPackage.Id;
            var version  = NuGetVersion.Parse(feedPackage.Version);
            var leafBlob = await _regResource.GetPackageMetadata(
                new PackageIdentity(id, version),
                NullSourceCacheContext.Instance,
                Logger.AsCommon(),
                token);

            if (leafBlob == null)
            {
                throw new Exception("Package is missing from registration!");
            }

            var catalogPageUri = new Uri(leafBlob["@id"].ToString());
            var catalogPage    = await _client.GetJObjectAsync(catalogPageUri, token);

            return(new CatalogIndexEntry[]
            {
                new CatalogIndexEntry(
                    catalogPageUri,
                    Schema.DataTypes.PackageDetails.ToString(),
                    catalogPage["catalog:commitId"].ToString(),
                    DateTime.Parse(catalogPage["catalog:commitTimeStamp"].ToString()),
                    id,
                    version)
            });
        }
Пример #3
0
        public override async Task <IEnumerable <UIPackageMetadata> > GetMetadata(IEnumerable <PackageIdentity> packages, CancellationToken token)
        {
            List <UIPackageMetadata> results = new List <UIPackageMetadata>();

            // group by id to optimize
            foreach (var group in packages.GroupBy(e => e.Id, StringComparer.OrdinalIgnoreCase))
            {
                var versions = group.OrderBy(e => e.Version, VersionComparer.VersionRelease);

                // find the range of versions we need
                VersionRange range = new VersionRange(versions.First().Version, true, versions.Last().Version, true, true);

                IEnumerable <JObject> metadataList = await _regResource.GetPackageMetadata(group.Key, range, true, true, token);

                results.AddRange(metadataList.Select(item => ParseMetadata(item)));
            }

            return(results);
        }
Пример #4
0
        public override async Task <IEnumerable <NuGetVersion> > VersionStartsWith(string packageId, string versionPrefix, bool includePrerelease, CancellationToken token)
        {
            //*TODOs : Take prerelease as parameter. Also it should return both listed and unlisted for powershell ?
            IEnumerable <JObject> packages = await _regResource.GetPackageMetadata(packageId, includePrerelease, false, token);

            List <NuGetVersion> versions = new List <NuGetVersion>();

            foreach (var package in packages)
            {
                string version = (string)package["version"];
                if (version.StartsWith(versionPrefix, StringComparison.OrdinalIgnoreCase))
                {
                    versions.Add(new NuGetVersion(version));
                }
            }
            return(versions);
        }
Пример #5
0
        public static async Task <long> PackageSize(this SourceRepository sourceRepository, PackageIdentity packageIdentity, CancellationToken cancellationToken)
        {
            if (null == sourceRepository || null == packageIdentity)
            {
                return(0);
            }

            using (SourceCacheContext cache = new SourceCacheContext
            {
                DirectDownload = true,
                NoCache = true
            })
            {
                try
                {
                    RegistrationResourceV3 registrationResource = await sourceRepository.GetResourceAsync <RegistrationResourceV3>(cancellationToken).ConfigureAwait(false);

                    JObject packageMetadata = await registrationResource.GetPackageMetadata(packageIdentity, cache, NullLogger.Instance, cancellationToken).ConfigureAwait(false);

                    if (null == packageMetadata)
                    {
                        throw new PackageNotFoundProtocolException(packageIdentity);
                    }
                    string catalogItemUrl = packageMetadata.Value <string>("@id");

                    HttpSourceResource httpSourceResource = await sourceRepository.GetResourceAsync <HttpSourceResource>(cancellationToken).ConfigureAwait(false);

                    JObject catalogItem = await httpSourceResource.HttpSource.GetJObjectAsync(new HttpSourceRequest(catalogItemUrl, NullLogger.Instance), NullLogger.Instance, cancellationToken).ConfigureAwait(false);

                    return(catalogItem.Value <long>("packageSize"));
                }
                catch (Exception ex) when(ex is FatalProtocolException || ex is HttpRequestException)
                {
                    return(0);
                }
            }
        }
Пример #6
0
 private Task <JObject> GetPackageFromIndexAsync(PackageIdentity package, ILogger log, CancellationToken token)
 {
     // If the registration index is missing, this will return null.
     return(_registration.GetPackageMetadata(package, NullSourceCacheContext.Instance, log, token));
 }