Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.10 KB | None | 0 0
  1. public bool UploadChunks(string file, string pfsHash)
  2. {
  3. int val = 8;
  4. int num = 1048576;
  5. string text = this.Game.LocalSaveFolder.Substring(0, this.Game.LocalSaveFolder.Length - 4);
  6. string fileName = Path.GetFileName(text);
  7. if (string.IsNullOrEmpty(pfsHash))
  8. {
  9. pfsHash = Util.GetHash(text);
  10. }
  11. List<int> list = new List<int>();
  12. string str = "---------------------------" + DateTime.Now.Ticks.ToString("x");
  13. byte[] bytes = Encoding.ASCII.GetBytes("\r\n--" + str + "\r\n");
  14. int num2 = 0;
  15. bool result;
  16. using (FileStream fileStream = File.Open(file, FileMode.Open))
  17. {
  18. int num3 = (int)Math.Ceiling((double)fileStream.Length / (double)num);
  19. long num4 = 0L;
  20. long length = fileStream.Length;
  21. long num5 = length;
  22. bool flag = true;
  23. this.SetProgress(0, new int?(60));
  24. for (;;)
  25. {
  26. try
  27. {
  28. if (this.AbortEvent.WaitOne(0))
  29. {
  30. fileStream.Close();
  31. result = false;
  32. break;
  33. }
  34. HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(Util.GetBaseUrl() + "/chunk_upload?token=" + Util.GetAuthToken());
  35. httpWebRequest.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
  36. httpWebRequest.AllowWriteStreamBuffering = true;
  37. httpWebRequest.PreAuthenticate = true;
  38. httpWebRequest.ContentType = "multipart/form-data; boundary=" + str;
  39. httpWebRequest.Method = "POST";
  40. httpWebRequest.UserAgent = Util.GetUserAgent();
  41. httpWebRequest.ProtocolVersion = new Version(1, 1);
  42. httpWebRequest.KeepAlive = true;
  43. ServicePointManager.Expect100Continue = false;
  44. httpWebRequest.Credentials = Util.GetNetworkCredential();
  45. httpWebRequest.Timeout = 600000;
  46. httpWebRequest.ReadWriteTimeout = 600000;
  47. httpWebRequest.SendChunked = true;
  48. string value = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(Util.GetHtaccessUser() + ":" + Util.GetHtaccessPwd()));
  49. httpWebRequest.AuthenticationLevel = AuthenticationLevel.MutualAuthRequested;
  50. httpWebRequest.Headers.Add("Authorization", value);
  51. string format = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
  52. long num6 = 0L;
  53. num6 += (long)bytes.Length;
  54. string s = string.Format(format, "form_id", "chunk_upload_form");
  55. byte[] bytes2 = Encoding.UTF8.GetBytes(s);
  56. num6 += (long)bytes2.Length;
  57. num6 += (long)bytes.Length;
  58. s = string.Format(format, "op", "Submit");
  59. bytes2 = Encoding.UTF8.GetBytes(s);
  60. num6 += (long)bytes2.Length;
  61. num6 += (long)bytes.Length;
  62. s = string.Format(format, "pfs_md5", pfsHash);
  63. bytes2 = Encoding.UTF8.GetBytes(s);
  64. num6 += (long)bytes2.Length;
  65. num6 += (long)bytes.Length;
  66. s = string.Format(format, "total_chunks", num3);
  67. bytes2 = Encoding.UTF8.GetBytes(s);
  68. num6 += (long)bytes2.Length;
  69. num6 += (long)bytes.Length;
  70. s = string.Format(format, "gamecode", this.Game.id);
  71. bytes2 = Encoding.UTF8.GetBytes(s);
  72. num6 += (long)bytes2.Length;
  73. if (!string.IsNullOrEmpty(this.Game.diskcode))
  74. {
  75. num6 += (long)bytes.Length;
  76. s = string.Format(format, "diskcode", this.Game.diskcode);
  77. bytes2 = Encoding.UTF8.GetBytes(s);
  78. num6 += (long)bytes2.Length;
  79. }
  80. num6 += (long)bytes.Length;
  81. s = string.Format(format, "pfs", fileName);
  82. bytes2 = Encoding.UTF8.GetBytes(s);
  83. num6 += (long)bytes2.Length;
  84. num6 += (long)bytes.Length;
  85. s = string.Format(format, "pfs_size", length);
  86. bytes2 = Encoding.UTF8.GetBytes(s);
  87. num6 += (long)bytes2.Length;
  88. Dictionary<int, byte[]> dictionary = new Dictionary<int, byte[]>();
  89. if (!flag)
  90. {
  91. int i = 0;
  92. int num7 = 1;
  93. while (i < Math.Min(list.Count, val))
  94. {
  95. byte[] array = new byte[num];
  96. fileStream.Seek((long)((list[i] - 1) * num), SeekOrigin.Begin);
  97. int num8 = fileStream.Read(array, 0, num);
  98. if (num8 < num)
  99. {
  100. byte[] array2 = new byte[num8];
  101. Array.Copy(array, array2, num8);
  102. array = array2;
  103. }
  104. dictionary.Add(list[i], array);
  105. string hash = Util.GetHash(array);
  106. num6 += (long)bytes.Length;
  107. s = string.Format(format, "chunk" + num7 + "_md5", hash);
  108. bytes2 = Encoding.UTF8.GetBytes(s);
  109. num6 += (long)bytes2.Length;
  110. num6 += (long)bytes.Length;
  111. s = string.Format(format, "chunk" + num7 + "id", string.Concat(list[i]));
  112. bytes2 = Encoding.UTF8.GetBytes(s);
  113. num6 += (long)bytes2.Length;
  114. num6 += (long)bytes.Length;
  115. string s2 = string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n", "files[chunk" + num7 + "]", "chunk" + list[i], "application/octet-stream");
  116. byte[] bytes3 = Encoding.UTF8.GetBytes(s2);
  117. num6 += (long)bytes3.Length;
  118. num6 += (long)num8;
  119. i++;
  120. num7++;
  121. }
  122. }
  123. if (!flag && dictionary.Count == 0)
  124. {
  125. result = false;
  126. break;
  127. }
  128. byte[] bytes4 = Encoding.ASCII.GetBytes("\r\n--" + str + "--\r\n");
  129. num6 += (long)bytes4.Length;
  130. httpWebRequest.ContentLength = num6;
  131. Stream requestStream = httpWebRequest.GetRequestStream();
  132. format = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
  133. if (!flag)
  134. {
  135. int j = 0;
  136. int num9 = 1;
  137. while (j < Math.Min(list.Count, val))
  138. {
  139. requestStream.Write(bytes, 0, bytes.Length);
  140. byte[] array3 = dictionary[list[j]];
  141. int num10 = array3.Length;
  142. string hash2 = Util.GetHash(array3);
  143. s = string.Format(format, "chunk" + num9 + "_md5", hash2);
  144. bytes2 = Encoding.UTF8.GetBytes(s);
  145. requestStream.Write(bytes2, 0, bytes2.Length);
  146. requestStream.Write(bytes, 0, bytes.Length);
  147. s = string.Format(format, "chunk" + num9 + "id", string.Concat(list[j]));
  148. bytes2 = Encoding.UTF8.GetBytes(s);
  149. requestStream.Write(bytes2, 0, bytes2.Length);
  150. string s3 = string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n", "files[chunk" + num9 + "]", "chunk" + list[j], "application/octet-stream");
  151. requestStream.Write(bytes, 0, bytes.Length);
  152. byte[] bytes5 = Encoding.UTF8.GetBytes(s3);
  153. requestStream.Write(bytes5, 0, bytes5.Length);
  154. requestStream.Write(array3, 0, num10);
  155. num4 += (long)num10;
  156. int val2 = Math.Min(100, (int)(num4 * 100L / num5));
  157. this.SetProgress(val2, null);
  158. j++;
  159. num9++;
  160. }
  161. }
  162. requestStream.Write(bytes, 0, bytes.Length);
  163. s = string.Format(format, "op", "Submit");
  164. bytes2 = Encoding.UTF8.GetBytes(s);
  165. requestStream.Write(bytes2, 0, bytes2.Length);
  166. requestStream.Write(bytes, 0, bytes.Length);
  167. s = string.Format(format, "form_id", "chunk_upload_form");
  168. bytes2 = Encoding.UTF8.GetBytes(s);
  169. requestStream.Write(bytes2, 0, bytes2.Length);
  170. requestStream.Write(bytes, 0, bytes.Length);
  171. s = string.Format(format, "pfs_md5", pfsHash);
  172. bytes2 = Encoding.UTF8.GetBytes(s);
  173. requestStream.Write(bytes2, 0, bytes2.Length);
  174. requestStream.Write(bytes, 0, bytes.Length);
  175. s = string.Format(format, "total_chunks", num3);
  176. bytes2 = Encoding.UTF8.GetBytes(s);
  177. requestStream.Write(bytes2, 0, bytes2.Length);
  178. requestStream.Write(bytes, 0, bytes.Length);
  179. s = string.Format(format, "gamecode", this.Game.id);
  180. bytes2 = Encoding.UTF8.GetBytes(s);
  181. requestStream.Write(bytes2, 0, bytes2.Length);
  182. if (!string.IsNullOrEmpty(this.Game.diskcode))
  183. {
  184. requestStream.Write(bytes, 0, bytes.Length);
  185. s = string.Format(format, "diskcode", this.Game.diskcode);
  186. bytes2 = Encoding.UTF8.GetBytes(s);
  187. requestStream.Write(bytes2, 0, bytes2.Length);
  188. }
  189. requestStream.Write(bytes, 0, bytes.Length);
  190. s = string.Format(format, "pfs", fileName);
  191. bytes2 = Encoding.UTF8.GetBytes(s);
  192. requestStream.Write(bytes2, 0, bytes2.Length);
  193. requestStream.Write(bytes, 0, bytes.Length);
  194. s = string.Format(format, "pfs_size", length);
  195. bytes2 = Encoding.UTF8.GetBytes(s);
  196. requestStream.Write(bytes2, 0, bytes2.Length);
  197. bytes4 = Encoding.ASCII.GetBytes("\r\n--" + str + "--\r\n");
  198. requestStream.Write(bytes4, 0, bytes4.Length);
  199. requestStream.Close();
  200. HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
  201. if (httpWebResponse.StatusCode == HttpStatusCode.OK)
  202. {
  203. using (Stream responseStream = httpWebResponse.GetResponseStream())
  204. {
  205. using (StreamReader streamReader = new StreamReader(responseStream))
  206. {
  207. long contentLength = httpWebResponse.ContentLength;
  208. string text2 = streamReader.ReadToEnd();
  209. if (text2.IndexOf("true") > 0)
  210. {
  211. httpWebResponse.Close();
  212. requestStream.Dispose();
  213. result = true;
  214. break;
  215. }
  216. Dictionary<string, object> dictionary2 = new JavaScriptSerializer().Deserialize(text2, typeof(Dictionary<string, object>)) as Dictionary<string, object>;
  217. if (dictionary2.ContainsKey("remaining_chunks"))
  218. {
  219. Dictionary<string, object> dictionary3 = dictionary2["remaining_chunks"] as Dictionary<string, object>;
  220. list = new List<int>();
  221. foreach (string text3 in dictionary3.Keys)
  222. {
  223. if (!(bool)dictionary3[text3])
  224. {
  225. list.Add(int.Parse(text3));
  226. }
  227. }
  228. if (list.Count == 0)
  229. {
  230. result = false;
  231. break;
  232. }
  233. }
  234. if (dictionary2.ContainsKey("status") && (string)dictionary2["status"] == "ERROR")
  235. {
  236. Util.ShowErrorMessage(dictionary2, Resources.errUnknown);
  237. this.RaiseDownloadFinishEvent(false, "Abort");
  238. result = false;
  239. break;
  240. }
  241. }
  242. }
  243. httpWebResponse.Close();
  244. requestStream.Dispose();
  245. if (flag)
  246. {
  247. num5 = (long)(list.Count * num);
  248. flag = false;
  249. }
  250. }
  251. else
  252. {
  253. num2++;
  254. if (num2 > 3)
  255. {
  256. result = false;
  257. break;
  258. }
  259. }
  260. }
  261. catch (Exception)
  262. {
  263. num2++;
  264. if (num2 > 3)
  265. {
  266. SaveUploadDownloder.ErrorMessage(base.ParentForm, Resources.errUnknown, null);
  267. this.RaiseDownloadFinishEvent(false, "Abort");
  268. result = false;
  269. break;
  270. }
  271. }
  272. }
  273. }
  274. return result;
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement