[{"PkgName":"nuget-cli.1.0.0.nupkg","Executed":true,"OK":true,"AlreadyPresent":false,"Message":"{\"versionId\":\"nuget-host, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null\",\"files\":[\"nuget-cli-1.0.0.nupkg\"]}","StatusCode":"OK","StackTrace":null}]

broken/ef
Paul Schneider 3 years ago
parent f6a27fa919
commit 3a59b8304a
3 changed files with 58 additions and 112 deletions

@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net472</TargetFramework> <TargetFramework>net472</TargetFramework>
<RootNamespace>nuget_cli</RootNamespace> <RootNamespace>nuget_cli</RootNamespace>
<UserSecretsId>45b74c62-05bc-4603-95b4-3e80ae2fdf50</UserSecretsId> <UserSecretsId>45b74c62-05bc-4603-95b4-3e80ae2fdf50</UserSecretsId>
<IsTool>true</IsTool>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Mono.Options" Version="5.3.0" /> <PackageReference Include="Mono.Options" Version="5.3.0" />

@ -3,14 +3,12 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Linq; using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using NuGet.Packaging;
using NuGet.Packaging.Core; using NuGet.Packaging.Core;
using nuget_host.Data; using nuget_host.Data;
using nuget_host.Entities; using nuget_host.Entities;
@ -43,28 +41,28 @@ namespace nuget_host.Controllers
[HttpPut("packages")] [HttpPut("packages")]
public async Task<IActionResult> Put() public async Task<IActionResult> Put()
{ {
string path = null; 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;
var clearkey = protector.Unprotect(apiKey); var clientVersionId = Request.Headers["X-NuGet-Client-Version"];
var apikey = dbContext.ApiKeys.SingleOrDefault(k => k.Id == clearkey); var apiKey = Request.Headers["X-NuGet-ApiKey"];
if (apikey == null) ViewData["versionId"] = typeof(PackagesController).Assembly.FullName;
{ var files = new List<string>();
logger.LogInformation("403 : no api-key"); ViewData["files"] = files;
return Unauthorized();
}
foreach (var file in Request.Form.Files) var clearkey = protector.Unprotect(apiKey);
{ var apikey = dbContext.ApiKeys.SingleOrDefault(k => k.Id == clearkey);
try if (apikey == null)
{ {
files.Add(file.Name); logger.LogInformation("403 : no api-key");
string initpath = Path.Combine(Environment.GetEnvironmentVariable("TEMP"), return Unauthorized();
}
foreach (var file in Request.Form.Files)
{
string initpath = Path.Combine(Environment.GetEnvironmentVariable("TEMP") ??
Environment.GetEnvironmentVariable("TMP") ?? "/tmp",
$"nuget_host-{Guid.NewGuid()}.nupkg"); $"nuget_host-{Guid.NewGuid()}.nupkg");
using (FileStream fw = new FileStream(initpath, FileMode.Create)) using (FileStream fw = new FileStream(initpath, FileMode.Create))
@ -74,7 +72,7 @@ namespace nuget_host.Controllers
using (FileStream fw = new FileStream(initpath, FileMode.Open)) using (FileStream fw = new FileStream(initpath, FileMode.Open))
{ {
var archive = new System.IO.Compression.ZipArchive(fw); var archive = new ZipArchive(fw);
foreach (var entry in archive.Entries) foreach (var entry in archive.Entries)
{ {
@ -87,14 +85,15 @@ namespace nuget_host.Controllers
string pkgdesc = reader.GetDescription(); string pkgdesc = reader.GetDescription();
string pkgid = reader.GetId(); string pkgid = reader.GetId();
var version = reader.GetVersion(); 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);
path = Path.Combine(nugetSettings.PackagesRootDir,
Path.Combine(pkgid,
Path.Combine(version.Version.ToString()),
$"{pkgid}-{version}.nupkg"));
var source = new FileInfo(initpath); var source = new FileInfo(initpath);
var dest = new FileInfo(path); var dest = new FileInfo(fullpath);
var destdir = new DirectoryInfo(dest.DirectoryName); var destdir = new DirectoryInfo(dest.DirectoryName);
if (dest.Exists) if (dest.Exists)
{ {
@ -103,98 +102,45 @@ namespace nuget_host.Controllers
return BadRequest(ViewData); return BadRequest(ViewData);
} }
if (!destdir.Exists)
destdir.Create();
source.MoveTo(fullpath);
files.Add(name);
var newpkg = new Package
{ {
destdir.Create(); Id = pkgid,
source.MoveTo(path); Description = pkgdesc,
var newpkg = new Package OwnerId = apikey.UserId
{ };
Id = pkgid, dbContext.Packages.Add(newpkg);
Description = pkgdesc,
OwnerId = apikey.UserId var newversion = new PackageVersion
};
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); Package = newpkg,
if (pkg == null) Major = version.Major,
{ Minor = version.Minor,
// TODO Choose an app policy to take ownership Patch = version.Patch,
// and populate db with zip info IsPrerelease = version.IsPrerelease,
// from either existing package on disk, FullString = version.ToFullString()
// or from request };
logger.LogError("Package directory exists, but don't have any owner"); dbContext.PackageVersions.Add(newversion);
throw new NotImplementedException();
await dbContext.SaveChangesAsync();
Package newpkgfromdisk = new Package{ logger.LogInformation($"new package : {entry.Name}");
Id = pkgid,
Description = pkgdesc,
OwnerId = apikey.UserId
};
dbContext.Packages.Add(newpkgfromdisk);
var newversionfromdisk = new PackageVersion
{
Package = newpkgfromdisk,
Major = version.Major,
Minor = version.Minor,
Patch = version.Patch,
IsPrerelease = version.IsPrerelease,
FullString = version.ToFullString()
};
dbContext.PackageVersions.Add(newversionfromdisk);
await dbContext.SaveChangesAsync();
logger.LogInformation($"new package : {entry.Name}");
}
if (apikey.UserId != pkg.OwnerId)
{
logger.LogInformation("403 : not owner");
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}");
}
} }
} }
} }
} }
catch (Exception ex) return Ok(ViewData);
{ }
logger.LogError($"400 en traitant {file.Name}:"); catch (Exception ex)
logger.LogError(ex.Message); {
throw; return new ObjectResult(new { ViewData, ex.Message, ex.StackTrace })
} { StatusCode = 500 };
} }
return Ok(ViewData);
} }
[HttpGet("packages/{spec}")] [HttpGet("packages/{spec}")]

Loading…