Example #1
0
 private RegistrationIndexPageItem ToRegistrationIndexPageItem(Package package) =>
 new RegistrationIndexPageItem
 {
     RegistrationLeafUrl = _url.GetRegistrationLeafUrl(package.Id, package.Version),
     PackageContentUrl   = _url.GetPackageDownloadUrl(package.Id, package.Version),
     PackageMetadata     = new BaGetPackageMetadata
     {
         PackageId   = package.Id,
         Version     = package.Version.ToFullString(),
         Authors     = string.Join(", ", package.Authors),
         Description = package.Description,
         Downloads   = package.Downloads,
         HasReadme   = package.HasReadme,
         IconUrl     = package.HasEmbeddedIcon
                 ? _url.GetPackageIconDownloadUrl(package.Id, package.Version)
                 : package.IconUrlString,
         Language                 = package.Language,
         LicenseUrl               = package.LicenseUrlString,
         Listed                   = package.Listed,
         MinClientVersion         = package.MinClientVersion,
         ReleaseNotes             = package.ReleaseNotes,
         PackageContentUrl        = _url.GetPackageDownloadUrl(package.Id, package.Version),
         PackageTypes             = package.PackageTypes.Select(t => t.Name).ToList(),
         ProjectUrl               = package.ProjectUrlString,
         RepositoryUrl            = package.RepositoryUrlString,
         RepositoryType           = package.RepositoryType,
         Published                = package.Published,
         RequireLicenseAcceptance = package.RequireLicenseAcceptance,
         Summary                  = package.Summary,
         Tags             = package.Tags,
         Title            = package.Title,
         DependencyGroups = ToDependencyGroups(package)
     },
 };
        public async Task <PackageInfoCollection> GetPackageInfo(
            string packageId,
            string version = default,
            CancellationToken cancellationToken = default)
        {
            var query  = packageId.ToLower();
            var result = await _context.Packages
                         .Include(x => x.Dependencies)
                         .Include(x => x.PackageTypes)
                         .Include(x => x.PackageDownloads)
                         .Where(x => x.Id.ToLower() == packageId.ToLower())
                         .ToListAsync();

            if (!result.Any())
            {
                return(new PackageInfoCollection());
            }

            var packageInfo = string.IsNullOrEmpty(version) ? new PackageInfoCollection() : new PackageInfoCollection(version);

            packageInfo.Versions = result
                                   .Select(x => new PackageInfo
            {
                PackageId    = x.Id,
                Authors      = x.Authors,
                Dependencies = x.Dependencies
                               .GroupBy(x => x.TargetFramework)
                               .ToDictionary(x => x.Key, x => x.Select(d => new PackageDependencyInfo
                {
                    PackageId         = d.Id,
                    VersionRange      = $"({VersionHelper.GetFormattedVersionConstraint(d.VersionRange)})",
                    IsLocalDependency = _context.Packages.Any(p => p.Id == d.Id)
                })),
                Description = x.Description,
#pragma warning disable CS0618
                Downloads = x.PackageDownloads.Count + x.Downloads,
#pragma warning restore CS06818
                HasReadme               = x.HasReadme,
                IsListed                = x.Listed,
                IsPrerelease            = x.IsPrerelease,
                IsDevelopmentDependency = x.IsDevelopmentDependency,
                IsTool         = x.IsTool,
                IsTemplate     = x.PackageTypes.Any(x => x.Name.Equals("Template", StringComparison.OrdinalIgnoreCase)),
                IsDeprecated   = false,   // TODO: BaGet will need to add support for deprecation
                IconUrl        = x.HasEmbeddedIcon ? _urlGenerator.GetPackageIconDownloadUrl(x.Id, x.Version) : x.IconUrlString,
                LicenseUrl     = x.LicenseUrlString,
                ProjectUrl     = x.ProjectUrlString,
                Published      = x.Published,
                ReleaseNotes   = x.ReleaseNotes,
                RepositoryType = x.RepositoryType,
                RepositoryUrl  = x.RepositoryUrlString,
                Summary        = x.Summary,
                Tags           = x.Tags,
                Version        = x.Version
            })
                                   .OrderByDescending(x => x.Version)
                                   .ToList();
            return(packageInfo);
        }
        public async Task <SearchResponse> SearchAsync(
            SearchRequest request,
            CancellationToken cancellationToken)
        {
            var result   = new List <SearchResult>();
            var packages = await SearchImplAsync(
                request,
                cancellationToken);

            foreach (var package in packages)
            {
                var versions = package.OrderByDescending(p => p.Version).ToList();
                var latest   = versions.First();
                var iconUrl  = latest.HasEmbeddedIcon
                    ? _url.GetPackageIconDownloadUrl(latest.Id, latest.Version)
                    : latest.IconUrlString;

                result.Add(new SearchResult
                {
                    PackageId            = latest.Id,
                    Version              = latest.Version.ToFullString(),
                    Description          = latest.Description,
                    Authors              = latest.Authors,
                    IconUrl              = iconUrl,
                    LicenseUrl           = latest.LicenseUrlString,
                    ProjectUrl           = latest.ProjectUrlString,
                    RegistrationIndexUrl = _url.GetRegistrationIndexUrl(latest.Id),
                    Summary              = latest.Summary,
                    Tags      = latest.Tags,
                    Title     = latest.Title,
                    Published = new DateTimeOffset(latest.Published, TimeZoneInfo.Local.GetUtcOffset(DateTime.UtcNow)),
#pragma warning disable CS0618
                    TotalDownloads = versions.Sum(p => p.Downloads + p.PackageDownloads.Count),
#pragma warning restore CS0618
                    Versions = versions
                               .Select(p => new SearchResultVersion
                    {
                        RegistrationLeafUrl = _url.GetRegistrationLeafUrl(p.Id, p.Version),
                        Version             = p.Version.ToFullString(),
#pragma warning disable CS0618
                        Downloads = p.PackageDownloads.Count + p.Downloads,
#pragma warning restore CS0618
                    })
                               .ToList()
                });
            }

            return(new SearchResponse
            {
                TotalHits = await SearchCountAsync(request, cancellationToken),
                Data = result,
                Context = SearchContext.Default(_url.GetPackageMetadataResourceUrl())
            });
        }
Example #4
0
        private SearchResult ToSearchResult(IReadOnlyList <PackageEntity> packages)
        {
            NuGetVersion  latestVersion  = null;
            PackageEntity latest         = null;
            var           versions       = new List <SearchResultVersion>();
            long          totalDownloads = 0;

            foreach (var package in packages)
            {
                var version = NuGetVersion.Parse(package.OriginalVersion);

                totalDownloads += package.Downloads;
                versions.Add(new SearchResultVersion
                {
                    RegistrationLeafUrl = _url.GetRegistrationLeafUrl(package.Id, version),
                    Version             = package.NormalizedVersion,
                    Downloads           = package.Downloads,
                });

                if (latestVersion == null || version > latestVersion)
                {
                    latest        = package;
                    latestVersion = version;
                }
            }

            var iconUrl = latest.HasEmbeddedIcon
                ? _url.GetPackageIconDownloadUrl(latest.Id, latestVersion)
                : latest.IconUrl;

            return(new SearchResult
            {
                PackageId = latest.Id,
                Version = latest.NormalizedVersion,
                Description = latest.Description,
                Authors = JsonConvert.DeserializeObject <string[]>(latest.Authors),
                IconUrl = iconUrl,
                LicenseUrl = latest.LicenseUrl,
                ProjectUrl = latest.ProjectUrl,
                RegistrationIndexUrl = _url.GetRegistrationIndexUrl(latest.Id),
                Summary = latest.Summary,
                Tags = JsonConvert.DeserializeObject <string[]>(latest.Tags),
                Title = latest.Title,
                TotalDownloads = totalDownloads,
                Versions = versions,
            });
        }
Example #5
0
        public SearchResponse BuildSearch(IReadOnlyList <PackageRegistration> packageRegistrations)
        {
            var result = new List <SearchResult>();

            foreach (var packageRegistration in packageRegistrations)
            {
                var versions = packageRegistration.Packages.OrderByDescending(p => p.Version).ToList();
                var latest   = versions.First();
                var iconUrl  = latest.HasEmbeddedIcon
                    ? _url.GetPackageIconDownloadUrl(latest.Id, latest.Version)
                    : latest.IconUrlString;

                result.Add(new SearchResult
                {
                    PackageId            = latest.Id,
                    Version              = latest.Version.ToFullString(),
                    Description          = latest.Description,
                    Authors              = latest.Authors,
                    IconUrl              = iconUrl,
                    LicenseUrl           = latest.LicenseUrlString,
                    ProjectUrl           = latest.ProjectUrlString,
                    RegistrationIndexUrl = _url.GetRegistrationIndexUrl(latest.Id),
                    Summary              = latest.Summary,
                    Tags           = latest.Tags,
                    Title          = latest.Title,
                    TotalDownloads = versions.Sum(p => p.Downloads),
                    Versions       = versions
                                     .Select(p => new SearchResultVersion
                    {
                        RegistrationLeafUrl = _url.GetRegistrationLeafUrl(p.Id, p.Version),
                        Version             = p.Version.ToFullString(),
                        Downloads           = p.Downloads,
                    })
                                     .ToList(),
                });
            }

            return(new SearchResponse
            {
                TotalHits = result.Count,
                Data = result,
                Context = SearchContext.Default(_url.GetPackageMetadataResourceUrl()),
            });
        }
Example #6
0
        public async Task <SearchResponse> SearchAsync(
            SearchRequest request,
            CancellationToken cancellationToken)
        {
            var searchText = BuildSeachQuery(request.Query, request.PackageType, request.Framework);
            var filter     = BuildSearchFilter(request.IncludePrerelease, request.IncludeSemVer2);
            var parameters = new SearchParameters
            {
                IncludeTotalResultCount = true,
                QueryType = QueryType.Full,
                Skip      = request.Skip,
                Top       = request.Take,
                Filter    = filter
            };

            var response = await _searchClient.Documents.SearchAsync <PackageDocument>(
                searchText,
                parameters,
                cancellationToken : cancellationToken);

            var results = new List <SearchResult>();

            foreach (var result in response.Results)
            {
                var document = result.Document;
                var versions = new List <SearchResultVersion>();

                if (document.Versions.Length != document.VersionDownloads.Length)
                {
                    throw new InvalidOperationException($"Invalid document {document.Key} with mismatched versions");
                }

                for (var i = 0; i < document.Versions.Length; i++)
                {
                    var version = NuGetVersion.Parse(document.Versions[i]);

                    versions.Add(new SearchResultVersion
                    {
                        RegistrationLeafUrl = _url.GetRegistrationLeafUrl(document.Id, version),
                        Version             = document.Versions[i],
                        Downloads           = long.Parse(document.VersionDownloads[i]),
                    });
                }

                var iconUrl = document.HasEmbeddedIcon
                    ? _url.GetPackageIconDownloadUrl(document.Id, NuGetVersion.Parse(document.Version))
                    : document.IconUrl;

                results.Add(new SearchResult
                {
                    PackageId            = document.Id,
                    Version              = document.Version,
                    Description          = document.Description,
                    Authors              = document.Authors,
                    IconUrl              = iconUrl,
                    LicenseUrl           = document.LicenseUrl,
                    ProjectUrl           = document.ProjectUrl,
                    RegistrationIndexUrl = _url.GetRegistrationIndexUrl(document.Id),
                    Summary              = document.Summary,
                    Tags           = document.Tags,
                    Title          = document.Title,
                    TotalDownloads = document.TotalDownloads,
                    Versions       = versions
                });
            }

            return(new SearchResponse
            {
                TotalHits = response.Count.Value,
                Data = results,
                Context = SearchContext.Default(_url.GetPackageMetadataResourceUrl())
            });
        }
Example #7
0
        public async Task <SearchResponse> SearchAsync(
            string query           = null,
            int skip               = 0,
            int take               = 20,
            bool includePrerelease = true,
            bool includeSemVer2    = true,
            string packageType     = null,
            string framework       = null,
            CancellationToken cancellationToken = default)
        {
            var result   = new List <SearchResult>();
            var packages = await SearchImplAsync(
                query,
                skip,
                take,
                includePrerelease,
                includeSemVer2,
                packageType,
                framework,
                cancellationToken);

            foreach (var package in packages)
            {
                var versions = package.OrderByDescending(p => p.Version).ToList();
                var latest   = versions.First();
                var iconUrl  = latest.HasEmbeddedIcon
                    ? _url.GetPackageIconDownloadUrl(latest.Id, latest.Version)
                    : latest.IconUrlString;

                result.Add(new SearchResult
                {
                    PackageId            = latest.Id,
                    Version              = latest.Version.ToFullString(),
                    Description          = latest.Description,
                    Authors              = latest.Authors,
                    IconUrl              = iconUrl,
                    LicenseUrl           = latest.LicenseUrlString,
                    ProjectUrl           = latest.ProjectUrlString,
                    RegistrationIndexUrl = _url.GetRegistrationIndexUrl(latest.Id),
                    Summary              = latest.Summary,
                    Tags           = latest.Tags,
                    Title          = latest.Title,
                    TotalDownloads = versions.Sum(p => p.Downloads),
                    Versions       = versions
                                     .Select(p => new SearchResultVersion
                    {
                        RegistrationLeafUrl = _url.GetRegistrationLeafUrl(p.Id, p.Version),
                        Version             = p.Version.ToFullString(),
                        Downloads           = p.Downloads,
                    })
                                     .ToList()
                });
            }

            return(new SearchResponse
            {
                TotalHits = result.Count,
                Data = result,
                Context = SearchContext.Default(_url.GetPackageMetadataResourceUrl())
            });
        }