From ab25d00e069706d07e98b085a376911cf142ed41 Mon Sep 17 00:00:00 2001 From: Chris Peters Date: Tue, 1 Sep 2015 18:43:06 -0400 Subject: [PATCH] Closes #3 Compatibility issues with CFWheels 1.4 --- README.md | 6 +- attachments/init.cfm | 10 +-- attachments/model/attachments.cfm | 51 +++++++------ attachments/model/callbacks.cfm | 117 ++++++++++++------------------ index.cfm | 5 +- 5 files changed, 86 insertions(+), 103 deletions(-) diff --git a/README.md b/README.md index a70112b..f713547 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Overrides `fileField()` to work better with `` and [nested properties][2 ## Dependencies -You must also install the [JSON Properties plugin][3] for the Attachments plugin to work. +You must also install the [JSON Properties plugin][3] (version 0.3+) for the Attachments plugin to work. ## Documentation @@ -20,7 +20,7 @@ This plugin was created by [James Gibson][4] and [Chris Peters][5] with support [1]: http://cfwheels.org/ [2]: http://cfwheels.org/docs/chapter/nested-properties -[3]: https://github.com/liferealized/cfwheels-json-properties +[3]: https://github.com/liquifusion/cfwheels-json-properties [4]: http://iamjamesgibson.com/ -[5]: http://www.clearcrystalmedia.com/ +[5]: http://www.chrisdpeters.com/ [6]: http://liquifusion.com/ \ No newline at end of file diff --git a/attachments/init.cfm b/attachments/init.cfm index 2329890..2a9a90d 100644 --- a/attachments/init.cfm +++ b/attachments/init.cfm @@ -1,8 +1,8 @@ - - + + - + - - + + \ No newline at end of file diff --git a/attachments/model/attachments.cfm b/attachments/model/attachments.cfm index a7fb7e3..f37f780 100644 --- a/attachments/model/attachments.cfm +++ b/attachments/model/attachments.cfm @@ -1,40 +1,39 @@ - - - - - - - - + + + + + + + + var loc = {}; - // set variables.wheels.class.attachments if it does not exist - if (!StructKeyExists(variables.wheels.class, "attachments")) + // set `variables.wheels.class.attachments` if it does not exist + if (!StructKeyExists(variables.wheels.class, "attachments")) { variables.wheels.class.attachments = {}; + } loc.styles = ListToArray(arguments.styles, ",", false); arguments.styles = {}; // do processing on the styles to set them up for later // we do this now so we only do it once for each `hasAttachment()` call - if (ArrayLen(loc.styles)) - { - for (loc.style in loc.styles) - { + if (ArrayLen(loc.styles)) { + for (loc.style in loc.styles) { loc.name = ListFirst(loc.style, ":"); loc.width = ListFirst(ListLast(loc.style, ":"), "x"); loc.height = ListLast(Replace(ListLast(loc.style, ":"), ">", "", "all"), "x"); loc.preserveAspect = Find(">", loc.style) gt 0; arguments.styles[loc.name] = { - width = loc.width - , height = loc.height - , preserveAspect = loc.preserveAspect + width=loc.width, + height=loc.height, + preserveAspect=loc.preserveAspect }; } } @@ -43,16 +42,20 @@ variables.wheels.class.attachments[arguments.property] = Duplicate(arguments); // allow for the two new types of callbacks - if (!StructKeyExists(variables.wheels.class.callbacks, "beforePostProcessing")) + if (!StructKeyExists(variables.wheels.class.callbacks, "beforePostProcessing")) { variables.wheels.class.callbacks.beforePostProcessing = []; + } - if (!StructKeyExists(variables.wheels.class.callbacks, "afterPostProcessing")) + if (!StructKeyExists(variables.wheels.class.callbacks, "afterPostProcessing")) { variables.wheels.class.callbacks.afterPostProcessing = []; + } - // set callbacks for this property - afterDelete("$deleteAttachments"); + // Callbacks + jsonProperty(property=arguments.property, type="struct", registerCallbacks=false); + afterFind("$deserializeJSONPropertiesAfterFind"); afterCreate("$saveAttachments"); - beforeValidationOnUpdate("$updateAttachments"); - jsonProperty(property=arguments.property, type="struct"); + beforeValidationOnUpdate("$updateAttachments,$serializeJSONProperties"); + beforeDelete("$serializeJSONProperties"); + afterDelete("$deleteAttachments"); \ No newline at end of file diff --git a/attachments/model/callbacks.cfm b/attachments/model/callbacks.cfm index 7257683..58f01d9 100644 --- a/attachments/model/callbacks.cfm +++ b/attachments/model/callbacks.cfm @@ -1,91 +1,84 @@ - + var loc = { success = true }; - if (!FindNoCase("multipart", cgi.content_type)) + if (!FindNoCase("multipart", cgi.CONTENT_TYPE)) { return false; + } - if (!StructKeyExists(variables, "$attachmentsSaved")) - { - if (!StructKeyExists(variables, "$persistedProperties") || !StructKeyExists(variables.$persistedProperties, ListFirst(primaryKey()))) + if (!StructKeyExists(variables, "$attachmentsSaved")) { + if (!StructKeyExists(variables, "$persistedProperties") || !StructKeyExists(variables.$persistedProperties, ListFirst(primaryKey()))) { $updatePersistedProperties(); + } // loop over our attachements and upload each one - for (loc.attachment in variables.wheels.class.attachments) - { + for (loc.attachment in variables.wheels.class.attachments) { loc.saved = $saveAttachment(property=loc.attachment); - if (loc.success) + if (loc.success) { loc.success = loc.saved; + } } variables.$attachmentsSaved = true; - - if (loc.success) - { + + if (loc.success) { + $serializeJSONProperties(); this.save(); this.reload(); } } - + - + var loc = { success = true }; - if (!FindNoCase("multipart", cgi.content_type)) + if (!FindNoCase("multipart", cgi.CONTENT_TYPE)) { return false; - - if (!StructKeyExists(variables, "$attachmentsSaved")) - { - if (!StructKeyExists(variables, "$persistedProperties") || !StructKeyExists(variables.$persistedProperties, ListFirst(primaryKey()))) + } + + if (!StructKeyExists(variables, "$attachmentsSaved")) { + if (!StructKeyExists(variables, "$persistedProperties") || !StructKeyExists(variables.$persistedProperties, ListFirst(primaryKey()))) { $updatePersistedProperties(); + } // loop over our attachements and upload each one - for (loc.attachment in variables.wheels.class.attachments) - { - if (StructKeyExists(this, loc.attachment & "$attachment") && Len(form[this[loc.attachment & "$attachment"]])) - { + for (loc.attachment in variables.wheels.class.attachments) { + if (StructKeyExists(this, loc.attachment & "$attachment") && Len(form[this[loc.attachment & "$attachment"]])) { loc.attempted = true; this[loc.attachment & "_old"] = this.changedFrom(loc.attachment); $deleteAttachment(loc.attachment & "_old", variables.wheels.class.attachments[loc.attachment]); loc.saved = $saveAttachment(loc.attachment); } - else - { + else { loc.attempted = false; this[loc.attachment] = this.changedFrom(loc.attachment); loc.saved = false; } - if (loc.success) + if (loc.success) { loc.success = !loc.attempted || loc.saved; + } } variables.$attachmentsSaved = true; - - if (loc.saved) - { - this.save(); - this.reload(); - } } - + - - + + var loc = {}; loc.attachment = variables.wheels.class.attachments[arguments.property]; // only try to upload something if we have the proper $attachment field - if (StructKeyExists(this, loc.attachment.property & "$attachment") && StructKeyExists(form, this[loc.attachment.property & "$attachment"]) && Len(form[this[loc.attachment.property & "$attachment"]])) - { + if (StructKeyExists(this, loc.attachment.property & "$attachment") && StructKeyExists(form, this[loc.attachment.property & "$attachment"]) && Len(form[this[loc.attachment.property & "$attachment"]])) { loc.file = $saveFileToTempDirectory(argumentCollection=arguments); if (IsBoolean(loc.file)) { @@ -95,28 +88,27 @@ loc.filePath = Replace(GetTempDirectory() & loc.file.ServerFile, "\", "/", "all"); // check to make sure we don't have a bad file - if ($validateAttachmentFileType(loc.file.ServerFileExt, loc.attachment.blockExtensions, loc.attachment.allowExtensions)) - { + if ($validateAttachmentFileType(loc.file.ServerFileExt, loc.attachment.blockExtensions, loc.attachment.allowExtensions)) { this[loc.attachment.property] = $saveFileToStorage( - argumentCollection=loc.attachment - , source=loc.filePath - , fileSize=loc.file.FileSize); - - if (IsImageFile(loc.filePath) && StructCount(loc.attachment.styles)) - { - for (loc.style in loc.attachment.styles) + argumentCollection=loc.attachment, + source=loc.filePath, + fileSize=loc.file.FileSize + ); + + if (IsImageFile(loc.filePath) && StructCount(loc.attachment.styles)) { + for (loc.style in loc.attachment.styles) { this[loc.attachment.property].styles[loc.style] = $saveImageFileWithStyle(source=loc.filePath, argumentCollection=loc.attachment, style=loc.style); + } } } - else - { - $file(action="delete", file=loc.filePath); + else { + FileDelete(loc.filePath); this.addError(property=arguments.property, message="File is not a valid type."); return false; } } - + @@ -173,30 +165,17 @@ - // Set the file in a temp location to verify it's not evil - var fileArgs = { - action = "upload" - , fileField = this[arguments.property & "$attachment"] - , destination = GetTempDirectory() - , result = "returnValue" - , nameconflict = "overwrite" - }; - - try - { - return $file(argumentCollection=fileArgs); + try { + return FileUpload(GetTempDirectory(), this[arguments.property & "$attachment"], "", "overwrite"); } - catch (any e) - { - // This is only tested on CF9. If you get an error on Railo, see if there is an equivalent error to - // catch in another `catch` block. - if (e.Detail contains "zero-length") - { + catch (any e) { + // This is only tested on CF9. If you get an error on Railo or Lucee, see if there is an equivalent error to + // catch in another `catch` block. + if (e.Detail contains "zero-length") { this.addError(property=arguments.property, message="Can't upload an empty file"); return false; } - else - { + else { $throw(argumentCollection=e); } } diff --git a/index.cfm b/index.cfm index f19f9bb..c673865 100644 --- a/index.cfm +++ b/index.cfm @@ -1,7 +1,7 @@ - + @@ -10,7 +10,8 @@

Attachments v#attachments.version#

- Add support for file uploads to your model with the hasAttachment() function. Also provides attachmentImageTag() and attachmentLinkTo() view helpers. + Add support for file uploads to your model with the hasAttachment() function. Also provides + attachmentImageTag() and attachmentLinkTo() view helpers.

Automatically resize uploaded images to a set of "styles" that you define. (For example, you could automatically