diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f69123e..7cf4b42 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -31,7 +31,6 @@ test2: script: - echo "Do another parallel test here" - echo "For example run a lint test" - - echo BUT DONO dotnet user-secrets list --project test/nuget.host.tests/ publish: stage: deploy diff --git a/src/nuget-host/Controllers/PackagesController.cs b/src/nuget-host/Controllers/PackagesController.cs index 277b138..249eb0a 100644 --- a/src/nuget-host/Controllers/PackagesController.cs +++ b/src/nuget-host/Controllers/PackagesController.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Linq; +using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.DataProtection; @@ -10,6 +11,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using NuGet.Packaging.Core; +using NuGet.Versioning; using nuget_host.Data; using nuget_host.Entities; using nuget_host.Helpers; @@ -72,74 +74,92 @@ namespace nuget_host.Controllers { var archive = new ZipArchive(fw); - foreach (var entry in archive.Entries) + var nuspec = archive.Entries.FirstOrDefault(e => e.FullName.EndsWith(".nuspec")); + if (nuspec==null) return BadRequest("no nuspec from archive"); + string pkgpath; + NuGetVersion version; + string pkgid; + string fullpath; + + using (var specstr = nuspec.Open()) { - if (entry.FullName.EndsWith(".nuspec")) + NuspecCoreReader reader = new NuspecCoreReader(specstr); + + string pkgdesc = reader.GetDescription(); + pkgid = reader.GetId(); + version = reader.GetVersion(); + string pkgidpath = Path.Combine(nugetSettings.PackagesRootDir, + pkgid); + pkgpath = Path.Combine(pkgidpath, version.ToFullString()); + string name = $"{pkgid}-{version}.nupkg"; + fullpath = Path.Combine(pkgpath, name); + + var destpkgiddir = new DirectoryInfo(pkgidpath); + Package package = dbContext.Packages.SingleOrDefault(p => p.Id == pkgid); + if (package != null) { - var specstr = entry.Open(); - NuspecCoreReader reader = new NuspecCoreReader(specstr); - - 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.ToFullString()); - string name = $"{pkgid}-{version}.nupkg"; - string fullpath = Path.Combine(pkgpath, name); - - var destpkgiddir = new DirectoryInfo(pkgidpath); - Package package = dbContext.Packages.SingleOrDefault(p => p.Id == pkgid); - if (package != null) + if (package.OwnerId != apikey.UserId) { - if (package.OwnerId != apikey.UserId) - { - return new ForbidResult(); - } + return new ForbidResult(); } - else + } + else + { + package = new Package { - package = new Package - { - Id = pkgid, - Description = pkgdesc, - OwnerId = apikey.UserId - }; - dbContext.Packages.Add(package); - } - if (!destpkgiddir.Exists) destpkgiddir.Create(); + Id = pkgid, + Description = pkgdesc, + OwnerId = apikey.UserId + }; + dbContext.Packages.Add(package); + } + 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["error"] = "existant"; + logger.LogWarning("400 : existant"); + return BadRequest(ViewData); + } + else + { + destdir.Create(); - var source = new FileInfo(initpath); - var dest = new FileInfo(fullpath); - var destdir = new DirectoryInfo(dest.DirectoryName); - if (dest.Exists) + source.MoveTo(fullpath); + files.Add(name); + var newversion = new PackageVersion { - ViewData["error"] = "existant"; - logger.LogWarning("400 : existant"); - return BadRequest(ViewData); - } - else + Package = package, + 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 : {nuspec.Name}"); + } + } + 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)) { - destdir.Create(); - - source.MoveTo(fullpath); - files.Add(name); - var newversion = new PackageVersion - { - Package = package, - 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}"); + shafile.Write(hashtextbytes, 0, hashtextbytes.Length); } - break; } } + nuspec.ExtractToFile(Path.Combine(pkgpath, pkgid + ".nuspec")); } } @@ -153,21 +173,21 @@ namespace nuget_host.Controllers { StatusCode = 500 }; } } - - [HttpGet("packages/{spec}")] - public IActionResult Index(string spec) + // dotnet add . package -s http://localhost:5000/packages nuget-cli + // packages/FindPackagesById()?id='nuget-cli'&semVerLevel=2.0.0 + [HttpGet("packages/FindPackagesById()")] + public IActionResult Index(string id, string semVerLevel) { - if (string.IsNullOrEmpty(spec)) + if (string.IsNullOrEmpty(id)) { - ViewData["warn"] = "no spec"; + ViewData["warn"] = "no id"; } else { - ViewData["spec"] = spec; + ViewData["id"] = id; // TODO Assert valid sem ver spec var filelst = new DirectoryInfo(nugetSettings.PackagesRootDir); - var fi = new FileInfo(spec); - var lst = filelst.GetDirectories(spec); + var lst = filelst.GetDirectories(id); ViewData["lst"] = lst.Select(entry => entry.Name); } return Ok(ViewData); diff --git a/src/nuget-host/packages/nuget-cli/1.0.0/nuget-cli-1.0.0.nupkg b/src/nuget-host/packages/nuget-cli/1.0.0/nuget-cli-1.0.0.nupkg deleted file mode 100644 index 2b57d2c..0000000 Binary files a/src/nuget-host/packages/nuget-cli/1.0.0/nuget-cli-1.0.0.nupkg and /dev/null differ