diff --git a/src/nuget-cli/Helpers.cs b/src/nuget-cli/Helpers.cs index 360e3f8..83818eb 100644 --- a/src/nuget-cli/Helpers.cs +++ b/src/nuget-cli/Helpers.cs @@ -8,12 +8,13 @@ namespace nuget_cli { public static class Helpers { + static readonly string clientVersion = nameof(Program) + " v1.0"; /// /// Creates HTTP POST request & uploads database to server. Author : Farhan Ghumra /// - static internal void UploadFilesToServer(this PushReport report, Uri uri, Dictionary data, string fileName, string fileContentType, - string apikey, byte[] fileData) + static internal void UploadFilesToServer(this PushReport report, Uri uri, Dictionary data, FileInfo fi, string fileContentType, + string apikey) { ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; @@ -23,6 +24,7 @@ namespace nuget_cli httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary; httpWebRequest.Method = "PUT"; httpWebRequest.AllowAutoRedirect = false; + httpWebRequest.Headers.Add("X-NuGet-Client-Version", clientVersion); httpWebRequest.Headers.Add("X-NuGet-ApiKey", apikey); httpWebRequest.BeginGetRequestStream((result) => @@ -32,7 +34,7 @@ namespace nuget_cli HttpWebRequest request = (HttpWebRequest)result.AsyncState; using (Stream requestStream = request.EndGetRequestStream(result)) { - WriteMultipartForm(requestStream, boundary, data, fileName, fileContentType, fileData); + WriteMultipartForm(requestStream, boundary, data, fi, fileContentType); } request.BeginGetResponse(a => { @@ -73,20 +75,18 @@ namespace nuget_cli httpWebRequest.GetResponse(); } + const int MAXSENDLEN = 65636; /// /// Writes multi part HTTP POST request. Author : Farhan Ghumra /// - public static void WriteMultipartForm(this Stream s, string boundary, Dictionary data, string fileName, string fileContentType, - byte[] fileData) + public static void WriteMultipartForm(this Stream s, string boundary, Dictionary data, FileInfo fi, string fileContentType) { /// The first boundary byte[] boundarybytes = Encoding.UTF8.GetBytes("--" + boundary + "\r\n"); /// the last boundary. byte[] trailer = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); - - /// the form data, properly formatted string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}"; /// the form-data file upload, properly formatted @@ -103,7 +103,7 @@ namespace nuget_cli WriteToStream(s, "\r\n"); /// Write the boundary. - WriteToStream(s, boundarybytes); + WriteToStream(s, boundarybytes, boundarybytes.Length); /// Write the key. WriteToStream(s, string.Format(formdataTemplate, key, data[key])); @@ -115,11 +115,17 @@ namespace nuget_cli if (bNeedsCRLF) WriteToStream(s, "\r\n"); - WriteToStream(s, boundarybytes); - WriteToStream(s, string.Format(fileheaderTemplate, "file", fileName, fileContentType)); + WriteToStream(s, boundarybytes, boundarybytes.Length); + WriteToStream(s, string.Format(fileheaderTemplate, "file", fi.Name, fileContentType)); /// Write the file data to the stream. - WriteToStream(s, fileData); - WriteToStream(s, trailer); + using (var fss = fi.OpenRead()) + { + byte[] buffer = new byte[MAXSENDLEN]; + var form_bytes_read = fss.Read(buffer, 0, MAXSENDLEN); + while (form_bytes_read>0) + WriteToStream(s, buffer, form_bytes_read); + } + WriteToStream(s, trailer, trailer.Length); } /// @@ -134,9 +140,9 @@ namespace nuget_cli /// /// Writes byte array to stream. Author : Farhan Ghumra /// - private static void WriteToStream(Stream s, byte[] bytes) + private static void WriteToStream(Stream s, byte[] bytes, int len) { - s.Write(bytes, 0, bytes.Length); + s.Write(bytes, 0, len); } } } \ No newline at end of file diff --git a/src/nuget-cli/Program.Commands.cs b/src/nuget-cli/Program.Commands.cs index a79c892..f663544 100644 --- a/src/nuget-cli/Program.Commands.cs +++ b/src/nuget-cli/Program.Commands.cs @@ -1,9 +1,11 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Threading.Tasks; namespace nuget_cli { + [Display(Name="nuget_cli")] partial class Program { diff --git a/src/nuget-cli/Program.cs b/src/nuget-cli/Program.cs index 0fcc481..00e6d43 100644 --- a/src/nuget-cli/Program.cs +++ b/src/nuget-cli/Program.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using Mono.Options; +using Newtonsoft.Json; namespace nuget_cli { @@ -73,11 +74,12 @@ namespace nuget_cli if (shouldShowPushHelp) { // output the options - Console.WriteLine("Push Options:"); + Console.Error.WriteLine("Push Options:"); pushoptions.WriteOptionDescriptions(Console.Out); return; } - await PushPkgAsync(pargs); + var reports = await PushPkgAsync(pargs); + Console.WriteLine(JsonConvert.SerializeObject(reports)); } }; commandSet.Add(push); diff --git a/src/nuget-cli/PushCommand.cs b/src/nuget-cli/PushCommand.cs index e8bac27..49cb154 100644 --- a/src/nuget-cli/PushCommand.cs +++ b/src/nuget-cli/PushCommand.cs @@ -20,20 +20,15 @@ namespace nuget_cli FileInfo fi = new FileInfo(pkg); if (!fi.Exists) throw new Exception("Le fichier n'existe pas"); - if (fi.Length > MAXSENDLEN) - throw new Exception($"Le fichier ne passe pas, trop gros ({MAXSENDLEN})."); - var fparams = new Dictionary { }; - using (var fss = fi.OpenRead()) - { + try { - byte[] buffer = new byte[MAXSENDLEN]; - var form_bytes_read = fss.Read(buffer, 0, MAXSENDLEN); + report.UploadFilesToServer(new Uri(source), - fparams, fi.Name, "application/octet-stream", - apikey, buffer); + fparams, fi, "application/octet-stream", + apikey); } catch (WebException ex) @@ -52,7 +47,6 @@ namespace nuget_cli await Console.Error.WriteLineAsync(ex.Message); throw; } - } report.Executed = true; return report; } diff --git a/src/nuget-cli/nuget-cli.csproj b/src/nuget-cli/nuget-cli.csproj index 8109df9..d1e0ff5 100644 --- a/src/nuget-cli/nuget-cli.csproj +++ b/src/nuget-cli/nuget-cli.csproj @@ -8,5 +8,6 @@ + diff --git a/src/nuget-host/Controllers/PackagesController.cs b/src/nuget-host/Controllers/PackagesController.cs index 5eebe7f..d5f2fcc 100644 --- a/src/nuget-host/Controllers/PackagesController.cs +++ b/src/nuget-host/Controllers/PackagesController.cs @@ -47,6 +47,8 @@ namespace nuget_host.Controllers var clientVersionId = Request.Headers["X-NuGet-Client-Version"]; var apiKey = Request.Headers["X-NuGet-ApiKey"]; ViewData["nuget client"] = "nuget {clientVersionId}"; + var files = new List(); + ViewData["files"] = files; var clearkey = protector.Unprotect(apiKey); var apikey = dbContext.ApiKeys.SingleOrDefault(k => k.Id == clearkey); @@ -55,45 +57,55 @@ namespace nuget_host.Controllers foreach (var file in Request.Form.Files) { - string initpath = "package.nupkg"; - using (FileStream fw = new FileStream(initpath, FileMode.Create)) + try { - file.CopyTo(fw); - } - - using (FileStream fw = new FileStream(initpath, FileMode.Open)) - { - var archive = new System.IO.Compression.ZipArchive(fw); + files.Add(file.Name); + string initpath = "package.nupkg"; + using (FileStream fw = new FileStream(initpath, FileMode.Create)) + { + file.CopyTo(fw); + } - foreach (var entry in archive.Entries) + using (FileStream fw = new FileStream(initpath, FileMode.Open)) { - if (entry.FullName.EndsWith(".nuspec")) + var archive = new System.IO.Compression.ZipArchive(fw); + + foreach (var entry in archive.Entries) { - // var entry = archive.GetEntry(filename); - var specstr = entry.Open(); - NuGet.Packaging.Core.NuspecCoreReader reader = new NuspecCoreReader(specstr); - - string pkgdesc = reader.GetDescription(); - string pkgid = reader.GetId(); - var version = reader.GetVersion(); - - 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 destdir = new DirectoryInfo(dest.DirectoryName); - if (dest.Exists) - return BadRequest(new { error = "existant" }); - - if (!destdir.Exists) destdir.Create(); - source.MoveTo(path); - logger.LogWarning($"200: {entry.Name}"); + if (entry.FullName.EndsWith(".nuspec")) + { + // var entry = archive.GetEntry(filename); + var specstr = entry.Open(); + NuGet.Packaging.Core.NuspecCoreReader reader = new NuspecCoreReader(specstr); + + string pkgdesc = reader.GetDescription(); + string pkgid = reader.GetId(); + var version = reader.GetVersion(); + + 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 destdir = new DirectoryInfo(dest.DirectoryName); + if (dest.Exists) + return BadRequest(new { error = "existant" }); + + if (!destdir.Exists) destdir.Create(); + source.MoveTo(path); + logger.LogWarning($"200: {entry.Name}"); + } + } } - + } + catch (Exception ex) + { + logger.LogError($"400: {file.Name}"); + logger.LogError(ex.Message); + return new BadRequestObjectResult(ViewData); } } return Ok(ViewData);