2021-06-21 23:38:05 +01:00
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.IO.Compression;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using NuGet.Packaging.Core;
|
|
|
|
|
using NuGet.Versioning;
|
2021-07-05 12:55:52 +01:00
|
|
|
using isn.Data;
|
|
|
|
|
using isn.Helpers;
|
2021-06-21 23:38:05 +01:00
|
|
|
|
2021-07-05 12:55:52 +01:00
|
|
|
namespace isn.Controllers
|
2021-06-21 23:38:05 +01:00
|
|
|
{
|
|
|
|
|
|
2021-07-05 12:55:52 +01:00
|
|
|
public partial class PackagesController
|
2021-06-21 23:38:05 +01:00
|
|
|
{
|
|
|
|
|
|
2021-07-12 03:23:11 +01:00
|
|
|
[HttpPut(_pkgRootPrefix)]
|
2021-06-21 23:38:05 +01:00
|
|
|
public async Task<IActionResult> Put()
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
|
2021-07-12 03:23:11 +01:00
|
|
|
var clearkey = _protector.Unprotect(apiKey);
|
|
|
|
|
var apikey = _dbContext.ApiKeys.SingleOrDefault(k => k.Id == clearkey);
|
2021-06-21 23:38:05 +01:00
|
|
|
if (apikey == null)
|
|
|
|
|
{
|
2021-07-12 03:23:11 +01:00
|
|
|
_logger.LogError("403 : no api-key");
|
2021-06-21 23:38:05 +01:00
|
|
|
return Unauthorized();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var file in Request.Form.Files)
|
|
|
|
|
{
|
|
|
|
|
string initpath = Path.Combine(Environment.GetEnvironmentVariable("TEMP") ??
|
|
|
|
|
Environment.GetEnvironmentVariable("TMP") ?? "/tmp",
|
2021-07-05 12:55:52 +01:00
|
|
|
$"isn-{Guid.NewGuid()}.nupkg");
|
2021-06-21 23:38:05 +01:00
|
|
|
|
|
|
|
|
using (FileStream fw = new FileStream(initpath, FileMode.Create))
|
|
|
|
|
{
|
|
|
|
|
file.CopyTo(fw);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (FileStream fw = new FileStream(initpath, FileMode.Open))
|
|
|
|
|
{
|
|
|
|
|
var archive = new ZipArchive(fw);
|
|
|
|
|
|
|
|
|
|
var nuspec = archive.Entries.FirstOrDefault(e => e.FullName.EndsWith(".nuspec"));
|
2021-06-22 01:25:28 +01:00
|
|
|
if (nuspec == null) return BadRequest(new { error = "no nuspec from archive" });
|
2021-06-21 23:38:05 +01:00
|
|
|
string pkgpath;
|
|
|
|
|
NuGetVersion version;
|
|
|
|
|
string pkgid;
|
|
|
|
|
string fullpath;
|
|
|
|
|
|
|
|
|
|
using (var specstr = nuspec.Open())
|
|
|
|
|
{
|
|
|
|
|
NuspecCoreReader reader = new NuspecCoreReader(specstr);
|
|
|
|
|
|
|
|
|
|
string pkgdesc = reader.GetDescription();
|
|
|
|
|
var types = reader.GetPackageTypes();
|
|
|
|
|
pkgid = reader.GetId();
|
|
|
|
|
version = reader.GetVersion();
|
2021-07-05 12:55:52 +01:00
|
|
|
|
2021-07-12 03:23:11 +01:00
|
|
|
string pkgidpath = Path.Combine(_nugetSettings.PackagesRootDir,
|
2021-06-21 23:38:05 +01:00
|
|
|
pkgid);
|
|
|
|
|
pkgpath = Path.Combine(pkgidpath, version.ToFullString());
|
|
|
|
|
string name = $"{pkgid}-{version}.nupkg";
|
|
|
|
|
fullpath = Path.Combine(pkgpath, name);
|
|
|
|
|
|
|
|
|
|
var destpkgiddir = new DirectoryInfo(pkgidpath);
|
2021-07-12 03:23:11 +01:00
|
|
|
Package package = _dbContext.Packages.SingleOrDefault(p => p.Id == pkgid);
|
2021-06-21 23:38:05 +01:00
|
|
|
if (package != null)
|
|
|
|
|
{
|
|
|
|
|
if (package.OwnerId != apikey.UserId)
|
|
|
|
|
{
|
|
|
|
|
return new ForbidResult();
|
|
|
|
|
}
|
|
|
|
|
package.Description = pkgdesc;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
package = new Package
|
|
|
|
|
{
|
|
|
|
|
Id = pkgid,
|
|
|
|
|
Description = pkgdesc,
|
|
|
|
|
OwnerId = apikey.UserId
|
|
|
|
|
};
|
2021-07-12 03:23:11 +01:00
|
|
|
_dbContext.Packages.Add(package);
|
2021-06-21 23:38:05 +01:00
|
|
|
}
|
|
|
|
|
if (!destpkgiddir.Exists) destpkgiddir.Create();
|
|
|
|
|
|
|
|
|
|
var source = new FileInfo(initpath);
|
|
|
|
|
var dest = new FileInfo(fullpath);
|
|
|
|
|
var destdir = new DirectoryInfo(dest.DirectoryName);
|
|
|
|
|
if (dest.Exists)
|
|
|
|
|
{
|
|
|
|
|
ViewData["msg"] = "existant";
|
|
|
|
|
ViewData["ecode"] = 1;
|
2021-07-12 03:23:11 +01:00
|
|
|
_logger.LogWarning("400 : existant");
|
2021-06-22 01:25:28 +01:00
|
|
|
return base.BadRequest(new { error = ModelState });
|
2021-06-21 23:38:05 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
destdir.Create();
|
|
|
|
|
source.MoveTo(fullpath);
|
|
|
|
|
files.Add(name);
|
|
|
|
|
string fullstringversion = version.ToFullString();
|
2021-07-12 03:23:11 +01:00
|
|
|
var pkgvers = _dbContext.PackageVersions.Where
|
2021-06-21 23:38:05 +01:00
|
|
|
(v => v.PackageId == package.Id && v.FullString == fullstringversion);
|
|
|
|
|
if (pkgvers.Count() > 0)
|
|
|
|
|
{
|
|
|
|
|
foreach (var v in pkgvers.ToArray())
|
2021-07-12 03:23:11 +01:00
|
|
|
_dbContext.PackageVersions.Remove(v);
|
2021-06-21 23:38:05 +01:00
|
|
|
}
|
|
|
|
|
foreach (var type in types)
|
|
|
|
|
{
|
|
|
|
|
var pkgver = new PackageVersion
|
|
|
|
|
{
|
|
|
|
|
Package = package,
|
|
|
|
|
Major = version.Major,
|
|
|
|
|
Minor = version.Minor,
|
|
|
|
|
Patch = version.Patch,
|
|
|
|
|
IsPrerelease = version.IsPrerelease,
|
|
|
|
|
FullString = version.ToFullString(),
|
|
|
|
|
Type = type.Name
|
|
|
|
|
};
|
2021-07-12 03:23:11 +01:00
|
|
|
_dbContext.PackageVersions.Add(pkgver);
|
2021-06-21 23:38:05 +01:00
|
|
|
}
|
2021-07-12 03:23:11 +01:00
|
|
|
await _dbContext.SaveChangesAsync();
|
2021-06-21 23:38:05 +01:00
|
|
|
|
2021-07-12 03:23:11 +01:00
|
|
|
_logger.LogInformation($"new package : {nuspec.Name}");
|
2021-06-21 23:38:05 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
using (var shacrypto = System.Security.Cryptography.SHA512.Create())
|
|
|
|
|
{
|
|
|
|
|
using (var stream = System.IO.File.OpenRead(fullpath))
|
|
|
|
|
{
|
|
|
|
|
var hash = shacrypto.ComputeHash(stream);
|
|
|
|
|
var shafullname = fullpath + ".sha512";
|
|
|
|
|
var hashtext = Convert.ToBase64String(hash);
|
|
|
|
|
var hashtextbytes = Encoding.ASCII.GetBytes(hashtext);
|
|
|
|
|
|
|
|
|
|
using (var shafile = System.IO.File.OpenWrite(shafullname))
|
|
|
|
|
{
|
|
|
|
|
shafile.Write(hashtextbytes, 0, hashtextbytes.Length);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
nuspec.ExtractToFile(Path.Combine(pkgpath, pkgid + ".nuspec"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return Ok(ViewData);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2021-07-12 03:23:11 +01:00
|
|
|
_logger.LogError(ex.Message);
|
|
|
|
|
_logger.LogError("Stack Trace: " + ex.StackTrace);
|
2021-06-21 23:38:05 +01:00
|
|
|
return new ObjectResult(new { ViewData, ex.Message })
|
|
|
|
|
{ StatusCode = 500 };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|