src/PromemoriaBundle/Services/HookAssetEvent.php line 46

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: lucamontanera
  5.  * Date: 2019-03-15
  6.  * Time: 15:41
  7.  */
  8. namespace App\PromemoriaBundle\Services;
  9. use Pimcore\Event\Model\AssetEvent;
  10. use Pimcore\Event\Model\ElementEventInterface;
  11. use Pimcore\Model\Metadata\Predefined\Listing;
  12. use Pimcore\Model\Element\Tag;
  13. class HookAssetEvent {
  14.     public function onPreAdd(ElementEventInterface $e) {
  15.         if($e instanceof AssetEvent) {
  16.             $asset = $e->getAsset();
  17.         }
  18.     }
  19.     public function onPostAdd(ElementEventInterface $e) {
  20.         if($e instanceof AssetEvent) {
  21.             $asset = $e->getAsset();
  22.             $dir = $asset->getParent();
  23.             if($dir->getType() === "folder"){
  24.                 $tags = Tag::getTagsForElement("asset", $dir->getId());
  25.                 Tag::setTagsForElement("asset", $asset->getId(), $tags);
  26.             }
  27.             $this->onPostUpdate($e);
  28.         }
  29.     }
  30.     public function onPostUpdate(ElementEventInterface $e) {
  31.         if($e instanceof AssetEvent) {
  32.             $asset = $e->getAsset();
  33.             $this->ocr($asset);
  34.             $this->siegfried($asset);
  35.             $this->sfogliatore($asset, "putFile");
  36.             $this->syncAsset($asset);
  37.         }
  38.     }
  39.     public function onPostDelete(ElementEventInterface $e) {
  40.         if($e instanceof AssetEvent) {
  41.             $asset = $e->getAsset();
  42.             $this->sfogliatore($asset, "delFile");
  43.             $this->syncAsset($asset, "delete");
  44.         }
  45.     }
  46.     private function siegfried($asset){
  47.         $config = \App\PromemoriaBundle\PromemoriaBundle::getConfig();
  48.         if(isset($config["siegfried"])){
  49.             $sf = shell_exec('sf -json "' . $asset->getFileSystemPath(). '"');
  50.             $version = json_decode($sf, TRUE);
  51.             if($version && isset($version["files"]) && isset($version["files"][0]) && isset($version["files"][0]["matches"])){
  52.                 $mime = $version["files"][0]["matches"][0]["mime"];
  53.                 $versione = $version["files"][0]["matches"][0]["version"];
  54.                 $asset->addMetadata("filesize", "input", $version["files"][0]["filesize"]);
  55.                 $asset->addMetadata("modified", "input", $version["files"][0]["modified"]);
  56.                 $asset->addMetadata("format", "input", $version["files"][0]["matches"][0]["format"]);
  57.                 $asset->addMetadata("version", "input", $versione);
  58.                 $asset->addMetadata("mime", "input", $mime);
  59.                 //calcolo indice
  60.                 $digitalpreservation = json_decode(file_get_contents(__DIR__ . "/../digitalpreservation.json"), TRUE);
  61.                 $valori = array_filter($digitalpreservation, function($value) use ($mime, $versione){
  62.                     return $value["mimetype"] === $mime && (!isset($value["version"]) || $value["version"] == $versione);
  63.                 });
  64.                 if($valori){
  65.                     $asset->addMetadata("indice", "input", array_values($valori)[0]["indice"]);
  66.                 }
  67.                 $asset->getDao()->update();
  68.             }
  69.         }
  70.     }
  71.     private function ocr($asset){
  72.         $config = \App\PromemoriaBundle\PromemoriaBundle::getConfig();
  73.         if(isset($config["ocr"]) && $asset->getMimetype() === 'application/pdf'){
  74.             $ocr = shell_exec('pdftotext "/var/www/html/public/var/assets' . $asset->getRealPath() . $asset->getFileName() . '" -');
  75.             $asset->addMetadata("ocr", "textarea", $ocr);
  76.             $asset->getDao()->update();
  77.         }
  78.     }
  79.     private function sfogliatore($asset, $action){
  80.         $config = \App\PromemoriaBundle\PromemoriaBundle::getConfig();
  81.         if(isset($config["sfogliatoreJwtToken"]) && $asset->getMimetype() === 'application/pdf'){
  82.             $urlSfogliatore = isset($config["sfogliatoreUrl"]) ?: "https://sfogliatore.promemoriagroup.com";
  83.             $data = [
  84.                 "id" => strval($asset->getId()),
  85.                 "url" => \Pimcore\Tool::getHostUrl() . $asset->getFullPath()
  86.             ];
  87.             $data_string = json_encode($data);
  88.             $ch = curl_init($urlSfogliatore . "/" . $action);
  89.             curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  90.             curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  91.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  92.             curl_setopt($ch, CURLOPT_HTTPHEADER, [
  93.                 'Content-Type: application/json',
  94.                 'Content-Length: ' . strlen($data_string),
  95.                 'Authorization: Bearer ' . $config["sfogliatoreJwtToken"]
  96.             ]);
  97.             curl_exec($ch);
  98.         }
  99.     }
  100.     private function syncAsset($asset, $action="sync"){
  101.         $asset_id = $asset->getId();
  102.         shell_exec("php /var/www/html/slim-sync/index.php /{$action}/asset/{$asset_id} >/dev/null 2>/dev/null &");
  103.     }
  104. }