Created
December 15, 2023 06:22
-
-
Save vishnusomanus/f3c6125168d8a1996c72614ccd3690ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cffunction name="createLinkString" access="private" returntype="string" output="false" description="Rearrange parameters alphabetically and construct query string like name value pairs"> | |
<cfargument name="stParams" type="struct" required="true"> | |
<cfset var preSignStr = ""> | |
<cfset var keys = listSort(structKeyList(arguments.stParams), "textnocase", "asc")> | |
<cfset var i = 1> | |
<cfset var key = ""> | |
<cfset var value = ""> | |
<cfloop from="1" to="#listLen(keys)#" index="i"> | |
<cfset key = listGetAt(keys, i)> | |
<cfset value = arguments.stParams[key]> | |
<cfif i EQ listLen(keys)> | |
<cfset preSignStr &= key & "=" & value> | |
<cfelse> | |
<cfset preSignStr &= key & "=" & value & "&"> | |
</cfif> | |
</cfloop> | |
<cfreturn preSignStr> | |
</cffunction> | |
<cffunction name="generateSignature" access="private" returntype="struct" output="false"> | |
<cfargument name="stParams" type="struct" required="true"> | |
<cfset var stSign = {}> | |
<cfset arguments.stParams["timestamp"] = dateDiff("s", dateConvert("utc2Local", createDateTime(1970, 1, 1, 0, 0, 0)), now())> | |
<cfset var linkStr = createLinkString(stParams)> | |
<cfset stSign.signature = sign(linkStr)> | |
<cfset stSign.timestamp = arguments.stParams["timestamp"]> | |
<cfreturn stSign> | |
</cffunction> | |
<cffunction name="sign" access="private" returntype="string" output="false"> | |
<cfargument name="text" type="string" required="true"> | |
<cfset var preSignStr = arguments.text & "6inA6Bh4gPJBbM9vkNwFRjeGWEo"> | |
<cfreturn LCase(hash(preSignStr, "SHA"))> | |
</cffunction> | |
<cfset apiKey = ""> | |
<cfset apiSecret = ""> | |
<cfset cloudName = ""> | |
<cfset localFilePath = "data:image/jpeg;base64,"> | |
<cfset stParams2Sign = {}> | |
<cfset stPostBody = {}> | |
<cfset stPostResponse = {}> | |
<cfset stParams2Sign["public_id"] = 1> | |
<cfset stParams2Sign["folder"] = 'addons/dev'> | |
<cfset stParams2Sign["invalidate"] = true> | |
<cfset stSign = generateSignature(stParams2Sign)> | |
<cfset stPostBody["file"] = localFilePath> | |
<cfset stPostBody["public_id"] = 1> | |
<cfset stPostBody["folder"] = 'addons/dev'> | |
<cfset stPostBody["invalidate"] = true> | |
<cfset stPostBody["timestamp"] = stSign.timestamp> | |
<cfset stPostBody["signature"] = stSign.signature> | |
<cfset stPostBody["api_key"] = apiKey> | |
<cfset httpCallUrl = "https://api.cloudinary.com/v1_1/#cloudName#/auto/upload"> | |
<cfset bodyData = StructNew()> | |
<cfset bodyData["CRLF"] = Chr(13) & Chr(10)> | |
<cfset bodyData["BODY"] = ""> | |
<cfset bodyData["HEADERS"] = ""> | |
<cfif StructIsEmpty(stPostBody)> | |
<cfset bodyData["HEADERS"] &= "Authorization: Basic #ToBase64(apiKey & ":" & apiSecret)#" & bodyData["CRLF"]> | |
</cfif> | |
<cfset bodyData["HEADERS"] &= "Content-Type: application/x-www-form-urlencoded" & bodyData["CRLF"]> | |
<cfloop collection="#stPostBody#" item="key"> | |
<cfset bodyData["BODY"] &= "#key#=#URLEncodedFormat(stPostBody[key])#&"> | |
</cfloop> | |
<cfset bodyData["BODY"] = Left(bodyData["BODY"], Len(bodyData["BODY"]) - 1)> | |
<cfhttp url="#httpCallUrl#" method="POST" result="result"> | |
<cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded"> | |
<cfhttpparam type="header" name="Authorization" value="Basic #ToBase64(apiKey & ":" & apiSecret)#"> | |
<cfhttpparam type="body" value="#bodyData.BODY#"> | |
</cfhttp> | |
<cfdump var="#result#"> | |
<cfabort> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment