AnsweredAssumed Answered

Remove association to folder after transforming (PDF dl)

Question asked by mycroes on Apr 8, 2010
Hi all,

I've developed a custom 'download as PDF' action that allows downloading files as PDF directly from within Share. The action currently works fine, however there are some unintended side-effects I want to get rid of.

In the webscript I first check whether a file has the <b>cm:transformable</b> aspect, if it has the aspect I search the <b>cm:formats</b> associations to see if there's an association to a node of the <b>application/pdf</b> mimetype. If so, I've found the PDF file I want to return, if not I proceed with creating the PDF document.

To create the PDF document I create a new unnamed folder in companyhome. This results in a folder with a generated name. I transform the source file to a PDF file in the newly created folder and then I create the <b>cm:formats</b> association between the PDF file and the source file.

However as a side-effect I now have a randomly named folder inside companyhome, with a PDF file in it. I know it's possible to have a more 'proper' structure for the converted files, however I'd prefer to just entirely remove the association between the converted file and whatever folder it is created in. Even better would be if I could create the PDF file directly under the cm:formats association, but it seems the javascript transformation API is only capable of generating a transformed file in a directory.

In my eyes the best solution is a change to the transformation API to allow a transformation without needing the association to a folder, but I would be satisfied if I can just remove this association afterwards.

For reference here's the webscript code I use to do the transformations:

<import resource="classpath:/alfresco/templates/webscripts/org/alfresco/slingshot/documentlibrary/action/action.lib.js">

/**
* Download PDF action.
* @method POST
*/

/**
* Entrypoint required by action.lib.js
*
* @method runAction
* @param  p_params {object} Object literal containing file
* @return {object|null} object representation of action results
*/
function runAction(p_params)
{
   var results = [];
   var files = p_params.files;
   var file, result, nodeRef;

   for (file in files)
   {
      nodeRef = files[file];
      result = {
         action: "downloadPdf",
         nodeRef: nodeRef,
         success: false
      };

      file = search.findNode(nodeRef);

      var pdfFile = null;

      if (file.hasAspect("cm:transformable")) {
         for (key in file.assocs["cm:formats"]) {
            var node = file.assocs["cm:formats"][key];
            if (node.mimetype.toString() == 'application/pdf') {
               pdfFile = node;
               break;
            }
         }
      } else {
         file.addAspect("cm:transformable");
      }

      if (null === pdfFile) {
         var folder = companyhome.createNode(null, "cm:folder");
         var pdfFile = file.transformDocument('application/pdf', folder);

         file.createAssociation(pdfFile, "cm:formats");
         result.createdNew = true;
      }

      result.id = pdfFile.name;
      result.nodeRef = pdfFile.nodeRef.toString();
      result.success = true;

      results.push(result);
   }

   return results;
}

/* Bootstrap action script */
main();

Also, it would be nice if I could just find the generated PDF without iterating over all the cm:formats associations, so if anyone knows if I can influence the key here I'd be interested to know!
Regards,

Michael

Outcomes