public static CommitUploadResult SimpleUpload(string localpath, string remotepath, Credential credential, string host = "c.pcs.baidu.com") { try { var size = new FileInfo(localpath).Length; var mtime = (long)(new FileInfo(localpath).LastAccessTime.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; var md5 = UploadHelper.GetMD5HashFromFile(localpath); var str = "path=" + remotepath + "&size=" + size + "&isdir=0&block_list=[\"" + md5 + "\"]&autoinit=1&local_mtime=" + mtime + "&method=post"; using (var wc = new PatientWebClient()) { wc.Headers.Add(HttpRequestHeader.Cookie, credential); wc.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded"); var res = wc.UploadData("http://pan.baidu.com/api/precreate?clienttype=8", Encoding.UTF8.GetBytes(str)); var obj = JsonConvert.DeserializeObject <InitUploadResult>(Encoding.UTF8.GetString(res)); if (obj.errno != 0) { throw new Exception("precreate had errno = " + obj.errno); } var boundary = GetBoundary(); wc.Headers.Add(HttpRequestHeader.ContentType, "multipart/form-data; boundary=" + boundary); str = "--" + boundary + "\r\nContent-Disposition: form-data; name=\"filename\"; filename=\"name\"\r\nContent-Type: application/octet-stream\r\n\r\n"; var str2 = "\r\n--" + boundary + "--\r\n"; var data = Encoding.UTF8.GetBytes(str).Concat(File.ReadAllBytes(localpath)).Concat(Encoding.UTF8.GetBytes(str2)).ToArray(); res = wc.UploadData("http://" + host + "/rest/2.0/pcs/superfile2?app_id=250528&method=upload&path=" + Uri.EscapeDataString(remotepath) + "&uploadid=" + Uri.EscapeDataString(obj.uploadid) + "&partseq=0&partoffset=0", data); str = "path=" + remotepath + "&size=" + size + "&isdir=0&uploadid=" + Uri.EscapeDataString(obj.uploadid) + "&block_list=[\"" + md5 + "\"]&method=post&rtype=2&sequence=1&mode=1&local_mtime=" + mtime; res = wc.UploadData("http://pan.baidu.com/api/create?a=commit&clienttype=8", Encoding.UTF8.GetBytes(str)); var obj2 = JsonConvert.DeserializeObject <CommitUploadResult>(Encoding.UTF8.GetString(res)); obj2.success = true; return(obj2); } } catch (Exception ex) { return(new CommitUploadResult() { exception = ex }); } }
public static Result UploadBlock(FileProperty prop, string path, InitUploadResult session, FileStream stream, int blockid, string host, Credential credential) { try { using (var wc = new PatientWebClient()) { var boundary = GetBoundary(); wc.Headers.Add(HttpRequestHeader.Cookie, credential); wc.Headers.Add(HttpRequestHeader.ContentType, "multipart/form-data; boundary=" + boundary); var str = "--" + boundary + "\r\nContent-Disposition: form-data; name=\"filename\"; filename=\"name\"\r\nContent-Type: application/octet-stream\r\n\r\n"; var str2 = "\r\n--" + boundary + "--\r\n"; stream.Seek((long)blockid * 4 * 1024 * 1024, SeekOrigin.Begin); var fdata = new byte[4 * 1024 * 1024]; var len = stream.Read(fdata, 0, fdata.Length); if (len < fdata.Length) { var arr = new byte[len]; Array.Copy(fdata, arr, len); fdata = arr; } var data = Encoding.UTF8.GetBytes(str).Concat(fdata).Concat(Encoding.UTF8.GetBytes(str2)).ToArray(); var res = wc.UploadData("http://" + host + "/rest/2.0/pcs/superfile2?app_id=250528&method=upload&path=" + Uri.EscapeDataString(path) + "&uploadid=" + Uri.EscapeDataString(session.uploadid) + "&partseq=" + blockid + "&partoffset=" + (long)blockid * 4 * 1024 * 1024, data); var obj = JsonConvert.DeserializeObject <SuperFileResponse>(Encoding.UTF8.GetString(res)); if (obj.md5 != prop.blocks[blockid]) { throw new Exception("MD5 mismatch."); } return(new Result() { success = true }); } } catch (Exception ex) { return(new Result() { exception = ex }); } }
private static void PublishModInner(TmodFile modFile, BuildProperties bp, bool commandLine = false) { var files = new List <UploadFile>(); files.Add(new UploadFile { Name = "file", Filename = Path.GetFileName(modFile.path), // ContentType = "text/plain", Content = File.ReadAllBytes(modFile.path) }); if (modFile.HasFile("icon.png")) // Test this on server { using (modFile.Open()) files.Add(new UploadFile { Name = "iconfile", Filename = "icon.png", Content = modFile.GetBytes("icon.png") }); } //if (bp.beta) // throw new WebException(Language.GetTextValue("tModLoader.BetaModCantPublishError")); if (bp.buildVersion != modFile.TModLoaderVersion) { throw new WebException(Language.GetTextValue("OutdatedModCantPublishError.BetaModCantPublishError")); } var values = new NameValueCollection { { "displayname", bp.displayName }, { "displaynameclean", string.Join("", ChatManager.ParseMessage(bp.displayName, Color.White).Where(x => x.GetType() == typeof(TextSnippet)).Select(x => x.Text)) }, { "name", modFile.Name }, { "version", "v" + bp.version }, { "author", bp.author }, { "homepage", bp.homepage }, { "description", bp.description }, { "steamid64", ModLoader.SteamID64 }, { "modloaderversion", "tModLoader v" + modFile.TModLoaderVersion }, { "passphrase", ModLoader.modBrowserPassphrase }, { "modreferences", String.Join(", ", bp.modReferences.Select(x => x.mod)) }, { "modside", bp.side.ToFriendlyString() }, }; if (values["steamid64"].Length != 17) { throw new WebException($"The steamid64 '{values["steamid64"]}' is invalid, verify that you are logged into Steam and don't have a pirated copy of Terraria."); } if (string.IsNullOrEmpty(values["author"])) { throw new WebException($"You need to specify an author in build.txt"); } ServicePointManager.Expect100Continue = false; string url = "http://javid.ddns.net/tModLoader/publishmod.php"; using (PatientWebClient client = new PatientWebClient()) { ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, policyErrors) => true; Interface.progress.Show(displayText: $"Uploading: {modFile.Name}", gotoMenu: Interface.modSourcesID, cancel: client.CancelAsync); var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", System.Globalization.NumberFormatInfo.InvariantInfo); client.Headers["Content-Type"] = "multipart/form-data; boundary=" + boundary; //boundary = "--" + boundary; byte[] data = UploadFile.GetUploadFilesRequestData(files, values, boundary); if (commandLine) { var result = client.UploadData(new Uri(url), data); // could use async version for progress output maybe string response = HandlePublishResponse(modFile, result); Console.WriteLine(Language.GetTextValue("tModLoader.MBServerResponse", response)); if (result.Length <= 256 || result[result.Length - 256 - 1] != '~') { throw new Exception("Publish failed due to invalid response from server"); } } else { client.UploadDataCompleted += (s, e) => PublishUploadDataComplete(s, e, modFile); client.UploadProgressChanged += (s, e) => Interface.progress.Progress = (float)e.BytesSent / e.TotalBytesToSend; client.UploadDataAsync(new Uri(url), data); } } }