Skip to content

Commit 0a84318

Browse files
author
Blair McKenzie
committed
archive and rollback functionality
1 parent 415ea7a commit 0a84318

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

packages/formtools/image.cfc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,40 @@
384384
</cfif>
385385
</cffunction>
386386

387+
<cffunction name="onArchive" access="public" output="false" returntype="string" hint="Called from setData when an object is deleted">
388+
<cfargument name="typename" required="true" type="string" hint="The name of the type that this field is part of.">
389+
<cfargument name="stObject" required="true" type="struct" hint="The object of the record that this field is part of.">
390+
<cfargument name="stMetadata" required="true" type="struct" hint="This is the metadata that is either setup as part of the type.cfc or overridden when calling ft:object by using the stMetadata argument.">
391+
<cfargument name="archiveID" type="uuid" required="true" hint="The ID of the new archive" />
392+
393+
<cfset var stInfo = application.fc.lib.cloudinary.getURLInformation(arguments.stObject[arguments.stMetadata.name]) />
394+
<cfset var archiveFile = "" />
395+
396+
<cfif len(stInfo.source) and application.fc.lib.cdn.ioFileExists(location="images",file=stInfo.source)>
397+
<cfset archiveFile = "/#arguments.stObject.typename#/#arguments.archiveID#.#arguments.stMetadata.name#.#ListLast(stInfo.source,'.')#" />
398+
399+
<cfset application.fc.lib.cdn.ioCopyFile(source_location="images",source_file=stInfo.source,dest_location="archive",dest_file=archiveFile) />
400+
</cfif>
401+
402+
<cfreturn archiveFile />
403+
</cffunction>
404+
405+
<cffunction name="onRollback" access="public" output="false" returntype="string" hint="Called from setData when an object is deleted">
406+
<cfargument name="typename" required="true" type="string" hint="The name of the type that this field is part of.">
407+
<cfargument name="stObject" required="true" type="struct" hint="The object of the record that this field is part of.">
408+
<cfargument name="stMetadata" required="true" type="struct" hint="This is the metadata that is either setup as part of the type.cfc or overridden when calling ft:object by using the stMetadata argument.">
409+
<cfargument name="archiveID" type="uuid" required="true" hint="The ID of the archive being rolled back" />
410+
411+
<cfset var stInfo = application.fc.lib.cloudinary.getURLInformation(arguments.stObject[arguments.stMetadata.name]) />
412+
<cfset var archiveFile = "/#arguments.stObject.typename#/#arguments.archiveID#.#arguments.stMetadata.name#.#ListLast(arguments.stObject[arguments.stMetadata.name],'.')#" />
413+
414+
<cfif len(stInfo.source) and application.fc.lib.cdn.ioFileExists(location="archive", file=archiveFile) and not application.fc.lib.cdn.ioFileExists(location="images", file=stInfo.source)>
415+
<cfset application.fc.lib.cdn.ioMoveFile(source_location="archive",source_file=archiveFile,dest_location="images",dest_file=stInfo.source) />
416+
</cfif>
417+
418+
<cfreturn arguments.stObject[arguments.stMetadata.name] />
419+
</cffunction>
420+
387421
<cffunction name="getFileLocation" access="public" output="false" returntype="struct" hint="Returns information used to access the file: type (stream | redirect), path (file system path | absolute URL), filename, mime type">
388422
<cfargument name="objectid" type="string" required="false" default="" hint="Object to retrieve" />
389423
<cfargument name="typename" type="string" required="false" default="" hint="Type of the object to retrieve" />

packages/lib/cloudinary.cfc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ component {
210210
stResult["type"] = "post";
211211
if (refindnocase("\?_?source=", arguments.file)){
212212
stResult["source"] = urldecode(rereplacenocase(arguments.file, ".*\?_?source=([^&]+).*","\1"));
213+
214+
if (refindnocase("\?source=", arguments.file)){
215+
stResult["dependant"] = false;
216+
}
213217
}
214218
stResult["transformation"] = rereplace(arguments.file, rePost, "\1");
215219
if (len(stResult["transformation"])){
@@ -321,6 +325,7 @@ component {
321325
var stResult = structnew();
322326
var cfhttp = structnew();
323327
var publicID = "";
328+
var stInfo = getURLInformation(arguments.file);
324329

325330
var cloudName = application.fapi.getConfig("cloudinary", "cloudName", "");
326331
var apiKey = application.fapi.getConfig("cloudinary", "apiKey", "");
@@ -330,7 +335,11 @@ component {
330335
throw(message="Cloudinary has not been configured - add the Cloud Name, API Key and API Secret");
331336
}
332337

333-
publicID = getCloudinaryID(arguments.file);
338+
if (stInfo.type != "post"){
339+
return {};
340+
}
341+
342+
publicID = getID(arguments.file);
334343
sigSignature = lcase( hash( "public_id=#publicID#&timestamp=#sigTimestamp##apiSecret#" ,"SHA" ) );
335344

336345
// DELETE FROM CLOUDINARY

0 commit comments

Comments
 (0)