129 lines
4.4 KiB
C#
129 lines
4.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
using System.Linq;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using NuGet.Packaging;
|
|
using nuget_host.Entities;
|
|
|
|
namespace nuget_host.Controllers
|
|
{
|
|
public class PackagesController : Controller
|
|
{
|
|
private readonly ILogger<PackagesController> logger;
|
|
private readonly IDataProtector protector;
|
|
|
|
private readonly NugetSettings nugetSettings;
|
|
|
|
public PackagesController(
|
|
ILoggerFactory loggerFactory,
|
|
IDataProtectionProvider provider,
|
|
IOptions<NugetSettings> nugetOptions)
|
|
{
|
|
logger = loggerFactory.CreateLogger<PackagesController>();
|
|
nugetSettings = nugetOptions.Value;
|
|
protector = provider.CreateProtector(nugetSettings.ProtectionTitle);
|
|
}
|
|
|
|
[HttpPut("packages/{*spec}")]
|
|
public IActionResult Put(string spec)
|
|
{
|
|
string path = null;
|
|
|
|
if (string.IsNullOrEmpty(spec))
|
|
{
|
|
var clientVersionId = Request.Headers["X-NuGet-Client-Version"];
|
|
var apiKey = Request.Headers["X-NuGet-ApiKey"];
|
|
ViewData["nuget client "] = "nuget {clientVersionId}";
|
|
|
|
var clearkey = protector.Unprotect(apiKey);
|
|
if (clearkey!= Startup.RootApiKeySecret)
|
|
return Unauthorized();
|
|
|
|
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);
|
|
foreach (var filename in archive.GetFiles())
|
|
{
|
|
if (filename.EndsWith(".nuspec"))
|
|
{
|
|
// var entry = archive.GetEntry(filename);
|
|
var specstr = archive.OpenFile(filename);
|
|
NuspecReader reader = new NuspecReader(specstr);
|
|
|
|
string pkgdesc = reader.GetDescription();
|
|
string pkgid = reader.GetId();
|
|
var version = reader.GetVersion();
|
|
|
|
|
|
path = Path.Combine(nugetSettings.PackagesRootDir,
|
|
Path.Combine(pkgid,
|
|
Path.Combine(version.ToFullString(),
|
|
$"{pkgid}-{version}.nupkg")));
|
|
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);
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return BadRequest();
|
|
}
|
|
return Ok(ViewData);
|
|
}
|
|
|
|
[HttpGet("Packages/{spec}")]
|
|
public IActionResult Index(string spec)
|
|
{
|
|
if (string.IsNullOrEmpty(spec))
|
|
{
|
|
ViewData["warn"] = "no spec";
|
|
}
|
|
else
|
|
{
|
|
ViewData["spec"] = spec;
|
|
// TODO Assert valid sem ver spec
|
|
var filelst = new DirectoryInfo(nugetSettings.PackagesRootDir);
|
|
var fi = new FileInfo(spec);
|
|
var lst = filelst.GetDirectories(spec);
|
|
ViewData["lst"] = lst.Select(entry => entry.Name);
|
|
}
|
|
return Ok(ViewData);
|
|
}
|
|
|
|
|
|
|
|
[Authorize]
|
|
[HttpGet("api/get-key/{*apikey}")]
|
|
public IActionResult GetApiKey(string apiKey)
|
|
{
|
|
return Ok(protector.Protect(apiKey));
|
|
}
|
|
}
|
|
} |