[{"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,7 +41,9 @@ 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 clientVersionId = Request.Headers["X-NuGet-Client-Version"];
var apiKey = Request.Headers["X-NuGet-ApiKey"]; var apiKey = Request.Headers["X-NuGet-ApiKey"];
@ -61,10 +61,8 @@ namespace nuget_host.Controllers
foreach (var file in Request.Form.Files) foreach (var file in Request.Form.Files)
{ {
try string initpath = Path.Combine(Environment.GetEnvironmentVariable("TEMP") ??
{ Environment.GetEnvironmentVariable("TMP") ?? "/tmp",
files.Add(file.Name);
string initpath = Path.Combine(Environment.GetEnvironmentVariable("TEMP"),
$"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,10 +102,10 @@ namespace nuget_host.Controllers
return BadRequest(ViewData); return BadRequest(ViewData);
} }
if (!destdir.Exists)
{
destdir.Create(); destdir.Create();
source.MoveTo(path); source.MoveTo(fullpath);
files.Add(name);
var newpkg = new Package var newpkg = new Package
{ {
Id = pkgid, Id = pkgid,
@ -128,74 +127,21 @@ namespace nuget_host.Controllers
await dbContext.SaveChangesAsync(); await dbContext.SaveChangesAsync();
logger.LogInformation($"new package : {entry.Name}"); 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
// and populate db with zip info
// from either existing package on disk,
// or from request
logger.LogError("Package directory exists, but don't have any owner");
throw new NotImplementedException();
Package newpkgfromdisk = new Package{
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}");
}
} }
} }
} }
return Ok(ViewData);
} }
catch (Exception ex) catch (Exception ex)
{ {
logger.LogError($"400 en traitant {file.Name}:"); return new ObjectResult(new { ViewData, ex.Message, ex.StackTrace })
logger.LogError(ex.Message); { StatusCode = 500 };
throw;
} }
} }
return Ok(ViewData);
}
[HttpGet("packages/{spec}")] [HttpGet("packages/{spec}")]
public IActionResult Index(string spec) public IActionResult Index(string spec)

Loading…