diff --git a/src/nuget-cli/nuget-cli.csproj b/src/nuget-cli/nuget-cli.csproj index ab0444f..6e4aa77 100644 --- a/src/nuget-cli/nuget-cli.csproj +++ b/src/nuget-cli/nuget-cli.csproj @@ -1,10 +1,10 @@  - Exe net472 nuget_cli 45b74c62-05bc-4603-95b4-3e80ae2fdf50 + true diff --git a/src/nuget-host/Controllers/PackagesController.cs b/src/nuget-host/Controllers/PackagesController.cs index 83648f2..5305122 100644 --- a/src/nuget-host/Controllers/PackagesController.cs +++ b/src/nuget-host/Controllers/PackagesController.cs @@ -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 Put() { - string path = null; - - 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(); - ViewData["files"] = files; - - var clearkey = protector.Unprotect(apiKey); - var apikey = dbContext.ApiKeys.SingleOrDefault(k => k.Id == clearkey); - if (apikey == null) + try { - logger.LogInformation("403 : no api-key"); - return Unauthorized(); - } - foreach (var file in Request.Form.Files) - { - 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(); + ViewData["files"] = files; + + var clearkey = protector.Unprotect(apiKey); + var apikey = dbContext.ApiKeys.SingleOrDefault(k => k.Id == clearkey); + if (apikey == null) { - files.Add(file.Name); - string initpath = Path.Combine(Environment.GetEnvironmentVariable("TEMP"), + logger.LogInformation("403 : no api-key"); + return Unauthorized(); + } + + foreach (var file in Request.Form.Files) + { + 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); + 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 + 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 = newpkg, + Major = version.Major, + Minor = version.Minor, + Patch = version.Patch, + IsPrerelease = version.IsPrerelease, + FullString = version.ToFullString() + }; + dbContext.PackageVersions.Add(newversion); - 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}"); - } + 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}")] diff --git a/src/nuget-host/packages/Yavsc.Abstract/1.0.6-rc17/Yavsc.Abstract-1.0.6-rc17.nupkg b/src/nuget-host/packages/Yavsc.Abstract/1.0.6-rc17/Yavsc.Abstract-1.0.6-rc17.nupkg deleted file mode 100644 index 57939af..0000000 Binary files a/src/nuget-host/packages/Yavsc.Abstract/1.0.6-rc17/Yavsc.Abstract-1.0.6-rc17.nupkg and /dev/null differ