From ecdcab7371707a8d69ca28be38b3ee5bbb051db6 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Thu, 9 Jul 2020 14:17:24 +0300 Subject: [PATCH] attachment fixes --- application/Espo/Repositories/Attachment.php | 32 ++++++++------------ application/Espo/Services/Attachment.php | 5 ++- application/Espo/Services/Record.php | 8 ++++- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/application/Espo/Repositories/Attachment.php b/application/Espo/Repositories/Attachment.php index 2d2e1dd469..3024ea385c 100644 --- a/application/Espo/Repositories/Attachment.php +++ b/application/Espo/Repositories/Attachment.php @@ -29,6 +29,8 @@ namespace Espo\Repositories; +use Espo\Core\Exceptions\Error; + use Espo\ORM\Entity; use Espo\Core\Utils\Util; @@ -62,16 +64,6 @@ class Attachment extends \Espo\Core\Repositories\Database implements 'xx-large', ]; - protected function getFileManager() - { - return $this->fileManager; - } - - protected function getFileStorageManager() - { - return $this->fileStorageManager; - } - public function beforeSave(Entity $entity, array $options = []) { parent::beforeSave($entity, $options); @@ -95,11 +87,11 @@ class Attachment extends \Espo\Core\Repositories\Database implements if ($isNew) { $entity->id = Util::generateId(); - if (!empty($entity->id) && $entity->has('contents')) { + if ($entity->has('contents')) { $contents = $entity->get('contents'); - $storeResult = $this->getFileStorageManager()->putContents($entity, $contents); + $storeResult = $this->fileStorageManager->putContents($entity, $contents); if ($storeResult === false) { - throw new \Espo\Core\Exceptions\Error("Could not store the file"); + throw new Error("Could not store the file"); } } } @@ -125,7 +117,7 @@ class Attachment extends \Espo\Core\Repositories\Database implements ])->count(); if ($duplicateCount === 0) { - $this->getFileStorageManager()->unlink($entity); + $this->fileStorageManager->unlink($entity); if (in_array($entity->get('type'), $this->imageTypeList)) { $this->removeImageThumbs($entity); @@ -137,8 +129,8 @@ class Attachment extends \Espo\Core\Repositories\Database implements { foreach ($this->imageThumbList as $suffix) { $filePath = "data/upload/thumbs/".$entity->getSourceId()."_{$suffix}"; - if ($this->getFileManager()->isFile($filePath)) { - $this->getFileManager()->removeFile($filePath); + if ($this->fileManager->isFile($filePath)) { + $this->fileManager->removeFile($filePath); } } } @@ -166,21 +158,21 @@ class Attachment extends \Espo\Core\Repositories\Database implements public function getContents(Entity $entity) { - return $this->getFileStorageManager()->getContents($entity); + return $this->fileStorageManager->getContents($entity); } public function getFilePath(Entity $entity) { - return $this->getFileStorageManager()->getLocalFilePath($entity); + return $this->fileStorageManager->getLocalFilePath($entity); } public function hasDownloadUrl(Entity $entity) { - return $this->getFileStorageManager()->hasDownloadUrl($entity); + return $this->fileStorageManager->hasDownloadUrl($entity); } public function getDownloadUrl(Entity $entity) { - return $this->getFileStorageManager()->getDownloadUrl($entity); + return $this->fileStorageManager->getDownloadUrl($entity); } } diff --git a/application/Espo/Services/Attachment.php b/application/Espo/Services/Attachment.php index ebf5c3a676..49e85dc7c0 100644 --- a/application/Espo/Services/Attachment.php +++ b/application/Espo/Services/Attachment.php @@ -79,7 +79,7 @@ class Attachment extends Record } } - protected function beforeCreateEntity(Entity $entity, $data) + protected function handleCreateInput($data) { if (!empty($data->file)) { $arr = explode(',', $data->file); @@ -140,7 +140,10 @@ class Attachment extends Record throw new BadRequest("Not supported attachment role."); } } + } + protected function beforeCreateEntity(Entity $entity, $data) + { $storage = $entity->get('storage'); if ($storage && !$this->getMetadata()->get(['app', 'fileStorage', 'implementationClassNameMap', $storage])) { $entity->clear('storage'); diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index ef2b464cac..78be25a3cc 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -911,6 +911,10 @@ class Record implements Crud, { } + protected function handleCreateInput($data) + { + } + protected function handleInput($data) { } @@ -987,9 +991,11 @@ class Record implements Crud, $entity = $this->getRepository()->get(); + $this->handleInput($data); + $this->handleCreateInput($data); + $this->filterInput($data); $this->filterCreateInput($data); - $this->handleInput($data); unset($data->id); unset($data->modifiedById);