1 var REST = library.REST.REST();
  2 
  3 /**
  4  * Removes files > 24 hours old in aCacheFolder and deletes theParentFolder if it is empty
  5  *
  6  * @param theParent Managed Folder
  7  */
  8 function removeFiles() {
  9   var aCache = fileManager.folderNewByPath(
 10     "Renditions/Workflows/_internal/restapi/tmp/"
 11   );
 12   var aRemoved = [];
 13   logger.info("Removing temporary assets from " + aCache.path);
 14   var anIterator = aCache.getContents(1);
 15   if (!anIterator.hasNext()) {
 16     fileManager.folderDelete(aCache);
 17   }
 18   while (anIterator.hasNext()) {
 19     var aFileOrFolder = anIterator.next();
 20     if (fileManager.isFile(aFileOrFolder.path)) {
 21       var aToday = new Date();
 22       if (aFileOrFolder.lastModified == -1) {
 23         return;
 24       }
 25       if (
 26         aToday.getTime() - aFileOrFolder.lastModified >=
 27         24 * 60 * 60 * 1000
 28       ) {
 29         // 24 hours
 30         fileManager.fileDelete(aFileOrFolder);
 31         REST.push(null, { removedFile: aFile.path });
 32       }
 33     }
 34   }
 35   return aRemoved;
 36 }
 37 
 38 function deleteEverything() {
 39   var aCache = fileManager.folderNewByPath(
 40     "Renditions/Workflows/_internal/restapi/tmp/"
 41   );
 42   //Get Directory info
 43   logger.info("Removing all temporary assets from " + aCache.path);
 44   fileManager.folderDelete(aCache);
 45   REST.push(null, { removedDirectory: aCache.path });
 46 }
 47 
 48 /**
 49  * Removes old files in the restapi temp folder.  Renditions/Workflows/_internal/restapi/tmp/
 50  * Called every 24 hours on a schedule
 51  * Clears all tmp files if triggered manually or by REST request
 52  * @returns {string}
 53  */
 54 function main() {
 55   if (context.getType() != eventType.SCHEDULED) {
 56     REST.setCallback(removeFiles);
 57   } else {
 58     REST.setCallback(deleteEverything);
 59   }
 60   return REST.execute();
 61 }
 62 main();
 63