/**
  * For event in shared context:
  * Node.@images.add.post
  *
  * @param NodeRef $nodeRef      NodeRef Being Added
  * @param Node    $node         Node Being added
  * @param null    $notUsed      Paramater passed in via Events::trigger, but set to null, and not used.
  * @param bool    $asyncRebuild Wether to rebuildThumbnails asyncronously or not (defaults to true)
  *
  * @return void
  */
 public function processAdd(NodeRef $nodeRef, Node $node, $notUsed = null, $asyncRebuild = true)
 {
     $node = $this->RegulatedNodeService->getByNodeRef($nodeRef, new NodePartials('', '#original.fields'));
     //-----------
     // Build CMS thumbnail separately so that it's ready when this action completes.
     $file = $this->FileService->retrieveFileFromNode($node->getElement(), $node->getOutTag('#original')->TagLinkNodeRef);
     if ($file === null) {
         throw new Exception('File cannot be retrieved for Node');
     }
     $cmsThumb = $this->ImageService->createAndStoreThumbnail($node->getElement(), $this->imagesThumbnailCmsSize, $file->getLocalPath(), $this->ImageService->filenameForNode($node));
     $tag = new Tag($cmsThumb->getElement()->getSlug(), $cmsThumb->Slug, '#thumbnails', $this->imagesThumbnailCmsSize, $this->imagesThumbnailCmsSize);
     $node->replaceOutTags('#thumbnails', array($tag));
     $this->RegulatedNodeService->edit($node);
     if ($asyncRebuild) {
         // commit so the node is ready for the worker
         $this->TransactionManager->commit()->begin();
         //-----------
         // Rebuild thumbnails asynchronously
         $workerParams = array('nodeRef' => '' . $node->getNodeRef(), 'forceRebuildExisting' => false);
         $this->GearmanService->doBackgroundJob('ImagesWorker', 'rebuildThumbnails', $workerParams, 'high');
     } else {
         $this->ImageService->rebuildMissingThumbnails($node->getNodeRef(), false);
     }
     // remove storage facility generated temp file
     @unlink($file->getLocalPath());
 }
 protected function _buildCmsThumbnail($node)
 {
     $node = $this->RegulatedNodeService->getByNodeRef($node->getNodeRef(), new NodePartials('', '#original.fields'));
     //-----------
     // Build CMS thumbnail separately so that it's ready when this action completes.
     $file = $this->FileService->retrieveFileFromNode($node->getElement(), $node->getOutTag('#original')->TagLinkNodeRef);
     $cmsThumb = $this->ImageService->createAndStoreThumbnail($node->getElement(), $this->imagesThumbnailCmsSize, $file->getLocalPath(), $this->ImageService->filenameForNode($node));
     $tag = new Tag($cmsThumb->getElement()->getSlug(), $cmsThumb->Slug, '#thumbnails', $this->imagesThumbnailCmsSize, $this->imagesThumbnailCmsSize);
     $node->replaceOutTags('#thumbnails', array($tag));
     $this->RegulatedNodeService->edit($node);
 }