Answers.Body: <p>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 <a href="https://github.com/mmikkel/IncognitoField-Craft" rel="nofollow">Incognito field</a>). Add those new fields to the ZIP files' field layout.</p>
  
  <p>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 <code>insertFileByLocalPath</code> 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. </p>
  
  <p>You might also want to have a look at the source code of my <a href="https://github.com/carlcs/craft-assetmetadata" rel="nofollow">Asset Metadata</a> plugin, as I'm doing some quite similar things with the field type it provides.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-11T15:10:28.870Z
Answers.Text: 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.)
Answers.UserDisplayName: John Morton
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-10T12:18:08.483Z
Answers.LastActivityDate: 2016-04-10T12:18:08.483Z
Answers.UserDisplayName: carlcs
Body: <p>I need to build a plug-in. Actually I've built <em>half</em> 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.</p>
  
  <p>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.</p>
  
  <p>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. </p>
  
  <p>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. </p>
  
  <p>(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.)</p>
  
  <p><em>My current half-finished plug-in does the following:</em> 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. </p>
  
  <p>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).</p>
  
  <p>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. </p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-09-12T02:20:18.047Z
Comments.Text: Did you ever finish this plugin?
Comments.UserDisplayName: Erich Quist
Tags: <assets>
Title: Custom upload plugin to unzip files
ThreadText: # Custom upload plugin to unzip files
  
  (Asked by: username_0 on 2016-04-08)
  
  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.
  
  **Tags:** assets
  
  > **Comments:**
  > **username_1**: Did you ever finish this plugin?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2016-04-10. Score: 3 | Accepted answer
  
  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](https://github.com/mmikkel/IncognitoField-Craft)). 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](https://github.com/username_2/craft-assetmetadata) plugin, as I'm doing some quite similar things with the field type it provides.
  
  > **Comments:**
  > **username_0**: 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.)  
  
  ---
  
  