Don't merge.
This commit is contained in:
@ -2,147 +2,75 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace nuget_cli
|
||||
{
|
||||
public static class Helpers
|
||||
{
|
||||
static readonly string clientVersion = nameof(Program) + " v1.0";
|
||||
static readonly string clientVersion = "nuget_cli v1.0";
|
||||
|
||||
/// <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, FileInfo fi, string fileContentType,
|
||||
string apikey)
|
||||
static internal async Task UploadFilesToServer(
|
||||
this PushReport report, Uri uri,
|
||||
FileInfo fi, string apikey)
|
||||
{
|
||||
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-Client-Version", clientVersion);
|
||||
httpWebRequest.Headers.Add("X-NuGet-ApiKey", apikey);
|
||||
|
||||
httpWebRequest.BeginGetRequestStream((result) =>
|
||||
try
|
||||
{
|
||||
try
|
||||
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
|
||||
|
||||
// using (MultipartContent content = new MultipartContent("ascasc"))
|
||||
|
||||
using (var formdata = new MultipartFormDataContent("NKdKd9Yk"))
|
||||
{
|
||||
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
|
||||
using (Stream requestStream = request.EndGetRequestStream(result))
|
||||
using (HttpClient client = new HttpClient())
|
||||
{
|
||||
WriteMultipartForm(requestStream, boundary, data, fi, fileContentType);
|
||||
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
|
||||
client.DefaultRequestHeaders.Add("X-NuGet-Client-Version", clientVersion);
|
||||
client.DefaultRequestHeaders.Add("X-NuGet-ApiKey", apikey);
|
||||
var dispo = new ContentDispositionHeaderValue("file");
|
||||
dispo.FileName = fi.Name;
|
||||
dispo.CreationDate = fi.CreationTime;
|
||||
dispo.DispositionType = "form-data";
|
||||
dispo.Size = fi.Length;
|
||||
dispo.ModificationDate = fi.LastAccessTime;
|
||||
|
||||
Stream fileStream = fi.OpenRead();
|
||||
var streamcontent = new StreamContent(fileStream);
|
||||
streamcontent.Headers.ContentDisposition = dispo;
|
||||
formdata.Add(streamcontent, "file", fi.Name);
|
||||
|
||||
// content.Add(formdata);
|
||||
|
||||
var response = await client.PutAsync(uri, formdata);
|
||||
response.EnsureSuccessStatusCode();
|
||||
report.StatusCode = response.StatusCode.ToString();
|
||||
var respstream = await response.Content.ReadAsStreamAsync();
|
||||
var sr = new StreamReader(respstream);
|
||||
|
||||
report.Message = await sr.ReadToEndAsync();
|
||||
}
|
||||
request.BeginGetResponse(a =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = request.EndGetResponse(a);
|
||||
var responseStream = response.GetResponseStream();
|
||||
using (var sr = new StreamReader(responseStream))
|
||||
{
|
||||
using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
|
||||
{
|
||||
string responseString = streamReader.ReadToEnd();
|
||||
//responseString is depend upon your web service.
|
||||
if (responseString == "Success")
|
||||
{
|
||||
report.Message = "stored successfully on server.";
|
||||
}
|
||||
else
|
||||
{
|
||||
report.Message = "Error occurred while uploading packet on server.";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
report.Message = ex.Message;
|
||||
Console.Error.WriteLine(ex.Message);
|
||||
}
|
||||
}, null);
|
||||
}
|
||||
catch (Exception rex)
|
||||
{
|
||||
report.Message = rex.Message;
|
||||
Console.Error.WriteLine(rex.Message);
|
||||
}
|
||||
}, httpWebRequest);
|
||||
httpWebRequest.GetResponse();
|
||||
}
|
||||
|
||||
const int MAXSENDLEN = 65636;
|
||||
|
||||
/// <summary>
|
||||
/// Writes multi part HTTP POST request. Author : Farhan Ghumra
|
||||
/// </summary>
|
||||
public static void WriteMultipartForm(this Stream s, string boundary, Dictionary<string, string> 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
|
||||
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;
|
||||
if (data != null)
|
||||
|
||||
}
|
||||
catch (Exception rex)
|
||||
{
|
||||
foreach (string key in data.Keys)
|
||||
{
|
||||
/// if we need to drop a CRLF, do that.
|
||||
if (bNeedsCRLF)
|
||||
WriteToStream(s, "\r\n");
|
||||
|
||||
/// Write the boundary.
|
||||
WriteToStream(s, boundarybytes, boundarybytes.Length);
|
||||
|
||||
/// Write the key.
|
||||
WriteToStream(s, string.Format(formdataTemplate, key, data[key]));
|
||||
bNeedsCRLF = true;
|
||||
}
|
||||
report.Message = rex.Message;
|
||||
report.StatusCode = "internal error";
|
||||
Console.Error.WriteLine(rex.Message);
|
||||
}
|
||||
|
||||
/// If we don't have keys, we don't need a crlf.
|
||||
if (bNeedsCRLF)
|
||||
WriteToStream(s, "\r\n");
|
||||
|
||||
WriteToStream(s, boundarybytes, boundarybytes.Length);
|
||||
WriteToStream(s, string.Format(fileheaderTemplate, "file", fi.Name, fileContentType));
|
||||
/// Write the file data to the stream.
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes string to stream. Author : Farhan Ghumra
|
||||
/// </summary>
|
||||
private static void WriteToStream(Stream s, string txt)
|
||||
{
|
||||
byte[] bytes = Encoding.UTF8.GetBytes(txt);
|
||||
s.Write(bytes, 0, bytes.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes byte array to stream. Author : Farhan Ghumra
|
||||
/// </summary>
|
||||
private static void WriteToStream(Stream s, byte[] bytes, int len)
|
||||
{
|
||||
s.Write(bytes, 0, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -78,7 +78,7 @@ namespace nuget_cli
|
||||
pushoptions.WriteOptionDescriptions(Console.Out);
|
||||
return;
|
||||
}
|
||||
var reports = await PushPkgAsync(pargs);
|
||||
List<PushReport> reports = await PushPkgAsync(pargs);
|
||||
Console.WriteLine(JsonConvert.SerializeObject(reports));
|
||||
}
|
||||
};
|
||||
|
@ -9,44 +9,39 @@ namespace nuget_cli
|
||||
{
|
||||
internal class PushCommand
|
||||
{
|
||||
private static readonly int MAXSENDLEN = 0xffff;
|
||||
|
||||
static internal async Task<PushReport> RunAsync(string pkg, string source, string apikey)
|
||||
{
|
||||
FileInfo fi = new FileInfo(pkg);
|
||||
var report = new PushReport
|
||||
{
|
||||
PkgName = pkg
|
||||
PkgName = fi.Name
|
||||
};
|
||||
FileInfo fi = new FileInfo(pkg);
|
||||
if (!fi.Exists)
|
||||
throw new Exception("Le fichier n'existe pas");
|
||||
var fparams = new Dictionary<string, string> { };
|
||||
{
|
||||
report.StatusCode = "local";
|
||||
report.Message = "Le fichier n'existe pas";
|
||||
return report;
|
||||
}
|
||||
|
||||
|
||||
try
|
||||
try
|
||||
{
|
||||
await report.UploadFilesToServer(new Uri(source), fi, apikey);
|
||||
}
|
||||
catch (WebException ex)
|
||||
{
|
||||
await Console.Error.WriteLineAsync(ex.Message);
|
||||
report.StatusCode = ex.Status.ToString();
|
||||
using (var respStream = ex.Response.GetResponseStream())
|
||||
{
|
||||
|
||||
report.UploadFilesToServer(new Uri(source),
|
||||
fparams, fi, "application/octet-stream",
|
||||
apikey);
|
||||
|
||||
}
|
||||
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);
|
||||
throw;
|
||||
StreamReader sr = new StreamReader(respStream);
|
||||
report.Message = sr.ReadToEnd();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await Console.Error.WriteLineAsync(ex.Message);
|
||||
throw;
|
||||
}
|
||||
report.Executed = true;
|
||||
return report;
|
||||
}
|
||||
|
@ -1,11 +1,11 @@
|
||||
namespace nuget_cli
|
||||
{
|
||||
internal class PushReport
|
||||
public class PushReport
|
||||
{
|
||||
internal string PkgName { get; set; }
|
||||
internal bool Executed { get; set; }
|
||||
internal bool AlreadyPresent { get; set; }
|
||||
internal string Message { get; set; }
|
||||
internal string StatusCode { get; set; }
|
||||
public string PkgName { get; set; }
|
||||
public bool Executed { get; set; }
|
||||
public bool AlreadyPresent { get; set; }
|
||||
public string Message { get; set; }
|
||||
public string StatusCode { get; set; }
|
||||
}
|
||||
}
|
@ -20,7 +20,7 @@ namespace nuget.host.tests
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task TestHaveTestDbContext()
|
||||
public void TestHaveTestDbContext()
|
||||
{
|
||||
string envVar = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
|
||||
Assert.Equal("Development", envVar);
|
||||
|
@ -8,8 +8,8 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.App" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.0" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
|
||||
<PackageReference Include="coverlet.collector" Version="1.2.0" />
|
||||
|
Reference in New Issue
Block a user