62 lines
1.7 KiB
PHP
62 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace Espo\Custom\Hooks\CDokumente;
|
|
|
|
use Espo\ORM\Entity;
|
|
|
|
class CDokumente extends \Espo\Core\Hooks\Base
|
|
{
|
|
public function beforeSave(Entity $entity, array $options = [])
|
|
{
|
|
// Problem: isAttributeChanged('dokument') erkennt Datei-Änderungen nicht,
|
|
// da EspoCRM Datei-Uploads nicht als Feld-Änderung markiert.
|
|
// Daher läuft der Hook bei jeder beforeSave mit dokument-Feld,
|
|
// um sicherzustellen, dass Hashes bei Datei-Änderungen berechnet werden.
|
|
// Optimierung wäre wünschenswert, aber nicht möglich mit aktueller API.
|
|
|
|
$dokument = $entity->get('dokument');
|
|
|
|
if (!$dokument) {
|
|
return;
|
|
}
|
|
|
|
if (is_object($dokument)) {
|
|
$attachment = $dokument;
|
|
} else {
|
|
$attachment = $this->getEntityManager()->getEntity('Attachment', $dokument);
|
|
}
|
|
|
|
if (!$attachment) {
|
|
return;
|
|
}
|
|
|
|
$filePath = 'data/upload/' . $attachment->get('id');
|
|
if (!file_exists($filePath)) {
|
|
return;
|
|
}
|
|
|
|
// Berechne Blake3 Hash
|
|
$fileContent = file_get_contents($filePath);
|
|
if ($fileContent === false) {
|
|
return;
|
|
}
|
|
|
|
$newBlake3 = \blake3($fileContent);
|
|
|
|
// Setze Hash
|
|
$entity->set('blake3hash', $newBlake3);
|
|
|
|
// Bestimme Status
|
|
if ($entity->isNew()) {
|
|
$entity->set('fileStatus', 'new');
|
|
} else {
|
|
$oldBlake3 = $entity->getFetched('blake3hash');
|
|
|
|
if ($oldBlake3 !== $newBlake3) {
|
|
$entity->set('fileStatus', 'changed');
|
|
} else {
|
|
$entity->set('fileStatus', 'synced');
|
|
}
|
|
}
|
|
}
|
|
} |