Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public bool UploadChunks(string file, string pfsHash)
- {
- int val = 8;
- int num = 1048576;
- string text = this.Game.LocalSaveFolder.Substring(0, this.Game.LocalSaveFolder.Length - 4);
- string fileName = Path.GetFileName(text);
- if (string.IsNullOrEmpty(pfsHash))
- {
- pfsHash = Util.GetHash(text);
- }
- List<int> list = new List<int>();
- string str = "---------------------------" + DateTime.Now.Ticks.ToString("x");
- byte[] bytes = Encoding.ASCII.GetBytes("\r\n--" + str + "\r\n");
- int num2 = 0;
- bool result;
- using (FileStream fileStream = File.Open(file, FileMode.Open))
- {
- int num3 = (int)Math.Ceiling((double)fileStream.Length / (double)num);
- long num4 = 0L;
- long length = fileStream.Length;
- long num5 = length;
- bool flag = true;
- this.SetProgress(0, new int?(60));
- for (;;)
- {
- try
- {
- if (this.AbortEvent.WaitOne(0))
- {
- fileStream.Close();
- result = false;
- break;
- }
- HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(Util.GetBaseUrl() + "/chunk_upload?token=" + Util.GetAuthToken());
- httpWebRequest.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
- httpWebRequest.AllowWriteStreamBuffering = true;
- httpWebRequest.PreAuthenticate = true;
- httpWebRequest.ContentType = "multipart/form-data; boundary=" + str;
- httpWebRequest.Method = "POST";
- httpWebRequest.UserAgent = Util.GetUserAgent();
- httpWebRequest.ProtocolVersion = new Version(1, 1);
- httpWebRequest.KeepAlive = true;
- ServicePointManager.Expect100Continue = false;
- httpWebRequest.Credentials = Util.GetNetworkCredential();
- httpWebRequest.Timeout = 600000;
- httpWebRequest.ReadWriteTimeout = 600000;
- httpWebRequest.SendChunked = true;
- string value = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(Util.GetHtaccessUser() + ":" + Util.GetHtaccessPwd()));
- httpWebRequest.AuthenticationLevel = AuthenticationLevel.MutualAuthRequested;
- httpWebRequest.Headers.Add("Authorization", value);
- string format = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
- long num6 = 0L;
- num6 += (long)bytes.Length;
- string s = string.Format(format, "form_id", "chunk_upload_form");
- byte[] bytes2 = Encoding.UTF8.GetBytes(s);
- num6 += (long)bytes2.Length;
- num6 += (long)bytes.Length;
- s = string.Format(format, "op", "Submit");
- bytes2 = Encoding.UTF8.GetBytes(s);
- num6 += (long)bytes2.Length;
- num6 += (long)bytes.Length;
- s = string.Format(format, "pfs_md5", pfsHash);
- bytes2 = Encoding.UTF8.GetBytes(s);
- num6 += (long)bytes2.Length;
- num6 += (long)bytes.Length;
- s = string.Format(format, "total_chunks", num3);
- bytes2 = Encoding.UTF8.GetBytes(s);
- num6 += (long)bytes2.Length;
- num6 += (long)bytes.Length;
- s = string.Format(format, "gamecode", this.Game.id);
- bytes2 = Encoding.UTF8.GetBytes(s);
- num6 += (long)bytes2.Length;
- if (!string.IsNullOrEmpty(this.Game.diskcode))
- {
- num6 += (long)bytes.Length;
- s = string.Format(format, "diskcode", this.Game.diskcode);
- bytes2 = Encoding.UTF8.GetBytes(s);
- num6 += (long)bytes2.Length;
- }
- num6 += (long)bytes.Length;
- s = string.Format(format, "pfs", fileName);
- bytes2 = Encoding.UTF8.GetBytes(s);
- num6 += (long)bytes2.Length;
- num6 += (long)bytes.Length;
- s = string.Format(format, "pfs_size", length);
- bytes2 = Encoding.UTF8.GetBytes(s);
- num6 += (long)bytes2.Length;
- Dictionary<int, byte[]> dictionary = new Dictionary<int, byte[]>();
- if (!flag)
- {
- int i = 0;
- int num7 = 1;
- while (i < Math.Min(list.Count, val))
- {
- byte[] array = new byte[num];
- fileStream.Seek((long)((list[i] - 1) * num), SeekOrigin.Begin);
- int num8 = fileStream.Read(array, 0, num);
- if (num8 < num)
- {
- byte[] array2 = new byte[num8];
- Array.Copy(array, array2, num8);
- array = array2;
- }
- dictionary.Add(list[i], array);
- string hash = Util.GetHash(array);
- num6 += (long)bytes.Length;
- s = string.Format(format, "chunk" + num7 + "_md5", hash);
- bytes2 = Encoding.UTF8.GetBytes(s);
- num6 += (long)bytes2.Length;
- num6 += (long)bytes.Length;
- s = string.Format(format, "chunk" + num7 + "id", string.Concat(list[i]));
- bytes2 = Encoding.UTF8.GetBytes(s);
- num6 += (long)bytes2.Length;
- num6 += (long)bytes.Length;
- 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");
- byte[] bytes3 = Encoding.UTF8.GetBytes(s2);
- num6 += (long)bytes3.Length;
- num6 += (long)num8;
- i++;
- num7++;
- }
- }
- if (!flag && dictionary.Count == 0)
- {
- result = false;
- break;
- }
- byte[] bytes4 = Encoding.ASCII.GetBytes("\r\n--" + str + "--\r\n");
- num6 += (long)bytes4.Length;
- httpWebRequest.ContentLength = num6;
- Stream requestStream = httpWebRequest.GetRequestStream();
- format = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
- if (!flag)
- {
- int j = 0;
- int num9 = 1;
- while (j < Math.Min(list.Count, val))
- {
- requestStream.Write(bytes, 0, bytes.Length);
- byte[] array3 = dictionary[list[j]];
- int num10 = array3.Length;
- string hash2 = Util.GetHash(array3);
- s = string.Format(format, "chunk" + num9 + "_md5", hash2);
- bytes2 = Encoding.UTF8.GetBytes(s);
- requestStream.Write(bytes2, 0, bytes2.Length);
- requestStream.Write(bytes, 0, bytes.Length);
- s = string.Format(format, "chunk" + num9 + "id", string.Concat(list[j]));
- bytes2 = Encoding.UTF8.GetBytes(s);
- requestStream.Write(bytes2, 0, bytes2.Length);
- 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");
- requestStream.Write(bytes, 0, bytes.Length);
- byte[] bytes5 = Encoding.UTF8.GetBytes(s3);
- requestStream.Write(bytes5, 0, bytes5.Length);
- requestStream.Write(array3, 0, num10);
- num4 += (long)num10;
- int val2 = Math.Min(100, (int)(num4 * 100L / num5));
- this.SetProgress(val2, null);
- j++;
- num9++;
- }
- }
- requestStream.Write(bytes, 0, bytes.Length);
- s = string.Format(format, "op", "Submit");
- bytes2 = Encoding.UTF8.GetBytes(s);
- requestStream.Write(bytes2, 0, bytes2.Length);
- requestStream.Write(bytes, 0, bytes.Length);
- s = string.Format(format, "form_id", "chunk_upload_form");
- bytes2 = Encoding.UTF8.GetBytes(s);
- requestStream.Write(bytes2, 0, bytes2.Length);
- requestStream.Write(bytes, 0, bytes.Length);
- s = string.Format(format, "pfs_md5", pfsHash);
- bytes2 = Encoding.UTF8.GetBytes(s);
- requestStream.Write(bytes2, 0, bytes2.Length);
- requestStream.Write(bytes, 0, bytes.Length);
- s = string.Format(format, "total_chunks", num3);
- bytes2 = Encoding.UTF8.GetBytes(s);
- requestStream.Write(bytes2, 0, bytes2.Length);
- requestStream.Write(bytes, 0, bytes.Length);
- s = string.Format(format, "gamecode", this.Game.id);
- bytes2 = Encoding.UTF8.GetBytes(s);
- requestStream.Write(bytes2, 0, bytes2.Length);
- if (!string.IsNullOrEmpty(this.Game.diskcode))
- {
- requestStream.Write(bytes, 0, bytes.Length);
- s = string.Format(format, "diskcode", this.Game.diskcode);
- bytes2 = Encoding.UTF8.GetBytes(s);
- requestStream.Write(bytes2, 0, bytes2.Length);
- }
- requestStream.Write(bytes, 0, bytes.Length);
- s = string.Format(format, "pfs", fileName);
- bytes2 = Encoding.UTF8.GetBytes(s);
- requestStream.Write(bytes2, 0, bytes2.Length);
- requestStream.Write(bytes, 0, bytes.Length);
- s = string.Format(format, "pfs_size", length);
- bytes2 = Encoding.UTF8.GetBytes(s);
- requestStream.Write(bytes2, 0, bytes2.Length);
- bytes4 = Encoding.ASCII.GetBytes("\r\n--" + str + "--\r\n");
- requestStream.Write(bytes4, 0, bytes4.Length);
- requestStream.Close();
- HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
- if (httpWebResponse.StatusCode == HttpStatusCode.OK)
- {
- using (Stream responseStream = httpWebResponse.GetResponseStream())
- {
- using (StreamReader streamReader = new StreamReader(responseStream))
- {
- long contentLength = httpWebResponse.ContentLength;
- string text2 = streamReader.ReadToEnd();
- if (text2.IndexOf("true") > 0)
- {
- httpWebResponse.Close();
- requestStream.Dispose();
- result = true;
- break;
- }
- Dictionary<string, object> dictionary2 = new JavaScriptSerializer().Deserialize(text2, typeof(Dictionary<string, object>)) as Dictionary<string, object>;
- if (dictionary2.ContainsKey("remaining_chunks"))
- {
- Dictionary<string, object> dictionary3 = dictionary2["remaining_chunks"] as Dictionary<string, object>;
- list = new List<int>();
- foreach (string text3 in dictionary3.Keys)
- {
- if (!(bool)dictionary3[text3])
- {
- list.Add(int.Parse(text3));
- }
- }
- if (list.Count == 0)
- {
- result = false;
- break;
- }
- }
- if (dictionary2.ContainsKey("status") && (string)dictionary2["status"] == "ERROR")
- {
- Util.ShowErrorMessage(dictionary2, Resources.errUnknown);
- this.RaiseDownloadFinishEvent(false, "Abort");
- result = false;
- break;
- }
- }
- }
- httpWebResponse.Close();
- requestStream.Dispose();
- if (flag)
- {
- num5 = (long)(list.Count * num);
- flag = false;
- }
- }
- else
- {
- num2++;
- if (num2 > 3)
- {
- result = false;
- break;
- }
- }
- }
- catch (Exception)
- {
- num2++;
- if (num2 > 3)
- {
- SaveUploadDownloder.ErrorMessage(base.ParentForm, Resources.errUnknown, null);
- this.RaiseDownloadFinishEvent(false, "Abort");
- result = false;
- break;
- }
- }
- }
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement