-
Notifications
You must be signed in to change notification settings - Fork 642
/
PackageExtensions.cs
112 lines (106 loc) · 5.37 KB
/
PackageExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Data.Entity;
using System.Linq;
using OData.Linq;
using QueryInterceptor;
namespace NuGetGallery
{
public static class PackageExtensions
{
private static readonly DateTime UnpublishedDate = new DateTime(1900, 1, 1, 0, 0, 0);
public static IQueryable<V1FeedPackage> ToV1FeedPackageQuery(this IQueryable<Package> packages, string siteRoot)
{
siteRoot = EnsureTrailingSlash(siteRoot);
return packages
.Include(p => p.PackageRegistration)
.WithoutNullPropagation()
.Select(
p => new V1FeedPackage
{
Id = p.PackageRegistration.Id,
Version = p.Version,
Authors = p.FlattenedAuthors,
Copyright = p.Copyright,
Created = p.Created,
Dependencies = p.FlattenedDependencies,
Description = p.Description,
DownloadCount = p.PackageRegistration.DownloadCount,
ExternalPackageUrl = null,
GalleryDetailsUrl = siteRoot + "packages/" + p.PackageRegistration.Id + "/" + p.Version,
IconUrl = p.IconUrl,
IsLatestVersion = p.IsLatestStable,
Language = p.Language,
LastUpdated = p.LastUpdated,
LicenseUrl = p.LicenseUrl,
PackageHash = p.Hash,
PackageHashAlgorithm = p.HashAlgorithm,
PackageSize = p.PackageFileSize,
ProjectUrl = p.ProjectUrl,
Published = p.Listed ? p.Published : UnpublishedDate,
ReleaseNotes = p.ReleaseNotes,
ReportAbuseUrl = siteRoot + "package/ReportAbuse/" + p.PackageRegistration.Id + "/" + p.Version,
RequireLicenseAcceptance = p.RequiresLicenseAcceptance,
Summary = p.Summary,
Tags = p.Tags == null ? null : " " + p.Tags.Trim() + " ",
// In the current feed, tags are padded with a single leading and trailing space
Title = p.Title ?? p.PackageRegistration.Id, // Need to do this since the older feed always showed a title.
VersionDownloadCount = p.DownloadCount,
Rating = 0
});
}
public static IQueryable<V2FeedPackage> ToV2FeedPackageQuery(this IQueryable<Package> packages, string siteRoot)
{
siteRoot = EnsureTrailingSlash(siteRoot);
return packages
.Include(p => p.PackageRegistration)
.WithoutNullPropagation()
// Duplicate of the code above, because EF tries to translate a call to ToV2FeedPackage into a Database operation and fails.
.Select(p => new V2FeedPackage
{
Id = p.PackageRegistration.Id,
Version = p.Version,
Authors = p.FlattenedAuthors,
Copyright = p.Copyright,
Created = p.Created,
Dependencies = p.FlattenedDependencies,
Description = p.Description,
DownloadCount = p.PackageRegistration.DownloadCount,
GalleryDetailsUrl = siteRoot + "packages/" + p.PackageRegistration.Id + "/" + p.Version,
IconUrl = p.IconUrl,
IsLatestVersion = p.IsLatestStable,
// To maintain parity with v1 behavior of the feed, IsLatestVersion would only be used for stable versions.
IsAbsoluteLatestVersion = p.IsLatest,
IsPrerelease = p.IsPrerelease,
LastUpdated = p.LastUpdated,
LicenseUrl = p.LicenseUrl,
Language = p.Language,
PackageHash = p.Hash,
PackageHashAlgorithm = p.HashAlgorithm,
PackageSize = p.PackageFileSize,
ProjectUrl = p.ProjectUrl,
ReleaseNotes = p.ReleaseNotes,
ReportAbuseUrl = siteRoot + "package/ReportAbuse/" + p.PackageRegistration.Id + "/" + p.Version,
RequireLicenseAcceptance = p.RequiresLicenseAcceptance,
Published = p.Listed ? p.Published : UnpublishedDate,
Summary = p.Summary,
Tags = p.Tags,
Title = p.Title,
VersionDownloadCount = p.DownloadCount,
MinClientVersion = p.MinClientVersion,
LastEdited = p.LastEdited,
});
}
internal static IQueryable<TVal> WithoutVersionSort<TVal>(this IQueryable<TVal> feedQuery)
{
return feedQuery.InterceptWith(new ODataRemoveVersionSorter());
}
private static string EnsureTrailingSlash(string siteRoot)
{
if (!siteRoot.EndsWith("/", StringComparison.Ordinal))
{
siteRoot = siteRoot + '/';
}
return siteRoot;
}
}
}