Trashing assets with JS API


(Neil Dakeyne) #1

Squiz Matrix v5.3.4.0

I am in the process of setting up a document upload as part of a form submission but once this has been completed and the email sent I would like to completely delete the assets that were uploaded. These documents will contain sensitive data and can’t hang around the CMS for longer than they need to, so if possible removed from the trash as well.

I have followed the batching example on the manuals site but when it gets to the bit about supplying the asset ids to the trashAssets it errors out with:

Exception occurred when executing JS API function “trashAsset()”: Assertion failed: [NULL] “” is not a valid asset ID

This is the code I’m using:

 js_api.batchRequest({
    "functions":{
       "0":{
          "function":"getChildren",
          "args":{
            "asset_id": "1526929",
            "levels": 1,
            "get_attributes": 0
          },
          "blocking":1
       },
       "1":{
          "function":"trashAsset",
          "args":{
            "asset_ids":["%results_0_0_id%","%results_0_1_id%"],
          }
       }
    }
 });

Where am I going wrong? Ideally, I would like to be able to supply an array of all the file ids found in the folder but I can’t work that bit out either.


(Bart Banda) #2

That;s strange, there might be a bug with the batching function when it comes to trashing assets in that old version.

Does it work if you simply just call trashAsset without using the batchign functionality?


(Neil Dakeyne) #3

Thanks Bart for your quick reply.

Simply calling trashAsset worked as expected so Ive gone for this solution now:

js_api.getChildren({
  asset_id: '1526929',
  dataCallback: function(data) {
    
    var toTrash = [];
    for (var i = 0; i < data.length; ++i) {
      toTrash.push(data[i].asset_id);
    }

    js_api.trashAsset({
      asset_ids: toTrash
   });

  }
});

I don’t suppose there is a way to then remove these ids from the trash as well is there?


(Neil Dakeyne) #4

Also, I would like to run this after the form has been submitted, how should I go about this? Should I use a trigger or a form submission action to achieve this? Also, how do I access the JS API I created for this task then run the code above?


(Bart Banda) #5

Not without manually purging the trash. But you could potentially overwrite the sensitive data with dummy data with a separate JS API call if that would help?

If you simply want to trash the form submission asset after it has been completed, you should just use a trigger and use the form submission completed event. Would that work?