Skip to content

Commit d6f4ebe

Browse files
committed
ACF compatibility for cfhttp calls
1 parent 39f8f58 commit d6f4ebe

File tree

1 file changed

+127
-115
lines changed

1 file changed

+127
-115
lines changed

packages/lib/cloudinary.cfc

Lines changed: 127 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
component {
1+
<cfcomponent>
2+
<cfscript>
23

34
public any function init() {
45
return this;
@@ -270,52 +271,18 @@ component {
270271
return replace(stInfo.template, "{transformation}", arguments.transformation);
271272
}
272273

273-
public struct function upload(required string file, string publicID, string transformation){
274-
var sigTimestamp = DateDiff('s', CreateDate(1970,1,1), now());
275-
var sigSignature = "";
276-
var stResult = structnew();
277-
var cfhttp = structnew();
278-
var cloudName = application.fapi.getConfig("cloudinary", "cloudName", "");
279-
var apiKey = application.fapi.getConfig("cloudinary", "apiKey", "");
280-
var apiSecret = application.fapi.getConfig("cloudinary", "apiSecret", "");
281-
282-
if (not len(cloudName) or not len(apiKey) or not len(apiSecret)){
283-
throw message="Cloudinary has not been configured - add the Cloud Name, API Key and API Secret";
284-
}
285-
286-
sigSignature = lcase( hash( "public_id=#arguments.publicID#&timestamp=#sigTimestamp##apiSecret#" ,"SHA" ) );
287-
288-
// UPLOAD TO CLOUDINARY
289-
http url="https://api.cloudinary.com/v1_1/#cloudName#/image/upload" method="POST" multipart="true" {
290-
httpparam type="formfield" name="api_key" value="#apiKey#";
291-
httpparam type="formfield" name="public_id" value="#arguments.publicID#";
292-
httpparam type="formfield" name="timestamp" value="#sigTimestamp#";
293-
httpparam type="formfield" name="signature" value="#trim(sigSignature)#";
294-
if (len(arguments.transformation)){
295-
httpparam type="formfield" name="transformation" value="#arguments.transformation#";
296-
}
297-
httpparam type="file" name="file" file="#application.fc.lib.cdn.ioReadFile(location='images',file=arguments.file,datatype='image').source#";
298-
}
299-
300-
if (isjson(cfhttp.filecontent)){
301-
stResult = deserializejson(cfhttp.filecontent);
302-
stResult["urlWithSource"] = mid(stResult.url,6,len(stResult.url)) & "?source=#urlencodedformat(arguments.file)#";
303-
}
304-
305-
if (cfhttp.StatusCode neq "200 Ok"){
306-
if (structkeyexists(stResult,"error")){
307-
// Cloudinary threw error, returned information
308-
throw(message="Error uploading to Cloudinary: #stResult.error.message#", detail="#cfhttp.filecontent.toString()#");
309-
}
310-
else {
311-
// Cloudinary threw error, no information
312-
throw(message="Error uploading to Cloudinary", detail="#cfhttp.filecontent.toString()#");
313-
}
314-
}
274+
public string function getID(required string file){
315275

316-
return stResult;
276+
return urldecode(listfirst(listlast(listfirst(arguments.file,"?"),"/"),"."));
317277
}
318278

279+
public string function getSource(required string file){
280+
var stInfo = getURLInformation(arguments.file);
281+
282+
return stInfo.source;
283+
}
284+
285+
319286
public string function fetch(required string file) {
320287
var endpoint = getAPIEndpoint();
321288

@@ -337,90 +304,135 @@ component {
337304
var autoUploadFolder = application.fapi.getConfig("cloudinary", "autoUplaodFolder", "");
338305

339306
return "#endpoint#/image/upload/#autoUploadFolder##arguments.file#";
340-
}
307+
}
341308

342-
public struct function delete(required string file){
343-
var sigTimestamp = DateDiff('s', CreateDate(1970,1,1), now());
344-
var sigSignature = "";
345-
var stResult = structnew();
346-
var cfhttp = structnew();
347-
var publicID = "";
348-
var stInfo = getURLInformation(arguments.file);
309+
</cfscript>
349310

350-
var cloudName = application.fapi.getConfig("cloudinary", "cloudName", "");
351-
var apiKey = application.fapi.getConfig("cloudinary", "apiKey", "");
352-
var apiSecret = application.fapi.getConfig("cloudinary", "apiSecret", "");
311+
312+
<cffunction name="upload" access="public" output="false" returntype="struct">
313+
<cfargument name="file" type="string" required="true">
314+
<cfargument name="publicID" type="string" required="true">
315+
<cfargument name="transformation" type="string" required="true">
316+
317+
<cfset var sigTimestamp = DateDiff('s', CreateDate(1970,1,1), now())>
318+
<cfset var sigSignature = "">
319+
<cfset var stResult = structnew()>
320+
<cfset var stResponse = structnew()>
321+
<cfset var cloudName = application.fapi.getConfig("cloudinary", "cloudName", "")>
322+
<cfset var apiKey = application.fapi.getConfig("cloudinary", "apiKey", "")>
323+
<cfset var apiSecret = application.fapi.getConfig("cloudinary", "apiSecret", "")>
353324

354-
if (not len(cloudName) or not len(apiKey) or not len(apiSecret)){
355-
throw(message="Cloudinary has not been configured - add the Cloud Name, API Key and API Secret");
356-
}
325+
<cfif not len(cloudName) or not len(apiKey) or not len(apiSecret)>
326+
<cfthrow message="Cloudinary has not been configured - add the Cloud Name, API Key and API Secret">
327+
</cfif>
357328

358-
if (stInfo.type != "post"){
359-
return {};
360-
}
361-
362-
publicID = getID(arguments.file);
363-
sigSignature = lcase( hash( "public_id=#publicID#&timestamp=#sigTimestamp##apiSecret#" ,"SHA" ) );
329+
<cfset sigSignature = lcase( hash( "public_id=#arguments.publicID#&timestamp=#sigTimestamp##apiSecret#" ,"SHA" ) )>
364330

365-
// DELETE FROM CLOUDINARY
366-
http url="https://api.cloudinary.com/v1_1/#cloudName#/resources/image/upload?public_ids=#publicID#" method="DELETE" username="#apiKey#" password="#apiSecret#";
331+
<!--- UPLOAD TO CLOUDINARY --->
332+
<cfhttp url="https://api.cloudinary.com/v1_1/#cloudName#/image/upload" method="POST" multipart="true" result="stResponse">
333+
<cfhttpparam type="formfield" name="api_key" value="#apiKey#">
334+
<cfhttpparam type="formfield" name="public_id" value="#arguments.publicID#">
335+
<cfhttpparam type="formfield" name="timestamp" value="#sigTimestamp#">
336+
<cfhttpparam type="formfield" name="signature" value="#trim(sigSignature)#">
337+
<cfif len(arguments.transformation)>
338+
<cfhttpparam type="formfield" name="transformation" value="#arguments.transformation#">
339+
</cfif>
340+
<cfhttpparam type="file" name="file" file="#application.fc.lib.cdn.ioReadFile(location='images',file=arguments.file,datatype='image').source#">
341+
</cfhttp>
367342

368-
if (isjson(cfhttp.filecontent)){
369-
stResult = deserializejson(cfhttp.filecontent);
370-
}
343+
<cfif isjson(stResponse.filecontent)>
344+
<cfset stResult = deserializejson(stResponse.filecontent)>
345+
<cfset stResult["urlWithSource"] = mid(stResult.url,6,len(stResult.url)) & "?source=#urlencodedformat(arguments.file)#">
346+
</cfif>
371347

372-
if (cfhttp.StatusCode neq "200 Ok"){
373-
if (structkeyexists(stResult,"error")){
374-
// Cloudinary threw error, returned information
375-
throw(message="Error deleting from Cloudinary: #stResult.error.message#", detail="#cfhttp.filecontent.toString()#");
376-
}
377-
else {
378-
// Cloudinary threw error, no information
379-
throw(message="Error deleting from Cloudinary", detail="#cfhttp.filecontent.toString()#");
380-
}
381-
}
348+
<cfif stResponse.StatusCode neq "200 Ok">
349+
<cfif structkeyexists(stResult,"error")>
350+
<!--- Cloudinary threw error, returned information --->
351+
<cfthrow message="Error uploading to Cloudinary: #stResult.error.message#" detail="#stResponse.filecontent.toString()#">
352+
<cfelse>
353+
<!--- Cloudinary threw error, no information --->
354+
<cfthrow message="Error uploading to Cloudinary" detail="#stResponse.filecontent.toString()#">
355+
</cfif>
356+
</cfif>
382357

383-
return stResult;
384-
}
385-
386-
public struct function getInfo(required string file){
387-
var cfhttp = structnew();
388-
var publicID = getID(arguments.file);
389-
var stResult = structnew();
358+
<cfreturn stResult>
359+
</cffunction>
360+
361+
362+
363+
<cffunction name="delete" access="public" output="false" returntype="struct">
364+
<cfargument name="file" type="string" required="true">
365+
366+
<cfset var sigTimestamp = DateDiff('s', CreateDate(1970,1,1), now())>
367+
<cfset var sigSignature = "">
368+
<cfset var stResult = structnew()>
369+
<cfset var stResponse = structnew()>
370+
<cfset var publicID = "">
371+
<cfset var stInfo = getURLInformation(arguments.file)>
372+
373+
<cfset var cloudName = application.fapi.getConfig("cloudinary", "cloudName", "")>
374+
<cfset var apiKey = application.fapi.getConfig("cloudinary", "apiKey", "")>
375+
<cfset var apiSecret = application.fapi.getConfig("cloudinary", "apiSecret", "")>
390376

391-
if (not refindnocase("//res.cloudinary.com/",arguments.file)){
392-
throw(message="Source has not been migrated to Cloudinary");
393-
}
377+
<cfif not len(cloudName) or not len(apiKey) or not len(apiSecret)>
378+
<cfthrow message="Cloudinary has not been configured - add the Cloud Name, API Key and API Secret">
379+
</cfif>
394380

395-
http url="https://api.cloudinary.com/v1_1/#cloudName#/resources/image/upload/#publicID#" username="#apiKey#" password="#apiSecret#";
381+
<cfif stInfo.type neq "post">
382+
<cfreturn {}>
383+
</cfif>
384+
385+
<cfset publicID = getID(arguments.file)>
386+
<cfset sigSignature = lcase( hash( "public_id=#publicID#&timestamp=#sigTimestamp##apiSecret#" ,"SHA" ) )>
396387

397-
if (isjson(cfhttp.filecontent)){
398-
stResult = deserializejson(cfhttp.filecontent);
399-
}
388+
<!--- DELETE FROM CLOUDINARY --->
389+
<cfhttp url="https://api.cloudinary.com/v1_1/#cloudName#/resources/image/upload?public_ids=#publicID#" method="DELETE" username="#apiKey#" password="#apiSecret#">
400390

401-
if (cfhttp.StatusCode neq "200 Ok"){
402-
if (structkeyexists(stResult,"error")){
403-
// Cloudinary threw error, returned information
404-
throw(message="Error querying Cloudinary: #stResult.error.message#", detail="#cfhttp.filecontent.toString()#");
405-
}
406-
else {
407-
// Cloudinary threw error, no information
408-
throw(message="Error querying Cloudinary", detail="#cfhttp.filecontent.toString()#");
409-
}
410-
}
391+
<cfif isjson(stResponse.filecontent)>
392+
<cfset stResult = deserializejson(stResponse.filecontent)>
393+
</cfif>
411394

412-
return stResult;
413-
}
414-
415-
public string function getID(required string file){
395+
<cfif stResponse.StatusCode neq "200 Ok">
396+
<cfif structkeyexists(stResult,"error")>
397+
<!--- Cloudinary threw error, returned information --->
398+
<cfthrow message="Error deleting from Cloudinary: #stResult.error.message#" detail="#stResponse.filecontent.toString()#">
399+
<cfelse>
400+
<!--- Cloudinary threw error, no information --->
401+
<cfthrow message="Error deleting from Cloudinary" detail="#stResponse.filecontent.toString()#">
402+
</cfif>
403+
</cfif>
416404

417-
return urldecode(listfirst(listlast(listfirst(arguments.file,"?"),"/"),"."));
418-
}
419-
420-
public string function getSource(required string file){
421-
var stInfo = getURLInformation(arguments.file);
405+
<cfreturn stResult>
406+
</cffunction>
422407

423-
return stInfo.source;
424-
}
408+
<cffunction name="getInfo" access="public" output="false" returntype="struct">
409+
<cfargument name="file" type="string" required="true">
410+
411+
<cfset var stResponse = structnew()>
412+
<cfset var publicID = getID(arguments.file)>
413+
<cfset var stResult = structnew()>
414+
415+
<cfif not refindnocase("//res.cloudinary.com/", arguments.file)>
416+
<cfthrow message="Source has not been migrated to Cloudinary">
417+
</cfif>
418+
419+
<cfhttp url="https://api.cloudinary.com/v1_1/#cloudName#/resources/image/upload/#publicID#" username="#apiKey#" password="#apiSecret#">
420+
421+
<cfif isjson(stResponse.filecontent)>
422+
<cfset stResult = deserializejson(stResponse.filecontent)>
423+
</cfif>
424+
425+
<cfif stResponse.StatusCode neq "200 Ok">
426+
<cfif structkeyexists(stResult,"error")>
427+
<!--- Cloudinary threw error, returned information --->
428+
<cfthrow message="Error querying Cloudinary: #stResult.error.message#" detail="#stResponse.filecontent.toString()#">
429+
<cfelse>
430+
<!--- Cloudinary threw error, no information --->
431+
<cfthrow message="Error querying Cloudinary" detail="#stResponse.filecontent.toString()#">
432+
</cfif>
433+
</cfif>
434+
435+
<cfreturn stResult>
436+
</cffunction>
425437

426-
}
438+
</cfcomponent>

0 commit comments

Comments
 (0)