2021-06-22 23:03:38 +01:00
|
|
|
using System.ComponentModel.DataAnnotations;
|
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-06-22 01:25:28 +01:00
|
|
|
using NuGet.Versioning;
|
2021-07-05 12:55:52 +01:00
|
|
|
using isn.Data;
|
|
|
|
|
using isn.Entities;
|
|
|
|
|
using Unleash.ClientFactory;
|
|
|
|
|
using Unleash;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using isnd.Services;
|
2021-04-08 02:03:17 +01:00
|
|
|
|
2021-07-05 12:55:52 +01:00
|
|
|
namespace isn.Controllers
|
2021-04-08 02:03:17 +01:00
|
|
|
{
|
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-07-05 12:55:52 +01:00
|
|
|
const int maxTake = 100;
|
|
|
|
|
|
|
|
|
|
const string _pkgRootPrefix = "~/package";
|
|
|
|
|
const string defaultSemVer = "2.0.0";
|
|
|
|
|
private readonly Resource[] ressources;
|
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-07-05 12:55:52 +01:00
|
|
|
private PackageManager packageManager;
|
2021-05-02 15:22:46 +01:00
|
|
|
|
|
|
|
|
public PackagesController(
|
2021-07-05 12:55:52 +01:00
|
|
|
PackageManager packageManager,
|
2021-05-02 15:22:46 +01:00
|
|
|
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-07-05 12:55:52 +01:00
|
|
|
this.packageManager = packageManager;
|
|
|
|
|
|
|
|
|
|
ressources = packageManager.GetResources(Startup.UnleashĈlient).ToArray();
|
2021-04-08 02:03:17 +01:00
|
|
|
}
|
|
|
|
|
|
2021-07-05 12:55:52 +01:00
|
|
|
// dotnet add . package -s http://localhost:5000/packages isn
|
|
|
|
|
// packages/FindPackagesById()?id='isn'&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}
|
|
|
|
|
|
2021-06-22 23:03:38 +01:00
|
|
|
|
|
|
|
|
[HttpGet("~/index.json")]
|
|
|
|
|
public IActionResult ApiIndex()
|
|
|
|
|
{
|
|
|
|
|
return Ok(ressources);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet(_pkgRootPrefix + "/index.json")]
|
2021-06-21 23:38:05 +01:00
|
|
|
public IActionResult Index(
|
|
|
|
|
string q,
|
2021-06-22 00:10:01 +01:00
|
|
|
string semVerLevel = defaultSemVer,
|
2021-06-21 23:38:05 +01:00
|
|
|
bool prerelease = false,
|
|
|
|
|
string packageType = null,
|
|
|
|
|
int skip = 0,
|
2021-06-22 00:10:01 +01:00
|
|
|
int take = 25)
|
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");
|
2021-04-08 02:03:17 +01:00
|
|
|
}
|
2021-06-22 01:25:28 +01:00
|
|
|
if (take > maxTake)
|
2021-04-08 02:03:17 +01:00
|
|
|
{
|
2021-06-22 01:25:28 +01:00
|
|
|
ModelState.AddModelError("take", "Maximum exceeded");
|
|
|
|
|
}
|
|
|
|
|
if (ModelState.IsValid)
|
|
|
|
|
{
|
2021-07-05 12:55:52 +01:00
|
|
|
return Ok(packageManager.SearchByName(q,skip,take,prerelease,packageType));
|
2021-04-08 02:03:17 +01:00
|
|
|
}
|
2021-06-22 01:25:28 +01:00
|
|
|
return BadRequest(new { error = ModelState });
|
2021-04-08 02:03:17 +01:00
|
|
|
}
|
2021-04-08 03:08:20 +01:00
|
|
|
|
2021-06-22 00:10:01 +01:00
|
|
|
// GET /autocomplete?id=nuget.protocol&prerelease=true
|
2021-06-22 23:03:38 +01:00
|
|
|
[HttpGet(_pkgRootPrefix + "/autocomplete")]
|
2021-06-22 00:10:01 +01:00
|
|
|
public IActionResult AutoComplete(
|
|
|
|
|
string id,
|
|
|
|
|
string semVerLevel = defaultSemVer,
|
|
|
|
|
bool prerelease = false,
|
|
|
|
|
string packageType = null,
|
|
|
|
|
int skip = 0,
|
|
|
|
|
int take = 25)
|
|
|
|
|
{
|
2021-06-22 01:25:28 +01:00
|
|
|
if (take > maxTake)
|
|
|
|
|
{
|
|
|
|
|
ModelState.AddModelError("take", "Maximum exceeded");
|
|
|
|
|
return BadRequest(ModelState);
|
|
|
|
|
}
|
2021-07-05 12:55:52 +01:00
|
|
|
|
|
|
|
|
return Ok(packageManager.AutoComplete(id,skip,take,prerelease,packageType));
|
2021-06-22 01:25:28 +01:00
|
|
|
}
|
|
|
|
|
// TODO GET {@id}/{LOWER_ID}/index.json
|
|
|
|
|
// LOWER_ID URL string yes The package ID, lowercased
|
|
|
|
|
// response : versions array of strings yes The versions available
|
2021-06-22 23:03:38 +01:00
|
|
|
[HttpGet(_pkgRootPrefix + "/{id}/{lower}/index.json")]
|
2021-06-22 01:25:28 +01:00
|
|
|
public IActionResult GetVersions(
|
|
|
|
|
string id,
|
|
|
|
|
string lower,
|
|
|
|
|
bool prerelease = false,
|
|
|
|
|
string packageType = null,
|
|
|
|
|
int skip = 0,
|
|
|
|
|
int take = 25)
|
|
|
|
|
{
|
|
|
|
|
if (take > maxTake)
|
|
|
|
|
{
|
|
|
|
|
ModelState.AddModelError("take", "Maximum exceeded");
|
|
|
|
|
}
|
|
|
|
|
// NugetVersion
|
|
|
|
|
if (!NuGetVersion.TryParse(lower, out NuGetVersion parsedVersion))
|
|
|
|
|
{
|
|
|
|
|
ModelState.AddModelError("lower", "invalid version string");
|
|
|
|
|
}
|
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest(ModelState);
|
|
|
|
|
}
|
2021-06-22 00:10:01 +01:00
|
|
|
return Ok(new
|
|
|
|
|
{
|
2021-07-05 12:55:52 +01:00
|
|
|
versions = packageManager.GetVersions(
|
|
|
|
|
id, parsedVersion, prerelease, packageType, skip, take)
|
2021-06-22 00:10:01 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-22 01:25:28 +01:00
|
|
|
// TODO GET GET {@id}/{LOWER_ID}/{LOWER_VERSION}/{LOWER_ID}.{LOWER_VERSION}.nupkg
|
|
|
|
|
// LOWER_ID URL string yes The package ID, lowercase
|
|
|
|
|
// LOWER_VERSION URL string yes The package version, normalized and lowercased
|
|
|
|
|
// response 200 : the package
|
2021-06-22 23:03:38 +01:00
|
|
|
[HttpGet(_pkgRootPrefix + "/{id}/{lower}/{idf}.{lowerf}.nupkg")]
|
|
|
|
|
public IActionResult GetPackage(
|
|
|
|
|
[FromRoute] string id, [FromRoute] string lower,
|
|
|
|
|
[FromRoute] string idf, [FromRoute] string lowerf)
|
|
|
|
|
{
|
|
|
|
|
var pkgpath = Path.Combine(nugetSettings.PackagesRootDir,
|
|
|
|
|
id, lower, $"{idf}.{lowerf}.nupkg"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
FileInfo pkgfi = new FileInfo(pkgpath);
|
|
|
|
|
return File(pkgfi.OpenRead(), "application/zip; charset=binary");
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-22 01:25:28 +01:00
|
|
|
// TODO GET {@id}/{LOWER_ID}/{LOWER_VERSION}/{LOWER_ID}.nuspec
|
|
|
|
|
// response 200 : the nuspec
|
2021-06-22 23:03:38 +01:00
|
|
|
[HttpGet(_pkgRootPrefix + "/{id}/{lower}/{idf}.{lowerf}.nuspec")]
|
|
|
|
|
public IActionResult GetNuspec(
|
|
|
|
|
[FromRoute][SafeName][Required] string id,
|
|
|
|
|
[FromRoute][SafeName][Required] string lower,
|
|
|
|
|
[FromRoute][SafeName][Required] string idf,
|
|
|
|
|
[FromRoute][SafeName][Required] string lowerf)
|
|
|
|
|
{
|
|
|
|
|
var pkgpath = Path.Combine(nugetSettings.PackagesRootDir,
|
|
|
|
|
id, lower, $"{idf}.{lowerf}.nuspec");
|
2021-06-22 00:10:01 +01:00
|
|
|
|
2021-06-22 23:03:38 +01:00
|
|
|
FileInfo pkgfi = new FileInfo(pkgpath);
|
|
|
|
|
return File(pkgfi.OpenRead(), "text/xml; charset=utf-8");
|
|
|
|
|
}
|
2021-04-08 02:03:17 +01:00
|
|
|
}
|
|
|
|
|
}
|