isn/src/isnd/Controllers/Packages/Put.cs

220 lines
10 KiB
C#
Raw Normal View History

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-08-15 19:09:01 +01:00
using isnd.Data;
using isnd.Helpers;
2022-04-17 16:22:40 +01:00
using isnd.Entities;
2021-08-10 00:10:26 +01:00
using Microsoft.AspNetCore.Http;
2022-07-03 14:50:57 +01:00
using isn.abst;
2022-07-10 17:05:05 +01:00
using isnd.Data.Packages;
2021-06-21 23:38:05 +01:00
2021-08-15 19:09:01 +01:00
namespace isnd.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-09-02 23:37:28 +01:00
[HttpPut(_pkgRootPrefix + ApiConfig.Publish)]
2021-06-21 23:38:05 +01:00
public async Task<IActionResult> Put()
{
try
{
var clientVersionId = Request.Headers["X-NuGet-Client-Version"];
2021-08-10 00:10:26 +01:00
string apiKey = Request.Headers["X-NuGet-ApiKey"][0];
2021-06-21 23:38:05 +01:00
ViewData["versionId"] = typeof(PackagesController).Assembly.FullName;
var files = new List<string>();
ViewData["files"] = files;
2022-04-18 20:58:46 +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)
{
2022-04-18 20:58:46 +01:00
logger.LogError("403 : no api-key");
2021-06-21 23:38:05 +01:00
return Unauthorized();
}
2021-08-28 18:41:22 +01:00
Commit commit = new Commit
{
Action = PackageAction.PublishPackage,
TimeStamp = DateTime.Now
};
2022-04-18 20:58:46 +01:00
dbContext.Commits.Add(commit);
2021-06-21 23:38:05 +01:00
2021-08-10 00:10:26 +01:00
foreach (IFormFile file in Request.Form.Files)
2021-06-21 23:38:05 +01:00
{
string initpath = Path.Combine(Environment.GetEnvironmentVariable("TEMP") ??
Environment.GetEnvironmentVariable("TMP") ?? "/tmp",
2022-07-03 14:50:57 +01:00
$"isn-{Guid.NewGuid()}."+Constants.PaquetFileEstension);
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);
2022-07-03 14:50:57 +01:00
var spec = archive.Entries.FirstOrDefault(e => e.FullName.EndsWith("." + Constants.SpecFileEstension));
if (spec == null) return BadRequest(new { error = "no " + Constants.SpecFileEstension + " from archive" });
2021-06-21 23:38:05 +01:00
string pkgpath;
NuGetVersion version;
string pkgid;
string fullpath;
2022-07-03 14:50:57 +01:00
using (var specstr = spec.Open())
2021-06-21 23:38:05 +01:00
{
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
2022-04-18 20:58:46 +01:00
string pkgidpath = Path.Combine(isndSettings.PackagesRootDir,
2021-06-21 23:38:05 +01:00
pkgid);
pkgpath = Path.Combine(pkgidpath, version.ToFullString());
2022-07-03 14:50:57 +01:00
string name = $"{pkgid}-{version}."+Constants.PaquetFileEstension;
2021-06-21 23:38:05 +01:00
fullpath = Path.Combine(pkgpath, name);
var destpkgiddir = new DirectoryInfo(pkgidpath);
2022-07-03 14:50:57 +01:00
Package pkg = dbContext.Packages.SingleOrDefault(p => p.Id == pkgid);
if (pkg != null)
2021-06-21 23:38:05 +01:00
{
2022-07-03 14:50:57 +01:00
if (pkg.OwnerId != apikey.UserId)
2021-06-21 23:38:05 +01:00
{
return new ForbidResult();
}
2022-07-03 14:50:57 +01:00
pkg.Description = pkgdesc;
2021-06-21 23:38:05 +01:00
}
else
{
2022-07-03 14:50:57 +01:00
pkg = new Package
2021-06-21 23:38:05 +01:00
{
Id = pkgid,
Description = pkgdesc,
2021-08-28 18:41:22 +01:00
OwnerId = apikey.UserId,
2022-09-25 17:07:56 +01:00
LatestVersion = commit,
2021-06-21 23:38:05 +01:00
};
2022-07-03 14:50:57 +01:00
dbContext.Packages.Add(pkg);
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)
{
2021-08-11 23:25:15 +01:00
// La version existe sur le disque,
// mais si elle ne l'est pas en base de donnéés,
// on remplace la version sur disque.
2022-04-18 20:58:46 +01:00
var pkgv = dbContext.PackageVersions.Where(
2022-07-03 14:50:57 +01:00
v => v.PackageId == pkg.Id
2021-08-11 23:25:15 +01:00
);
if (pkgv !=null && pkgv.Count()==0)
{
dest.Delete();
}
else {
2022-04-18 20:58:46 +01:00
logger.LogWarning("400 : pkgversion:existant");
ModelState.AddModelError("pkgversion", "existant" );
return BadRequest(ModelState);
2021-08-11 23:25:15 +01:00
}
2021-06-21 23:38:05 +01:00
}
{
2021-08-11 23:25:15 +01:00
if (!destdir.Exists) destdir.Create();
2021-06-21 23:38:05 +01:00
source.MoveTo(fullpath);
files.Add(name);
string fullstringversion = version.ToFullString();
2022-04-18 20:58:46 +01:00
var pkgvers = dbContext.PackageVersions.Where
2022-07-03 14:50:57 +01:00
(v => v.PackageId == pkg.Id && v.FullString == fullstringversion);
2021-06-21 23:38:05 +01:00
if (pkgvers.Count() > 0)
{
foreach (var v in pkgvers.ToArray())
2022-04-18 20:58:46 +01:00
dbContext.PackageVersions.Remove(v);
2021-06-21 23:38:05 +01:00
}
2022-09-25 17:07:56 +01:00
2021-09-05 15:44:47 +01:00
// FIXME default type or null
2021-08-11 23:59:08 +01:00
if (types==null || types.Count==0)
2022-04-18 20:58:46 +01:00
dbContext.PackageVersions.Add
2021-08-11 23:59:08 +01:00
(new PackageVersion{
2022-07-03 14:50:57 +01:00
Package = pkg,
2021-08-11 23:59:08 +01:00
Major = version.Major,
Minor = version.Minor,
Patch = version.Patch,
2022-09-25 17:07:56 +01:00
Revision = version.Revision,
2021-08-11 23:59:08 +01:00
IsPrerelease = version.IsPrerelease,
FullString = version.ToFullString(),
2021-09-05 15:44:47 +01:00
Type = null,
2021-08-29 00:08:34 +01:00
LatestCommit = commit
2021-08-11 23:59:08 +01:00
});
else
2021-06-21 23:38:05 +01:00
foreach (var type in types)
{
var pkgver = new PackageVersion
{
2022-07-03 14:50:57 +01:00
Package = pkg,
2021-06-21 23:38:05 +01:00
Major = version.Major,
Minor = version.Minor,
Patch = version.Patch,
IsPrerelease = version.IsPrerelease,
FullString = version.ToFullString(),
2021-08-29 00:08:34 +01:00
Type = type.Name,
LatestCommit = commit
2021-06-21 23:38:05 +01:00
};
2022-04-18 20:58:46 +01:00
dbContext.PackageVersions.Add(pkgver);
2021-06-21 23:38:05 +01:00
}
2021-08-11 23:59:08 +01:00
2022-04-18 20:58:46 +01:00
await dbContext.SaveChangesAsync();
packageManager.ÛpdateCatalogFor(commit);
2021-06-21 23:38:05 +01:00
2022-07-03 14:50:57 +01:00
logger.LogInformation($"new paquet : {spec.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);
}
}
}
2022-07-03 14:50:57 +01:00
string nuspecfullpath = Path.Combine(pkgpath, pkgid + "." + Constants.SpecFileEstension);
2021-08-11 23:25:15 +01:00
FileInfo nfpi = new FileInfo(nuspecfullpath);
if (nfpi.Exists)
nfpi.Delete();
2022-07-03 14:50:57 +01:00
spec.ExtractToFile(nuspecfullpath);
2021-06-21 23:38:05 +01:00
}
}
return Ok(ViewData);
}
catch (Exception ex)
{
2022-04-18 20:58:46 +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 };
}
}
}
}