<?php
/**
* Created by PhpStorm.
* User: lucamontanera
* Date: 2019-03-15
* Time: 15:41
*/
namespace App\PromemoriaBundle\Services;
use Pimcore\Event\Model\AssetEvent;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Model\Metadata\Predefined\Listing;
use Pimcore\Model\Element\Tag;
class HookAssetEvent {
public function onPreAdd(ElementEventInterface $e) {
if($e instanceof AssetEvent) {
$asset = $e->getAsset();
}
}
public function onPostAdd(ElementEventInterface $e) {
if($e instanceof AssetEvent) {
$asset = $e->getAsset();
$dir = $asset->getParent();
if($dir->getType() === "folder"){
$tags = Tag::getTagsForElement("asset", $dir->getId());
Tag::setTagsForElement("asset", $asset->getId(), $tags);
}
$this->onPostUpdate($e);
}
}
public function onPostUpdate(ElementEventInterface $e) {
if($e instanceof AssetEvent) {
$asset = $e->getAsset();
$this->ocr($asset);
$this->siegfried($asset);
$this->sfogliatore($asset, "putFile");
$this->syncAsset($asset);
}
}
public function onPostDelete(ElementEventInterface $e) {
if($e instanceof AssetEvent) {
$asset = $e->getAsset();
$this->sfogliatore($asset, "delFile");
$this->syncAsset($asset, "delete");
}
}
private function siegfried($asset){
$config = \App\PromemoriaBundle\PromemoriaBundle::getConfig();
if(isset($config["siegfried"])){
$sf = shell_exec('sf -json "' . $asset->getFileSystemPath(). '"');
$version = json_decode($sf, TRUE);
if($version && isset($version["files"]) && isset($version["files"][0]) && isset($version["files"][0]["matches"])){
$mime = $version["files"][0]["matches"][0]["mime"];
$versione = $version["files"][0]["matches"][0]["version"];
$asset->addMetadata("filesize", "input", $version["files"][0]["filesize"]);
$asset->addMetadata("modified", "input", $version["files"][0]["modified"]);
$asset->addMetadata("format", "input", $version["files"][0]["matches"][0]["format"]);
$asset->addMetadata("version", "input", $versione);
$asset->addMetadata("mime", "input", $mime);
//calcolo indice
$digitalpreservation = json_decode(file_get_contents(__DIR__ . "/../digitalpreservation.json"), TRUE);
$valori = array_filter($digitalpreservation, function($value) use ($mime, $versione){
return $value["mimetype"] === $mime && (!isset($value["version"]) || $value["version"] == $versione);
});
if($valori){
$asset->addMetadata("indice", "input", array_values($valori)[0]["indice"]);
}
$asset->getDao()->update();
}
}
}
private function ocr($asset){
$config = \App\PromemoriaBundle\PromemoriaBundle::getConfig();
if(isset($config["ocr"]) && $asset->getMimetype() === 'application/pdf'){
$ocr = shell_exec("pdftotext " . $asset->getFileSystemPath() . " -");
$asset->addMetadata("ocr", "textarea", $ocr);
$asset->getDao()->update();
}
}
private function sfogliatore($asset, $action){
$config = \App\PromemoriaBundle\PromemoriaBundle::getConfig();
if(isset($config["sfogliatoreJwtToken"]) && $asset->getMimetype() === 'application/pdf'){
$urlSfogliatore = isset($config["sfogliatoreUrl"]) ? $config["sfogliatoreUrl"] : "https://sfogliatore.promemoriagroup.com";
$data = [
"id" => strval($asset->getId()),
"url" => \Pimcore\Tool::getHostUrl() . $asset->getFullPath()
];
$data_string = json_encode($data);
$ch = curl_init($urlSfogliatore . "/" . $action);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
'Authorization: Bearer ' . $config["sfogliatoreJwtToken"]
]);
curl_exec($ch);
}
}
private function syncAsset($asset, $action="sync"){
$asset_id = $asset->getId();
shell_exec("php /var/www/html/slim-sync/index.php /{$action}/asset/{$asset_id} >/dev/null 2>/dev/null &");
}
}