Don't merge.
This commit is contained in:
parent
d117f26c77
commit
c83ff84370
6 changed files with 80 additions and 157 deletions
|
|
@ -2,147 +2,75 @@ using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace nuget_cli
|
namespace nuget_cli
|
||||||
{
|
{
|
||||||
public static class Helpers
|
public static class Helpers
|
||||||
{
|
{
|
||||||
static readonly string clientVersion = nameof(Program) + " v1.0";
|
static readonly string clientVersion = "nuget_cli v1.0";
|
||||||
|
|
||||||
/// <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, FileInfo fi, string fileContentType,
|
static internal async Task UploadFilesToServer(
|
||||||
string apikey)
|
this PushReport report, Uri uri,
|
||||||
|
FileInfo fi, string apikey)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
|
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
|
||||||
|
|
||||||
// "X-NuGet-ApiKey
|
// using (MultipartContent content = new MultipartContent("ascasc"))
|
||||||
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) =>
|
using (var formdata = new MultipartFormDataContent("NKdKd9Yk"))
|
||||||
{
|
{
|
||||||
try
|
using (HttpClient client = new HttpClient())
|
||||||
{
|
{
|
||||||
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
|
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||||
using (Stream requestStream = request.EndGetRequestStream(result))
|
|
||||||
{
|
client.DefaultRequestHeaders.Add("X-NuGet-Client-Version", clientVersion);
|
||||||
WriteMultipartForm(requestStream, boundary, data, fi, fileContentType);
|
client.DefaultRequestHeaders.Add("X-NuGet-ApiKey", apikey);
|
||||||
}
|
var dispo = new ContentDispositionHeaderValue("file");
|
||||||
request.BeginGetResponse(a =>
|
dispo.FileName = fi.Name;
|
||||||
{
|
dispo.CreationDate = fi.CreationTime;
|
||||||
try
|
dispo.DispositionType = "form-data";
|
||||||
{
|
dispo.Size = fi.Length;
|
||||||
var response = request.EndGetResponse(a);
|
dispo.ModificationDate = fi.LastAccessTime;
|
||||||
var responseStream = response.GetResponseStream();
|
|
||||||
using (var sr = new StreamReader(responseStream))
|
Stream fileStream = fi.OpenRead();
|
||||||
{
|
var streamcontent = new StreamContent(fileStream);
|
||||||
using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
|
streamcontent.Headers.ContentDisposition = dispo;
|
||||||
{
|
formdata.Add(streamcontent, "file", fi.Name);
|
||||||
string responseString = streamReader.ReadToEnd();
|
|
||||||
//responseString is depend upon your web service.
|
// content.Add(formdata);
|
||||||
if (responseString == "Success")
|
|
||||||
{
|
var response = await client.PutAsync(uri, formdata);
|
||||||
report.Message = "stored successfully on server.";
|
response.EnsureSuccessStatusCode();
|
||||||
}
|
report.StatusCode = response.StatusCode.ToString();
|
||||||
else
|
var respstream = await response.Content.ReadAsStreamAsync();
|
||||||
{
|
var sr = new StreamReader(respstream);
|
||||||
report.Message = "Error occurred while uploading packet on server.";
|
|
||||||
|
report.Message = await sr.ReadToEndAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
report.Message = ex.Message;
|
|
||||||
Console.Error.WriteLine(ex.Message);
|
|
||||||
}
|
|
||||||
}, null);
|
|
||||||
}
|
}
|
||||||
catch (Exception rex)
|
catch (Exception rex)
|
||||||
{
|
{
|
||||||
report.Message = rex.Message;
|
report.Message = rex.Message;
|
||||||
|
report.StatusCode = "internal error";
|
||||||
Console.Error.WriteLine(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)
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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);
|
pushoptions.WriteOptionDescriptions(Console.Out);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var reports = await PushPkgAsync(pargs);
|
List<PushReport> reports = await PushPkgAsync(pargs);
|
||||||
Console.WriteLine(JsonConvert.SerializeObject(reports));
|
Console.WriteLine(JsonConvert.SerializeObject(reports));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -9,27 +9,23 @@ namespace nuget_cli
|
||||||
{
|
{
|
||||||
internal class PushCommand
|
internal class PushCommand
|
||||||
{
|
{
|
||||||
private static readonly int MAXSENDLEN = 0xffff;
|
|
||||||
|
|
||||||
static internal async Task<PushReport> RunAsync(string pkg, string source, string apikey)
|
static internal async Task<PushReport> RunAsync(string pkg, string source, string apikey)
|
||||||
{
|
{
|
||||||
|
FileInfo fi = new FileInfo(pkg);
|
||||||
var report = new PushReport
|
var report = new PushReport
|
||||||
{
|
{
|
||||||
PkgName = pkg
|
PkgName = fi.Name
|
||||||
};
|
};
|
||||||
FileInfo fi = new FileInfo(pkg);
|
|
||||||
if (!fi.Exists)
|
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);
|
||||||
report.UploadFilesToServer(new Uri(source),
|
|
||||||
fparams, fi, "application/octet-stream",
|
|
||||||
apikey);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (WebException ex)
|
catch (WebException ex)
|
||||||
{
|
{
|
||||||
|
|
@ -37,7 +33,6 @@ namespace nuget_cli
|
||||||
report.StatusCode = ex.Status.ToString();
|
report.StatusCode = ex.Status.ToString();
|
||||||
using (var respStream = ex.Response.GetResponseStream())
|
using (var respStream = ex.Response.GetResponseStream())
|
||||||
{
|
{
|
||||||
|
|
||||||
StreamReader sr = new StreamReader(respStream);
|
StreamReader sr = new StreamReader(respStream);
|
||||||
report.Message = sr.ReadToEnd();
|
report.Message = sr.ReadToEnd();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
namespace nuget_cli
|
namespace nuget_cli
|
||||||
{
|
{
|
||||||
internal class PushReport
|
public class PushReport
|
||||||
{
|
{
|
||||||
internal string PkgName { get; set; }
|
public string PkgName { get; set; }
|
||||||
internal bool Executed { get; set; }
|
public bool Executed { get; set; }
|
||||||
internal bool AlreadyPresent { get; set; }
|
public bool AlreadyPresent { get; set; }
|
||||||
internal string Message { get; set; }
|
public string Message { get; set; }
|
||||||
internal string StatusCode { get; set; }
|
public string StatusCode { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -20,7 +20,7 @@ namespace nuget.host.tests
|
||||||
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task TestHaveTestDbContext()
|
public void TestHaveTestDbContext()
|
||||||
{
|
{
|
||||||
string envVar = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
|
string envVar = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
|
||||||
Assert.Equal("Development", envVar);
|
Assert.Equal("Development", envVar);
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,8 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
|
||||||
<PackageReference Include="Microsoft.AspNetCore.App" />
|
<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" Version="2.4.0" />
|
||||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
|
||||||
<PackageReference Include="coverlet.collector" Version="1.2.0" />
|
<PackageReference Include="coverlet.collector" Version="1.2.0" />
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue