public AuthParamResponse GetAuthenticationParameters(string token = null, string expire = null) { var DEFAULT_TIME_DIFF = 60 * 30; AuthParamResponse authParameters = new AuthParamResponse(); authParameters.token = token; authParameters.expire = expire; authParameters.signature = ""; if (!options.ContainsKey("privateKey") || string.IsNullOrEmpty((string)options["privateKey"])) { return(authParameters); } string defaultExpire = Utils.GetSignatureTimestamp(DEFAULT_TIME_DIFF); if (string.IsNullOrEmpty(expire)) { expire = defaultExpire; } if (string.IsNullOrEmpty(token)) { token = Guid.NewGuid().ToString(); } string signature = Utils.calculateSignature(token + expire, Encoding.ASCII.GetBytes((string)options["privateKey"])); authParameters.token = token; authParameters.expire = expire; authParameters.signature = signature; return(authParameters); }
public async Task <ImagekitResponse> UploadAsync(byte[] file, AuthParamResponse clientAuth) { Uri apiEndpoint = new Uri(Utils.GetUploadApi()); var response = await Utils.PostUploadAsync(apiEndpoint, getUploadData(clientAuth), file).ConfigureAwait(false); var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false); return(JsonConvert.DeserializeObject <ImagekitResponse>(responseContent)); }
public Dictionary <string, string> getUploadData(AuthParamResponse clientAuth = null) { if (!options.ContainsKey("fileName") || string.IsNullOrEmpty((string)options["fileName"])) { throw new ArgumentException(errorMessages.MISSING_UPLOAD_FILENAME_PARAMETER); } Dictionary <string, string> postData = new Dictionary <string, string>(); postData.Add("fileName", (string)options["fileName"]); if (options.ContainsKey("folder") && !string.IsNullOrEmpty((string)options["folder"])) { postData.Add("folder", (string)options["folder"]); } if (options.ContainsKey("isPrivateFile") && (bool)options["isPrivateFile"] == true) { postData.Add("isPrivateFile", "true"); } if (options.ContainsKey("useUniqueFileName") && (bool)options["useUniqueFileName"] == false) { postData.Add("useUniqueFileName", "false"); } if (options.ContainsKey("customCoordinates") && !string.IsNullOrEmpty((string)options["customCoordinates"])) { postData.Add("customCoordinates", (string)options["customCoordinates"]); } if (options.ContainsKey("responseFields") && !string.IsNullOrEmpty((string)options["responseFields"])) { postData.Add("responseFields", (string)options["responseFields"]); } if (options.ContainsKey("tags") && !string.IsNullOrEmpty((string)options["tags"])) { postData.Add("tags", (string)options["tags"]); } else if (options.ContainsKey("tagsList")) { var tags = (string[])options["tagsList"]; if (tags.Any()) { postData.Add("tags", string.Join(",", tags)); } } if (clientAuth != null) { postData.Add("signature", clientAuth.signature); postData.Add("expire", clientAuth.expire); postData.Add("token", clientAuth.token); postData.Add("publicKey", (string)options["publicKey"]); } return(postData); }
/// <summary> /// Upload the file at the path. /// </summary> /// <param name="filePath">The local file path or remote URL for the file.</param> /// <returns>The response body of the upload request.</returns> public async Task <ImagekitResponse> UploadAsync(string filePath, AuthParamResponse clientAuth) { if (string.IsNullOrEmpty(filePath)) { throw new ArgumentException(errorMessages.MISSING_UPLOAD_FILE_PARAMETER); } Uri apiEndpoint = new Uri(Utils.GetUploadApi()); var response = await Utils.PostUploadAsync(apiEndpoint, getUploadData(clientAuth), filePath).ConfigureAwait(false); var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false); return(JsonConvert.DeserializeObject <ImagekitResponse>(responseContent)); }
/// <summary> /// Upload the file at the path. /// </summary> /// <param name="filePath">The local file path or remote URL for the file.</param> /// <returns>The response body of the upload request.</returns> public ImagekitResponse Upload(string filePath, AuthParamResponse clientAuth) { return(UploadAsync(filePath, clientAuth).Result); }
public ImagekitResponse Upload(byte[] file, AuthParamResponse clientAuth) { return(UploadAsync(file, clientAuth).Result); }