push
This commit is contained in:
parent
512495d10b
commit
97dec15153
5 changed files with 75 additions and 78 deletions
|
|
@ -8,17 +8,23 @@ namespace nuget_cli
|
||||||
{
|
{
|
||||||
public static class Helpers
|
public static class Helpers
|
||||||
{
|
{
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates HTTP POST request & uploads database to server. Author : Farhan Ghumra
|
/// Creates HTTP POST request & uploads database to server. Author : Farhan Ghumra
|
||||||
/// </summary>
|
/// </summary>
|
||||||
static internal void UploadFilesToServer(this PushReport report, Uri uri, Dictionary<string, string> data, string fileName, string fileContentType,
|
static internal void UploadFilesToServer(this PushReport report, Uri uri, Dictionary<string, string> data, string fileName, string fileContentType,
|
||||||
string apikey, byte[] fileData)
|
string apikey, byte[] fileData)
|
||||||
{
|
{
|
||||||
|
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
|
||||||
|
|
||||||
// "X-NuGet-ApiKey
|
// "X-NuGet-ApiKey
|
||||||
string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
|
string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
|
||||||
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
|
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
|
||||||
httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary;
|
httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary;
|
||||||
httpWebRequest.Method = "PUT";
|
httpWebRequest.Method = "PUT";
|
||||||
|
httpWebRequest.AllowAutoRedirect = false;
|
||||||
|
httpWebRequest.Headers.Add("X-NuGet-ApiKey", apikey);
|
||||||
|
|
||||||
httpWebRequest.BeginGetRequestStream((result) =>
|
httpWebRequest.BeginGetRequestStream((result) =>
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|
@ -26,7 +32,7 @@ namespace nuget_cli
|
||||||
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
|
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
|
||||||
using (Stream requestStream = request.EndGetRequestStream(result))
|
using (Stream requestStream = request.EndGetRequestStream(result))
|
||||||
{
|
{
|
||||||
WriteMultipartForm(requestStream, boundary, data, fileName, fileContentType, apikey, fileData);
|
WriteMultipartForm(requestStream, boundary, data, fileName, fileContentType, fileData);
|
||||||
}
|
}
|
||||||
request.BeginGetResponse(a =>
|
request.BeginGetResponse(a =>
|
||||||
{
|
{
|
||||||
|
|
@ -72,29 +78,22 @@ namespace nuget_cli
|
||||||
/// Writes multi part HTTP POST request. Author : Farhan Ghumra
|
/// Writes multi part HTTP POST request. Author : Farhan Ghumra
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static void WriteMultipartForm(this Stream s, string boundary, Dictionary<string, string> data, string fileName, string fileContentType,
|
public static void WriteMultipartForm(this Stream s, string boundary, Dictionary<string, string> data, string fileName, string fileContentType,
|
||||||
string apiKey, byte[] fileData)
|
byte[] fileData)
|
||||||
{
|
{
|
||||||
/// The first boundary
|
/// The first boundary
|
||||||
byte[] boundarybytes = Encoding.UTF8.GetBytes("--" + boundary + "\r\n");
|
byte[] boundarybytes = Encoding.UTF8.GetBytes("--" + boundary + "\r\n");
|
||||||
/// the last boundary.
|
/// the last boundary.
|
||||||
byte[] trailer = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
|
byte[] trailer = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Api Key header template
|
|
||||||
/// </summary>
|
|
||||||
/// <value></value>
|
|
||||||
string apiKeyHeaderTemplate = "X-NuGet-ApiKey: \"{0}\"\r\n";
|
|
||||||
|
|
||||||
/// the form data, properly formatted
|
/// the form data, properly formatted
|
||||||
string formdataTemplate = "Content-Dis-data; name=\"{0}\"\r\n\r\n{1}";
|
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
|
||||||
/// the form-data file upload, properly formatted
|
/// the form-data file upload, properly formatted
|
||||||
string fileheaderTemplate = "Content-Dis-data; name=\"{0}\"; filename=\"{1}\";\r\nContent-Type: {2}\r\n\r\n";
|
string fileheaderTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\";\r\nContent-Type: {2}\r\n\r\n";
|
||||||
|
|
||||||
/// Added to track if we need a CRLF or not.
|
/// Added to track if we need a CRLF or not.
|
||||||
bool bNeedsCRLF = false;
|
bool bNeedsCRLF = false;
|
||||||
|
|
||||||
WriteToStream(s, string.Format(apiKeyHeaderTemplate, apiKey));
|
|
||||||
|
|
||||||
if (data != null)
|
if (data != null)
|
||||||
{
|
{
|
||||||
foreach (string key in data.Keys)
|
foreach (string key in data.Keys)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
|
@ -22,19 +23,30 @@ namespace nuget_cli
|
||||||
if (fi.Length > MAXSENDLEN)
|
if (fi.Length > MAXSENDLEN)
|
||||||
throw new Exception($"Le fichier ne passe pas, trop gros ({MAXSENDLEN}).");
|
throw new Exception($"Le fichier ne passe pas, trop gros ({MAXSENDLEN}).");
|
||||||
|
|
||||||
var fparams = new Dictionary<string, string> { { "userid", "9" } };
|
var fparams = new Dictionary<string, string> { };
|
||||||
|
|
||||||
using (var fss = fi.OpenRead())
|
using (var fss = fi.OpenRead())
|
||||||
{
|
{
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
byte[] buffer = new byte[MAXSENDLEN];
|
byte[] buffer = new byte[MAXSENDLEN];
|
||||||
var form_bytes_read = fss.Read(buffer, 0, MAXSENDLEN);
|
var form_bytes_read = fss.Read(buffer, 0, MAXSENDLEN);
|
||||||
report.UploadFilesToServer(new Uri(source), fparams, fi.Name, "application/octet-stream",
|
report.UploadFilesToServer(new Uri(source),
|
||||||
|
fparams, fi.Name, "application/octet-stream",
|
||||||
apikey, buffer);
|
apikey, buffer);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
catch (WebException ex)
|
||||||
|
{
|
||||||
|
await Console.Error.WriteLineAsync(ex.Message);
|
||||||
|
report.StatusCode = ex.Status.ToString();
|
||||||
|
using (var respStream = ex.Response.GetResponseStream())
|
||||||
|
{
|
||||||
|
|
||||||
|
StreamReader sr = new StreamReader(respStream);
|
||||||
|
report.Message = sr.ReadToEnd();
|
||||||
|
}
|
||||||
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
await Console.Error.WriteLineAsync(ex.Message);
|
await Console.Error.WriteLineAsync(ex.Message);
|
||||||
|
|
|
||||||
|
|
@ -39,22 +39,19 @@ namespace nuget_host.Controllers
|
||||||
this.dbContext = dbContext;
|
this.dbContext = dbContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPut("packages/{*spec}")]
|
[HttpPut("packages")]
|
||||||
public IActionResult Put(string spec)
|
public IActionResult Put()
|
||||||
{
|
{
|
||||||
string path = null;
|
string path = null;
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(spec))
|
|
||||||
{
|
|
||||||
var clientVersionId = Request.Headers["X-NuGet-Client-Version"];
|
var clientVersionId = Request.Headers["X-NuGet-Client-Version"];
|
||||||
var apiKey = Request.Headers["X-NuGet-ApiKey"];
|
var apiKey = Request.Headers["X-NuGet-ApiKey"];
|
||||||
ViewData["nuget client"] = "nuget {clientVersionId}";
|
ViewData["nuget client"] = "nuget {clientVersionId}";
|
||||||
|
|
||||||
var clearkey = protector.Unprotect(apiKey);
|
var clearkey = protector.Unprotect(apiKey);
|
||||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
||||||
var apikey = dbContext.ApiKeys.SingleOrDefault(k => k.Id == clearkey);
|
var apikey = dbContext.ApiKeys.SingleOrDefault(k => k.Id == clearkey);
|
||||||
if (apikey == null)
|
if (apikey == null)
|
||||||
return new BadRequestObjectResult(new {error = "api-key"});
|
return new BadRequestObjectResult(new { error = "api-key" });
|
||||||
|
|
||||||
foreach (var file in Request.Form.Files)
|
foreach (var file in Request.Form.Files)
|
||||||
{
|
{
|
||||||
|
|
@ -88,7 +85,7 @@ namespace nuget_host.Controllers
|
||||||
var dest = new FileInfo(path);
|
var dest = new FileInfo(path);
|
||||||
var destdir = new DirectoryInfo(dest.DirectoryName);
|
var destdir = new DirectoryInfo(dest.DirectoryName);
|
||||||
if (dest.Exists)
|
if (dest.Exists)
|
||||||
return BadRequest(new {error = "existant"});
|
return BadRequest(new { error = "existant" });
|
||||||
|
|
||||||
if (!destdir.Exists) destdir.Create();
|
if (!destdir.Exists) destdir.Create();
|
||||||
source.MoveTo(path);
|
source.MoveTo(path);
|
||||||
|
|
@ -98,15 +95,6 @@ namespace nuget_host.Controllers
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
logger.LogWarning("400");
|
|
||||||
return new BadRequestObjectResult(ViewData);
|
|
||||||
}
|
}
|
||||||
return Ok(ViewData);
|
return Ok(ViewData);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -77,8 +77,6 @@ namespace nuget_host
|
||||||
|
|
||||||
app.UseStaticFiles();
|
app.UseStaticFiles();
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
|
||||||
|
|
||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="../../src/nuget-host/nuget-host.csproj" />
|
<ProjectReference Include="..\..\src\nuget-host\nuget-host.csproj" />
|
||||||
<ProjectReference Include="..\..\src\nuget-cli\nuget-cli.csproj" />
|
<ProjectReference Include="..\..\src\nuget-cli\nuget-cli.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue