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
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Creates HTTP POST request & uploads database to server. Author : Farhan Ghumra
|
||||
/// </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)
|
||||
{
|
||||
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
|
||||
|
||||
// "X-NuGet-ApiKey
|
||||
string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
|
||||
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
|
||||
httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary;
|
||||
httpWebRequest.Method = "PUT";
|
||||
httpWebRequest.AllowAutoRedirect = false;
|
||||
httpWebRequest.Headers.Add("X-NuGet-ApiKey", apikey);
|
||||
|
||||
httpWebRequest.BeginGetRequestStream((result) =>
|
||||
{
|
||||
try
|
||||
|
|
@ -26,7 +32,7 @@ namespace nuget_cli
|
|||
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
|
||||
using (Stream requestStream = request.EndGetRequestStream(result))
|
||||
{
|
||||
WriteMultipartForm(requestStream, boundary, data, fileName, fileContentType, apikey, fileData);
|
||||
WriteMultipartForm(requestStream, boundary, data, fileName, fileContentType, fileData);
|
||||
}
|
||||
request.BeginGetResponse(a =>
|
||||
{
|
||||
|
|
@ -71,30 +77,23 @@ namespace nuget_cli
|
|||
/// <summary>
|
||||
/// Writes multi part HTTP POST request. Author : Farhan Ghumra
|
||||
/// </summary>
|
||||
public static void WriteMultipartForm(this Stream s, string boundary, Dictionary<string, string> data, string fileName, string fileContentType,
|
||||
string apiKey, byte[] fileData)
|
||||
public static void WriteMultipartForm(this Stream s, string boundary, Dictionary<string, string> data, string fileName, string fileContentType,
|
||||
byte[] fileData)
|
||||
{
|
||||
/// The first boundary
|
||||
byte[] boundarybytes = Encoding.UTF8.GetBytes("--" + boundary + "\r\n");
|
||||
/// the last boundary.
|
||||
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
|
||||
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
|
||||
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.
|
||||
bool bNeedsCRLF = false;
|
||||
|
||||
WriteToStream(s, string.Format(apiKeyHeaderTemplate, apiKey));
|
||||
|
||||
if (data != null)
|
||||
{
|
||||
foreach (string key in data.Keys)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
|
@ -22,19 +23,30 @@ namespace nuget_cli
|
|||
if (fi.Length > 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())
|
||||
{
|
||||
|
||||
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);
|
||||
report.UploadFilesToServer(new Uri(source),
|
||||
fparams, fi.Name, "application/octet-stream",
|
||||
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)
|
||||
{
|
||||
await Console.Error.WriteLineAsync(ex.Message);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue