isn/src/nuget-host/Controllers/PackagesController.cs

99 lines
3.3 KiB
C#
Raw Normal View History

2021-04-08 02:03:17 +01:00
using System.IO;
using System.Linq;
2021-04-08 03:08:20 +01:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.DataProtection;
2021-04-08 02:03:17 +01:00
using Microsoft.AspNetCore.Mvc;
2021-06-21 23:38:05 +01:00
using Microsoft.EntityFrameworkCore;
2021-04-08 02:03:17 +01:00
using Microsoft.Extensions.Logging;
2021-05-02 15:22:46 +01:00
using Microsoft.Extensions.Options;
2021-05-09 03:06:23 +01:00
using nuget_host.Data;
2021-05-02 15:22:46 +01:00
using nuget_host.Entities;
2021-04-08 02:03:17 +01:00
namespace nuget_host.Controllers
{
2021-05-09 03:06:23 +01:00
[AllowAnonymous]
2021-06-21 23:38:05 +01:00
public partial class PackagesController : Controller
2021-04-08 02:03:17 +01:00
{
2021-04-25 12:12:50 +01:00
private readonly ILogger<PackagesController> logger;
private readonly IDataProtector protector;
2021-04-08 02:03:17 +01:00
2021-05-02 15:22:46 +01:00
private readonly NugetSettings nugetSettings;
2021-05-09 03:06:23 +01:00
ApplicationDbContext dbContext;
2021-05-02 15:22:46 +01:00
public PackagesController(
ILoggerFactory loggerFactory,
IDataProtectionProvider provider,
2021-05-09 03:06:23 +01:00
IOptions<NugetSettings> nugetOptions,
ApplicationDbContext dbContext)
2021-04-08 02:03:17 +01:00
{
logger = loggerFactory.CreateLogger<PackagesController>();
2021-05-02 15:22:46 +01:00
nugetSettings = nugetOptions.Value;
protector = provider.CreateProtector(nugetSettings.ProtectionTitle);
2021-05-09 03:06:23 +01:00
this.dbContext = dbContext;
2021-04-08 02:03:17 +01:00
}
2021-05-23 20:21:46 +01:00
// dotnet add . package -s http://localhost:5000/packages nuget-cli
// packages/FindPackagesById()?id='nuget-cli'&semVerLevel=2.0.0
2021-06-21 23:38:05 +01:00
// Search
// GET {@id}?q={QUERY}&skip={SKIP}&take={TAKE}&prerelease={PRERELEASE}&semVerLevel={SEMVERLEVEL}&packageType={PACKAGETYPE}
[HttpGet("~/search/index.json")]
public IActionResult Index(
string q,
bool prerelease = false,
string packageType = null,
int skip = 0,
int take = 25,
string semVerLevel = "2.0.0")
2021-04-08 02:03:17 +01:00
{
2021-06-21 23:38:05 +01:00
if (string.IsNullOrEmpty(q))
2021-04-08 02:03:17 +01:00
{
2021-06-21 23:38:05 +01:00
ModelState.AddModelError("q", "no value");
return NotFound(ModelState);
2021-04-08 02:03:17 +01:00
}
else
{
2021-06-21 23:38:05 +01:00
var scope = dbContext.Packages
.Include(p => p.Versions)
.Where(
p => (CamelCaseMatch(p.Id, q) || SeparatedByMinusMatch(p.Id, q))
&& (prerelease || p.Versions.Any(v => !v.IsPrerelease))
&& (packageType == null || p.Versions.Any(v => v.Type == packageType))
);
var result = new {
totalHits = scope.Count(),
data = scope.Skip(skip).Take(take).ToArray()
};
return Ok(result);
2021-04-08 02:03:17 +01:00
}
}
2021-04-08 03:08:20 +01:00
2021-06-21 23:38:05 +01:00
protected static bool CamelCaseMatch(string id, string q)
{
// Assert.False (q==null);
string query = q;
if (query.Length==0) return false;
2021-05-02 15:22:46 +01:00
2021-06-21 23:38:05 +01:00
while (id.Length > 0)
{
int i = 0;
while (id.Length > i && char.IsLower(id[i])) i++;
if (i==0) break;
id = id.Substring(i);
if (id.StartsWith(q, System.StringComparison.OrdinalIgnoreCase)) return true;
}
return false;
}
protected static bool SeparatedByMinusMatch(string id, string q)
2021-04-08 03:08:20 +01:00
{
2021-06-21 23:38:05 +01:00
foreach (var part in id.Split('-'))
{
if (part.StartsWith(q, System.StringComparison.OrdinalIgnoreCase)) return true;
}
return false;
2021-04-08 03:08:20 +01:00
}
2021-04-08 02:03:17 +01:00
}
}