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-08-15 19:09:01 +01:00
|
|
|
using isnd.Data;
|
|
|
|
|
using isnd.Entities;
|
2021-07-05 12:55:52 +01:00
|
|
|
using Unleash;
|
|
|
|
|
using isnd.Services;
|
2021-08-15 19:09:01 +01:00
|
|
|
using isnd.ViewModels;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-08-23 03:21:08 +01:00
|
|
|
using isnd.Interfaces;
|
2021-04-08 02:03:17 +01:00
|
|
|
|
2021-08-15 19:09:01 +01:00
|
|
|
namespace isnd.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 defaultSemVer = "2.0.0";
|
2021-09-08 01:55:00 +01:00
|
|
|
private readonly Resource[] _resources;
|
2021-07-12 03:23:11 +01:00
|
|
|
private readonly ILogger<PackagesController> _logger;
|
|
|
|
|
private readonly IDataProtector _protector;
|
2021-04-08 02:03:17 +01:00
|
|
|
|
2021-08-15 01:50:07 +01:00
|
|
|
private readonly IsndSettings _isndSettings;
|
2021-07-12 03:23:11 +01:00
|
|
|
readonly ApplicationDbContext _dbContext;
|
2021-08-23 03:21:08 +01:00
|
|
|
private readonly IPackageManager _packageManager;
|
2021-07-11 18:08:49 +01:00
|
|
|
private readonly IUnleash _unleashĈlient;
|
2021-09-05 15:44:47 +01:00
|
|
|
const string _pkgRootPrefix = "~/";
|
2021-05-02 15:22:46 +01:00
|
|
|
|
|
|
|
|
public PackagesController(
|
|
|
|
|
ILoggerFactory loggerFactory,
|
|
|
|
|
IDataProtectionProvider provider,
|
2021-08-15 01:50:07 +01:00
|
|
|
IOptions<IsndSettings> isndOptions,
|
2021-07-11 22:13:48 +01:00
|
|
|
IUnleash unleashĈlient,
|
2021-08-23 03:21:08 +01:00
|
|
|
ApplicationDbContext dbContext,
|
|
|
|
|
IPackageManager pm)
|
2021-04-08 02:03:17 +01:00
|
|
|
{
|
2021-07-12 03:23:11 +01:00
|
|
|
_logger = loggerFactory.CreateLogger<PackagesController>();
|
2021-08-15 01:50:07 +01:00
|
|
|
_isndSettings = isndOptions.Value;
|
|
|
|
|
_protector = provider.CreateProtector(_isndSettings.ProtectionTitle);
|
2021-07-12 03:23:11 +01:00
|
|
|
_dbContext = dbContext;
|
2021-08-23 03:21:08 +01:00
|
|
|
_packageManager = pm;
|
2021-07-11 22:13:48 +01:00
|
|
|
_unleashĈlient = unleashĈlient;
|
2021-09-08 01:55:00 +01:00
|
|
|
_resources = _packageManager.GetResources(_unleashĈlient).ToArray();
|
2021-04-08 02:03:17 +01:00
|
|
|
}
|
|
|
|
|
|
2021-09-02 23:37:28 +01:00
|
|
|
[HttpGet(_pkgRootPrefix + ApiConfig.Base)]
|
2021-06-22 23:03:38 +01:00
|
|
|
public IActionResult ApiIndex()
|
|
|
|
|
{
|
2021-09-08 01:55:00 +01:00
|
|
|
return Ok(new { version = "3.0.0", resources = _resources });
|
2021-06-22 23:03:38 +01:00
|
|
|
}
|
|
|
|
|
|
2021-08-13 18:55:25 +01:00
|
|
|
// GET /autocomplete?id=isn.protocol&prerelease=true
|
2021-09-02 23:37:28 +01:00
|
|
|
[HttpGet(_pkgRootPrefix + ApiConfig.AutoComplete)]
|
2021-06-22 00:10:01 +01:00
|
|
|
public IActionResult AutoComplete(
|
|
|
|
|
string id,
|
2021-07-11 18:08:49 +01:00
|
|
|
string semVerLevel,
|
2021-06-22 00:10:01 +01:00
|
|
|
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-11 18:08:49 +01:00
|
|
|
if (semVerLevel != defaultSemVer)
|
|
|
|
|
{
|
|
|
|
|
ModelState.AddModelError("semVerLevel", defaultSemVer + " expected");
|
|
|
|
|
}
|
2021-07-05 12:55:52 +01:00
|
|
|
|
2021-07-12 03:23:11 +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-09-02 23:37:28 +01:00
|
|
|
[HttpGet(_pkgRootPrefix + ApiConfig.Get + "/{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-12 03:23:11 +01:00
|
|
|
versions = _packageManager.GetVersions(
|
2021-07-05 12:55:52 +01:00
|
|
|
id, parsedVersion, prerelease, packageType, skip, take)
|
2021-06-22 00:10:01 +01:00
|
|
|
});
|
|
|
|
|
}
|
2021-04-08 02:03:17 +01:00
|
|
|
}
|
|
|
|
|
}
|