FirmwareUploadProcedure.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using AwInitilizer.Model;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Web;
  12. namespace AwInitilizer.Procedure
  13. {
  14. public class FirmwareUploadProcedure : ProcedureBase
  15. {
  16. internal string Version;
  17. internal string fileName;
  18. internal string module;
  19. private FirmwareUpdateModel _model;
  20. public FirmwareUploadProcedure(FirmwareUpdateModel model) : base()
  21. {
  22. _model = model;
  23. Name = string.Format("Firmware {0} upload", model.Module);
  24. Content = string.Format("Update {0} Firemware", model.Module);
  25. Version = model.Version;
  26. fileName = model.FirmwareFileName;
  27. module = model.Module;
  28. }
  29. internal override async Task<bool> Run()
  30. {
  31. Logger.Print("Firmware Uploading...");
  32. var uploadResult = await Uploadfirmware(fileName);
  33. LogPair.Add($"{module}Upload", uploadResult?"1":"0");
  34. return uploadResult;
  35. }
  36. internal async Task<bool> Uploadfirmware(string fileName)
  37. {
  38. try
  39. {
  40. //using (var stream = File.Open(fileName, FileMode.Open))
  41. //{
  42. // UploadFileAsync(
  43. // new NameValueCollection()
  44. // {
  45. // {"fw_tag","iso" }
  46. // },
  47. // new UploadFile()
  48. // {
  49. // Name = "file",
  50. // Filename = Path.GetFileName(fileName),
  51. // Stream = stream
  52. // }
  53. // );
  54. //}
  55. //return true;
  56. using (var stream = File.Open(fileName, FileMode.Open))
  57. {
  58. var response = await UploadFiles(
  59. $"https://{ServerIpAddress}/upgrade_iso_action.php",
  60. new List<UploadFile>() {
  61. new UploadFile()
  62. {
  63. Name="file",
  64. Filename= Path.GetFileName(fileName),
  65. Stream = stream
  66. }
  67. },
  68. new NameValueCollection() {
  69. {"fw_tag","iso" }
  70. }
  71. );
  72. var responseStr = Encoding.ASCII.GetString(response).ToLower();
  73. InfoLog += $"get firmware update response {responseStr}\n";
  74. if (responseStr.Contains("file is uploaded"))
  75. return true;
  76. return false;
  77. }
  78. return true;
  79. //using (WebClient webClient = new WebClient())
  80. //{
  81. // NameValueCollection parameters = new NameValueCollection();
  82. // parameters.Add("fw_tag", "iso");
  83. // webClient.QueryString = parameters;
  84. // var responseBytes = await webClient.UploadFileTaskAsync($"https://{ServerIpAddress}/upgrade_iso_action.php", fileName);
  85. // string responseString = Encoding.ASCII.GetString(responseBytes);
  86. // return true;
  87. //}
  88. }
  89. catch (Exception e)
  90. {
  91. Logger.Print("Upload Firmware Failed", isError: true);
  92. Logger.Print(e.Message + "", isError: true);
  93. InfoLog += "Upload Firmware Failed\n";
  94. InfoLog += e.Message;
  95. InfoLog += "\n";
  96. return false;
  97. }
  98. }
  99. public async Task<byte[]> UploadFiles(string address, IEnumerable<UploadFile> files, NameValueCollection values)
  100. {
  101. var request = (HttpWebRequest)HttpWebRequest.Create(address);
  102. //request.Timeout = 10 * 1000;
  103. request.KeepAlive = true;
  104. request.Accept = "*/*";
  105. request.Method = "POST";
  106. request.Referer = address;
  107. request.Expect = "";
  108. var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
  109. request.ContentType = "multipart/form-data; boundary=" + boundary;
  110. boundary = "--" + boundary;
  111. using (var requestStream = request.GetRequestStream())
  112. {
  113. // Write the values
  114. foreach (string name in values.Keys)
  115. {
  116. var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
  117. requestStream.Write(buffer, 0, buffer.Length);
  118. buffer = Encoding.ASCII.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"{1}{1}", name, Environment.NewLine));
  119. requestStream.Write(buffer, 0, buffer.Length);
  120. buffer = Encoding.UTF8.GetBytes(values[name] + Environment.NewLine);
  121. requestStream.Write(buffer, 0, buffer.Length);
  122. }
  123. // Write the files
  124. foreach (var file in files)
  125. {
  126. var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
  127. requestStream.Write(buffer, 0, buffer.Length);
  128. buffer = Encoding.UTF8.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", file.Name, file.Filename, Environment.NewLine));
  129. requestStream.Write(buffer, 0, buffer.Length);
  130. var ctype = MimeMapping.GetMimeMapping(file.Filename);
  131. buffer = Encoding.ASCII.GetBytes(string.Format("Content-Type: {0}{1}{1}", MimeMapping.GetMimeMapping(file.Filename), Environment.NewLine));
  132. requestStream.Write(buffer, 0, buffer.Length);
  133. file.Stream.CopyTo(requestStream);
  134. buffer = Encoding.ASCII.GetBytes(Environment.NewLine);
  135. requestStream.Write(buffer, 0, buffer.Length);
  136. }
  137. var boundaryBuffer = Encoding.ASCII.GetBytes(boundary + "--");
  138. requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length);
  139. }
  140. using (var response = await request.GetResponseAsync())
  141. using (var responseStream = response.GetResponseStream())
  142. using (var stream = new MemoryStream())
  143. {
  144. responseStream.CopyTo(stream);
  145. return stream.ToArray();
  146. }
  147. }
  148. }
  149. }