|
|
|
@ -3,14 +3,12 @@ using System.Collections.Generic;
|
|
|
|
|
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;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using NuGet.Packaging;
|
|
|
|
|
using NuGet.Packaging.Core;
|
|
|
|
|
using nuget_host.Data;
|
|
|
|
|
using nuget_host.Entities;
|
|
|
|
@ -43,28 +41,28 @@ namespace nuget_host.Controllers
|
|
|
|
|
[HttpPut("packages")]
|
|
|
|
|
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 apikey = dbContext.ApiKeys.SingleOrDefault(k => k.Id == clearkey);
|
|
|
|
|
if (apikey == null)
|
|
|
|
|
{
|
|
|
|
|
logger.LogInformation("403 : no api-key");
|
|
|
|
|
return Unauthorized();
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
foreach (var file in Request.Form.Files)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
var clearkey = protector.Unprotect(apiKey);
|
|
|
|
|
var apikey = dbContext.ApiKeys.SingleOrDefault(k => k.Id == clearkey);
|
|
|
|
|
if (apikey == null)
|
|
|
|
|
{
|
|
|
|
|
logger.LogInformation("403 : no api-key");
|
|
|
|
|
return Unauthorized();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var file in Request.Form.Files)
|
|
|
|
|
{
|
|
|
|
|
files.Add(file.Name);
|
|
|
|
|
string initpath = Path.Combine(Environment.GetEnvironmentVariable("TEMP"),
|
|
|
|
|
string initpath = Path.Combine(Environment.GetEnvironmentVariable("TEMP") ??
|
|
|
|
|
Environment.GetEnvironmentVariable("TMP") ?? "/tmp",
|
|
|
|
|
$"nuget_host-{Guid.NewGuid()}.nupkg");
|
|
|
|
|
|
|
|
|
|
using (FileStream fw = new FileStream(initpath, FileMode.Create))
|
|
|
|
@ -74,7 +72,7 @@ namespace nuget_host.Controllers
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
@ -87,14 +85,15 @@ namespace nuget_host.Controllers
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
path = Path.Combine(nugetSettings.PackagesRootDir,
|
|
|
|
|
Path.Combine(pkgid,
|
|
|
|
|
Path.Combine(version.Version.ToString()),
|
|
|
|
|
$"{pkgid}-{version}.nupkg"));
|
|
|
|
|
|
|
|
|
|
var source = new FileInfo(initpath);
|
|
|
|
|
var dest = new FileInfo(path);
|
|
|
|
|
var dest = new FileInfo(fullpath);
|
|
|
|
|
var destdir = new DirectoryInfo(dest.DirectoryName);
|
|
|
|
|
if (dest.Exists)
|
|
|
|
|
{
|
|
|
|
@ -103,98 +102,45 @@ namespace nuget_host.Controllers
|
|
|
|
|
return BadRequest(ViewData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!destdir.Exists)
|
|
|
|
|
|
|
|
|
|
destdir.Create();
|
|
|
|
|
source.MoveTo(fullpath);
|
|
|
|
|
files.Add(name);
|
|
|
|
|
var newpkg = new Package
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
Id = pkgid,
|
|
|
|
|
Description = pkgdesc,
|
|
|
|
|
OwnerId = apikey.UserId
|
|
|
|
|
};
|
|
|
|
|
dbContext.Packages.Add(newpkg);
|
|
|
|
|
|
|
|
|
|
var newversion = new PackageVersion
|
|
|
|
|
{
|
|
|
|
|
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}");
|
|
|
|
|
}
|
|
|
|
|
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}");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger.LogError($"400 en traitant {file.Name}:");
|
|
|
|
|
logger.LogError(ex.Message);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
return Ok(ViewData);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return new ObjectResult(new { ViewData, ex.Message, ex.StackTrace })
|
|
|
|
|
{ StatusCode = 500 };
|
|
|
|
|
}
|
|
|
|
|
return Ok(ViewData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("packages/{spec}")]
|
|
|
|
|