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

182 lines
7.3 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-16 14:07:14 +01:00
using System.Threading.Tasks;
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-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
}
2021-05-11 23:01:11 +01:00
[HttpPut("packages")]
2021-05-16 14:07:14 +01:00
public async Task<IActionResult> Put()
2021-04-08 02:03:17 +01:00
{
try
{
var clientVersionId = Request.Headers["X-NuGet-Client-Version"];
var apiKey = Request.Headers["X-NuGet-ApiKey"];
ViewData["versionId"] = typeof(PackagesController).Assembly.FullName;
var files = new List<string>();
ViewData["files"] = files;
2021-05-11 23:01:11 +01:00
var clearkey = protector.Unprotect(apiKey);
var apikey = dbContext.ApiKeys.SingleOrDefault(k => k.Id == clearkey);
if (apikey == null)
2021-04-08 02:03:17 +01:00
{
logger.LogInformation("403 : no api-key");
return Unauthorized();
}
foreach (var file in Request.Form.Files)
{
string initpath = Path.Combine(Environment.GetEnvironmentVariable("TEMP") ??
Environment.GetEnvironmentVariable("TMP") ?? "/tmp",
2021-05-16 14:07:14 +01:00
$"nuget_host-{Guid.NewGuid()}.nupkg");
2021-05-13 23:05:19 +01:00
using (FileStream fw = new FileStream(initpath, FileMode.Create))
{
file.CopyTo(fw);
}
2021-05-11 23:01:11 +01:00
2021-05-13 23:05:19 +01:00
using (FileStream fw = new FileStream(initpath, FileMode.Open))
2021-04-08 02:03:17 +01:00
{
var archive = new ZipArchive(fw);
2021-05-13 23:05:19 +01:00
foreach (var entry in archive.Entries)
2021-04-08 02:03:17 +01:00
{
2021-05-13 23:05:19 +01:00
if (entry.FullName.EndsWith(".nuspec"))
{
var specstr = entry.Open();
2021-05-20 20:24:03 +01:00
NuspecCoreReader reader = new NuspecCoreReader(specstr);
2021-05-13 23:05:19 +01:00
string pkgdesc = reader.GetDescription();
string pkgid = reader.GetId();
var version = reader.GetVersion();
string pkgidpath = Path.Combine(nugetSettings.PackagesRootDir,
pkgid);
string pkgpath = Path.Combine(pkgidpath, version.Version.ToString());
string name = $"{pkgid}-{version}.nupkg";
string fullpath = Path.Combine(pkgpath, name);
2021-05-20 20:24:03 +01:00
Package package;
var destpkgiddir = new DirectoryInfo(pkgidpath);
if (destpkgiddir.Exists)
{
package = dbContext.Packages.SingleOrDefault(p => p.Id == pkgid);
if (package != null) if (package.OwnerId != apikey.UserId)
{
return new ForbidResult();
}
}
else
{
destpkgiddir.Create();
package = new Package
{
Id = pkgid,
Description = pkgdesc,
OwnerId = apikey.UserId
};
dbContext.Packages.Add(package);
}
2021-05-16 14:07:14 +01:00
2021-05-13 23:05:19 +01:00
var source = new FileInfo(initpath);
var dest = new FileInfo(fullpath);
2021-05-13 23:05:19 +01:00
var destdir = new DirectoryInfo(dest.DirectoryName);
if (dest.Exists)
2021-05-18 00:39:03 +01:00
{
2021-05-20 20:24:03 +01:00
ViewData["error"] = "existant";
logger.LogWarning("400 : existant");
2021-05-18 00:39:03 +01:00
return BadRequest(ViewData);
}
2021-05-20 20:24:03 +01:00
else
2021-05-16 14:07:14 +01:00
{
2021-05-20 20:24:03 +01:00
destdir.Create();
source.MoveTo(fullpath);
files.Add(name);
var newversion = new PackageVersion
{
Package = package,
Major = version.Major,
Minor = version.Minor,
Patch = version.Patch,
IsPrerelease = version.IsPrerelease,
FullString = version.ToFullString()
};
dbContext.PackageVersions.Add(newversion);
await dbContext.SaveChangesAsync();
logger.LogInformation($"new package : {entry.Name}");
}
2021-05-13 23:05:19 +01:00
}
2021-04-08 02:03:17 +01:00
}
}
2021-05-13 23:05:19 +01:00
}
return Ok(ViewData);
}
catch (Exception ex)
{
return new ObjectResult(new { ViewData, ex.Message, ex.StackTrace })
{ StatusCode = 500 };
2021-04-08 02:03:17 +01:00
}
}
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
}
}