3

I need to build a plug-in. Actually I've built half of the plug-in already but I am questioning my approach. I wanted to get another point of view to see if I'm headed down the wrong path.

In my Craft control panel I want to be able to add a ZIP file to an entry or just into Assets directly. In an entry I can currently do this through an Assets field type. But I need to extend the functionality because I need to do some file validation on the zip file and then unzip it and then validate the contents of the unzipped content as well. Finally I then need to have the ability to reference the folder that contains the unzipped files.

In more concrete words, I want to be able to upload a zip file that contains an animated advertising banner. As an example, the zip file would be named "acme_banner_300x250.zip". Uploading that file would check that the banner dimensions are stated in the filename (300x250) and that the contents of the ZIP include an "index.html" plus it must contain an image file with the word "backupImage" in the file name. If all those things are true, it would unzip the file and then delete the original ZIP.

Finally, when this asset is included in an entry, I want to be able to pick it from the asset list (where it shows the backupImage file from the zip as the thumbnail) and have it be able to report the proper height and width properties along with a 'url' property that points to the folder containing the index.html file.

(Side note: These are all files I've built myself so I am not worried about malicious content being included in the upload, although if there is a way to scan them for malicious content, that would be good with me.)

My current half-finished plug-in does the following: It will intercept an uploaded asset using the "assets.onBeforeUploadAsset" event listener and then unzip the file (if the extension is "zip") in a location I can designate. I'm left with the original zip file in the Assets listing and I'm struggling with how to use the unzipped directory in my entry because that unzipped location isn't associated with the zip file listed in my assets.

I'd like to select whether this whole process happens or not. Right now, any ZIP file gets unzipped and I would prefer to be able to upload a ZIP file and have it remain a plain old ZIP file sometimes (i.e. when a client needs to get a ZIP of files from me).

Am I going down the wrong path trying to work within Assets to build this functionality? It feels like I'm creating a new type of Assets rather than just working within the existing Assets available file types.

1
  • Did you ever finish this plugin? Commented Sep 12, 2019 at 2:20

1 Answer 1

3

What you could do is to create a new asset source and a new asset field for the backupimage and a plain text field for the HTML (you can later convert this to an Incognito field). Add those new fields to the ZIP files' field layout.

After unzipping the bundle to a temp directory with you plugin, you can now check if the files meet your requirements. If everything's fine, create an asset file model from the backupimage using the insertFileByLocalPath method and relate that asset to the ZIP asset by storing its ID to the ZIP's backupimage asset field. Now read the HTML file and store it into the ZIP's html plain text field.

You might also want to have a look at the source code of my Asset Metadata plugin, as I'm doing some quite similar things with the field type it provides.

1
  • Thanks, Carl. I'll do some digging into your suggestion. I'll dig into the docs again for Assets. The Asset Metadata plugin looks interesting too. (I'll come back here in a few days and "accept" the answer after a bit too.) Commented Apr 11, 2016 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.