feat: Add file status tracking to CDokumente entity; implement beforeSave hook for hash calculation and status determination
This commit is contained in:
60
custom/Espo/Custom/Hooks/CDokumente/CDokumente.php
Normal file
60
custom/Espo/Custom/Hooks/CDokumente/CDokumente.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Espo\Custom\Hooks\CDokumente;
|
||||
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\Core\Utils\Util;
|
||||
|
||||
class CDokumente extends \Espo\Core\Hooks\Base
|
||||
{
|
||||
public function beforeSave(Entity $entity, array $options = [])
|
||||
{
|
||||
// Nur berechnen, wenn die Datei geändert wurde oder neu
|
||||
if (!$entity->isNew() && !$entity->isAttributeChanged('dokument')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$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 neue Hashes
|
||||
$newMd5 = hash_file('md5', $filePath);
|
||||
$newSha256 = hash_file('sha256', $filePath);
|
||||
|
||||
// Setze Hashes
|
||||
$entity->set('md5sum', $newMd5);
|
||||
$entity->set('sha256', $newSha256);
|
||||
|
||||
// Bestimme Status
|
||||
if ($entity->isNew()) {
|
||||
$entity->set('fileStatus', 'new');
|
||||
} else {
|
||||
$oldMd5 = $entity->getFetched('md5sum');
|
||||
$oldSha256 = $entity->getFetched('sha256');
|
||||
|
||||
if ($oldMd5 !== $newMd5 || $oldSha256 !== $newSha256) {
|
||||
$entity->set('fileStatus', 'changed');
|
||||
} else {
|
||||
$entity->set('fileStatus', 'unchanged');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user