This commit is contained in:
Paul Schneider 2021-05-16 14:07:14 +01:00
commit e4511a8aaa
38 changed files with 615 additions and 68 deletions

View file

@ -4,6 +4,7 @@ using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Mvc;
@ -40,27 +41,29 @@ namespace nuget_host.Controllers
}
[HttpPut("packages")]
public IActionResult Put()
public async Task<IActionResult> Put()
{
string path = null;
var clientVersionId = Request.Headers["X-NuGet-Client-Version"];
var apiKey = Request.Headers["X-NuGet-ApiKey"];
ViewData["clientVersionId"] = clientVersionId;
ViewData["versionId"] = typeof(PackagesController).Assembly.FullName;
var files = new List<string>();
ViewData["files"] = files;
var clearkey = protector.Unprotect(apiKey);
var apikey = dbContext.ApiKeys.SingleOrDefault(k => k.Id == clearkey);
if (apikey == null)
return new BadRequestObjectResult(new { error = "api-key" });
return Unauthorized();
foreach (var file in Request.Form.Files)
{
try
{
files.Add(file.Name);
string initpath = file.Name;
string initpath = Path.Combine(Environment.GetEnvironmentVariable("TEMP"),
$"nuget_host-{Guid.NewGuid()}.nupkg");
using (FileStream fw = new FileStream(initpath, FileMode.Create))
{
file.CopyTo(fw);
@ -86,15 +89,66 @@ namespace nuget_host.Controllers
Path.Combine(pkgid,
Path.Combine(version.Version.ToString()),
$"{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);
logger.LogWarning($"200: {entry.Name}");
if (!destdir.Exists)
{
destdir.Create();
source.MoveTo(path);
var newpkg = new Package
{
Id = pkgid,
Description = pkgdesc,
OwnerId = apikey.UserId
};
dbContext.Packages.Add(newpkg);
var newversion = new PackageVersion
{
Package = newpkg,
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}");
}
else
{
var pkg = dbContext.Packages.SingleOrDefault(p => p.Id == pkgid);
if (pkg == null)
{
// TODO Choose an app policy to take ownership
// on existing package on disk.
throw new Exception("Package directory exists, but don't have any owner");
}
if (apikey.UserId != pkg.OwnerId)
return Unauthorized();
var newversion = new PackageVersion
{
PackageId = pkg.Id,
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 version : {entry.Name}");
}
}
}
}