using com.shephertz.app42.paas.sdk.csharp; using com.shephertz.app42.paas.sdk.csharp.abTest; using com.shephertz.app42.paas.sdk.csharp.achievement; using com.shephertz.app42.paas.sdk.csharp.avatar; using com.shephertz.app42.paas.sdk.csharp.bravoboard; using com.shephertz.app42.paas.sdk.csharp.buddy; using com.shephertz.app42.paas.sdk.csharp.email; using com.shephertz.app42.paas.sdk.csharp.gallery; using com.shephertz.app42.paas.sdk.csharp.game; using com.shephertz.app42.paas.sdk.csharp.geo; using com.shephertz.app42.paas.sdk.csharp.gift; using com.shephertz.app42.paas.sdk.csharp.imageProcessor; using com.shephertz.app42.paas.sdk.csharp.log; using com.shephertz.app42.paas.sdk.csharp.message; using com.shephertz.app42.paas.sdk.csharp.pushNotification; using com.shephertz.app42.paas.sdk.csharp.recommend; using com.shephertz.app42.paas.sdk.csharp.connection; using com.shephertz.app42.paas.sdk.csharp.review; using com.shephertz.app42.paas.sdk.csharp.reward; using com.shephertz.app42.paas.sdk.csharp.session; using com.shephertz.app42.paas.sdk.csharp.shopping; using com.shephertz.app42.paas.sdk.csharp.social; using com.shephertz.app42.paas.sdk.csharp.storage; using com.shephertz.app42.paas.sdk.csharp.timer; using com.shephertz.app42.paas.sdk.csharp.upload; using com.shephertz.app42.paas.sdk.csharp.user; using CSharpAssemble; using SimpleJSON; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using UnityEngine; using com.shephertz.app42.paas.sdk.csharp; using com.shephertz.app42.paas.sdk.csharp.connection; using com.shephertz.app42.paas.sdk.csharp.util; using SimpleJSON; using System.Collections.Generic; using System.IO; using System.Text; public static class App42UploadTest { static void ThrowExceptionIfNullOrBlank(object param, string paramName) { if (param == null) throw new App42Exception(paramName + " can not be null"); if (param is string && ((string) param).Trim().Equals("")) throw new App42Exception(paramName + " can not be blank"); } public static void UploadFile(UploadService service, string name, Stream inputStream, string fileType, string description, App42CallBack callBack) { ThrowExceptionIfNullOrBlank((object) name, "Name"); ThrowExceptionIfNullOrBlank((object) inputStream, "inputStream"); ThrowExceptionIfNullOrBlank((object) fileType, "FileType"); ThrowExceptionIfNullOrBlank((object) description, "Description"); Dictionary queryParams = new Dictionary(); Dictionary dictionary1 = service.PopulateSignParams(); Dictionary dictionary2 = service.PopulateMetaHeaderParams(); Dictionary dictionary3 = new Dictionary(); Dictionary headerParams = service.Merge(dictionary1, dictionary2); Dictionary postParams = new Dictionary(); postParams.Add("name", name); postParams.Add("type", fileType); postParams.Add("description", description); foreach (KeyValuePair keyValuePair in postParams) dictionary1.Add(keyValuePair.Key, keyValuePair.Value); string str = Util.Sign(service.secretKey, dictionary1); headerParams.Add("signature", str); string url = service.version + "/" + service.resource; ExecuteMultipart("uploadFile", inputStream, queryParams, postParams, dictionary1, headerParams, url, Config.GetInstance().GetAccept(), callBack); } public static void ExecuteMultipart(string name, Stream stream, Dictionary queryParams, Dictionary postParams, Dictionary signParams, Dictionary headerParams, string url, string accept, App42CallBack callBack) { WWWForm wwwForm = new WWWForm(); foreach (KeyValuePair keyValuePair in postParams) wwwForm.AddField(keyValuePair.Key, keyValuePair.Value); FileStream fileStream = stream as FileStream; byte[] numArray = RESTConnectorWWW.StreamToByteArray(stream); string extension = Path.GetExtension(fileStream.Name); string name1 = fileStream.Name; wwwForm.AddBinaryData(name, numArray, name1 + "." + extension, "*"); Hashtable headers = wwwForm.headers; headers.Add((object) "Accept", (object) accept); foreach (DictionaryEntry entry in headers) { if (!headerParams.ContainsKey(entry.Key.ToString())) headerParams.Add(entry.Key.ToString(), entry.Value.ToString()); } string str = RESTConnectorWWW.GetInstance().buildQueryString(queryParams); App42Log.Console(" QueryString is " + str); string uri = Config.GetInstance().GetBaseURL() + UrlEncode(url).Replace("+", "%20").Replace("%2f", "/") + str; if (!headerParams.ContainsKey("Custom-Headers")) headerParams.Add("Custom-Headers", "POST"); RESTConnectorWWW.GetInstance().StartCoroutine(RESTConnectorWWW.GetInstance().WaitForMultipart(uri, wwwForm.data, signParams, headerParams, callBack)); } public static string UrlEncode(string str) { if (str == null) return (string) null; byte[] bytes1 = Encoding.UTF8.GetBytes(str); byte[] bytes2 = UrlEncode(bytes1, 0, bytes1.Length, false); return Encoding.UTF8.GetString(bytes2, 0, bytes2.Length); } private static byte[] UrlEncode(byte[] bytes, int offset, int count, bool alwaysCreateNewReturnValue) { byte[] numArray = UrlEncode(bytes, offset, count); if (!alwaysCreateNewReturnValue || numArray == null || numArray != bytes) return numArray; return (byte[]) numArray.Clone(); } private static byte[] UrlEncode(byte[] bytes, int offset, int count) { if (!ValidateUrlEncodingParameters(bytes, offset, count)) return (byte[]) null; int num1 = 0; int num2 = 0; for (int index = 0; index < count; ++index) { char ch = (char) bytes[offset + index]; if ((int) ch == 32) ++num1; else if (!IsUrlSafeChar(ch)) ++num2; } if (num1 == 0 && num2 == 0) return bytes; byte[] numArray1 = new byte[count + num2 * 2]; int num3 = 0; for (int index1 = 0; index1 < count; ++index1) { byte num4 = bytes[offset + index1]; char ch = (char) num4; if (IsUrlSafeChar(ch)) numArray1[num3++] = num4; else if ((int) ch == 32) { numArray1[num3++] = (byte) 43; } else { byte[] numArray2 = numArray1; int index2 = num3; int num5 = 1; int num6 = index2 + num5; int num7 = 37; numArray2[index2] = (byte) num7; byte[] numArray3 = numArray1; int index3 = num6; int num8 = 1; int num9 = index3 + num8; int num10 = (int) (byte) IntToHex((int) num4 >> 4 & 15); numArray3[index3] = (byte) num10; byte[] numArray4 = numArray1; int index4 = num9; int num11 = 1; num3 = index4 + num11; int num12 = (int) (byte) IntToHex((int) num4 & 15); numArray4[index4] = (byte) num12; } } return numArray1; } private static bool ValidateUrlEncodingParameters(byte[] bytes, int offset, int count) { if (bytes == null && count == 0) return false; if (bytes == null) throw new ArgumentNullException("bytes"); if (offset < 0 || offset > bytes.Length) throw new ArgumentOutOfRangeException("offset"); if (count < 0 || offset + count > bytes.Length) throw new ArgumentOutOfRangeException("count"); return true; } private static char IntToHex(int n) { if (n <= 9) return (char) (n + 48); return (char) (n - 10 + 97); } private static bool IsUrlSafeChar(char ch) { if ((int) ch >= 97 && (int) ch <= 122 || (int) ch >= 65 && (int) ch <= 90 || (int) ch >= 48 && (int) ch <= 57) return true; switch (ch) { case '!': case '(': case ')': case '*': case '-': case '.': case '_': return true; default: return false; } } }