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

142 lines
5 KiB
C#
Raw Normal View History

2021-04-08 02:03:17 +01:00
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
2021-05-09 03:06:23 +01:00
using System.Security.Claims;
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;
using Microsoft.Extensions.Logging;
2021-05-02 15:22:46 +01:00
using Microsoft.Extensions.Options;
2021-04-08 02:03:17 +01:00
using NuGet.Packaging;
2021-05-08 20:44:40 +01:00
using NuGet.Packaging.Core;
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-05-08 20:44:40 +01:00
using nuget_host.Helpers;
2021-04-08 02:03:17 +01:00
namespace nuget_host.Controllers
{
2021-05-09 03:06:23 +01:00
[AllowAnonymous]
2021-04-08 02:03:17 +01:00
public class PackagesController : Controller
{
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
}
[HttpPut("packages/{*spec}")]
public IActionResult Put(string spec)
{
string path = null;
2021-04-08 03:08:20 +01:00
2021-04-08 02:03:17 +01:00
if (string.IsNullOrEmpty(spec))
{
var clientVersionId = Request.Headers["X-NuGet-Client-Version"];
2021-04-08 03:08:20 +01:00
var apiKey = Request.Headers["X-NuGet-ApiKey"];
2021-05-08 20:44:40 +01:00
ViewData["nuget client"] = "nuget {clientVersionId}";
2021-04-08 03:08:20 +01:00
var clearkey = protector.Unprotect(apiKey);
2021-05-09 03:06:23 +01:00
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
var apikey = dbContext.ApiKeys.SingleOrDefault(k => k.Id == clearkey);
if (apikey == null)
return new BadRequestObjectResult(new {error = "api-key"});
2021-04-08 03:08:20 +01:00
2021-04-08 02:03:17 +01:00
foreach (var file in Request.Form.Files)
{
string initpath = "package.nupkg";
using (FileStream fw = new FileStream(initpath, FileMode.Create))
{
file.CopyTo(fw);
}
using (FileStream fw = new FileStream(initpath, FileMode.Open))
{
var archive = new System.IO.Compression.ZipArchive(fw);
2021-05-08 20:44:40 +01:00
foreach (var entry in archive.Entries)
2021-04-08 02:03:17 +01:00
{
2021-05-08 20:44:40 +01:00
if (entry.FullName.EndsWith(".nuspec"))
2021-04-08 02:03:17 +01:00
{
// var entry = archive.GetEntry(filename);
2021-05-08 20:44:40 +01:00
var specstr = entry.Open();
NuGet.Packaging.Core.NuspecCoreReader reader = new NuspecCoreReader(specstr);
2021-04-08 02:03:17 +01:00
string pkgdesc = reader.GetDescription();
string pkgid = reader.GetId();
var version = reader.GetVersion();
2021-05-02 15:22:46 +01:00
path = Path.Combine(nugetSettings.PackagesRootDir,
2021-04-08 02:03:17 +01:00
Path.Combine(pkgid,
2021-05-08 20:44:40 +01:00
Path.Combine(version.Version.ToString()),
$"{pkgid}-{version}.nupkg"));
2021-04-08 02:03:17 +01:00
var source = new FileInfo(initpath);
var dest = new FileInfo(path);
var destdir = new DirectoryInfo(dest.DirectoryName);
if (dest.Exists)
return BadRequest(new {error = "existant"});
if (!destdir.Exists) destdir.Create();
source.MoveTo(path);
2021-05-11 02:38:52 +01:00
logger.LogWarning($"200: {entry.Name}");
2021-04-08 02:03:17 +01:00
}
}
}
}
}
else
{
2021-05-11 02:38:52 +01:00
logger.LogWarning("400");
2021-05-09 03:06:23 +01:00
return new BadRequestObjectResult(ViewData);
2021-04-08 02:03:17 +01:00
}
return Ok(ViewData);
}
2021-05-11 02:38:52 +01:00
[HttpGet("packages/{spec}")]
2021-04-08 02:03:17 +01:00
public IActionResult Index(string spec)
{
if (string.IsNullOrEmpty(spec))
{
ViewData["warn"] = "no spec";
}
else
{
ViewData["spec"] = spec;
// TODO Assert valid sem ver spec
2021-05-02 15:22:46 +01:00
var filelst = new DirectoryInfo(nugetSettings.PackagesRootDir);
2021-04-08 02:03:17 +01:00
var fi = new FileInfo(spec);
var lst = filelst.GetDirectories(spec);
ViewData["lst"] = lst.Select(entry => entry.Name);
}
return Ok(ViewData);
}
2021-04-08 03:08:20 +01:00
2021-05-02 15:22:46 +01:00
2021-04-08 03:08:20 +01:00
[Authorize]
[HttpGet("api/get-key/{*apikey}")]
public IActionResult GetApiKey(string apiKey)
{
return Ok(protector.Protect(apiKey));
}
2021-04-08 02:03:17 +01:00
}
}