From 2c2ac08bf72fe858baef06e9be2cc2b558409b65 Mon Sep 17 00:00:00 2001 From: yuri Date: Mon, 19 Nov 2018 15:20:20 +0200 Subject: [PATCH] fix upload attacment --- application/Espo/Core/Mail/Importer.php | 3 ++- application/Espo/Services/Attachment.php | 23 +++++++++++------------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/application/Espo/Core/Mail/Importer.php b/application/Espo/Core/Mail/Importer.php index ef306a4fa5..3f62f16155 100644 --- a/application/Espo/Core/Mail/Importer.php +++ b/application/Espo/Core/Mail/Importer.php @@ -366,7 +366,8 @@ class Importer foreach ($inlineAttachmentList as $attachment) { $attachment->set([ 'relatedId' => $email->id, - 'relatedType' => 'Email' + 'relatedType' => 'Email', + 'field' => 'body' ]); $this->getEntityManager()->saveEntity($attachment); } diff --git a/application/Espo/Services/Attachment.php b/application/Espo/Services/Attachment.php index 9723e8a656..0bdef87886 100644 --- a/application/Espo/Services/Attachment.php +++ b/application/Espo/Services/Attachment.php @@ -42,7 +42,7 @@ class Attachment extends Record protected $attachmentFieldTypeList = ['file', 'image', 'attachmentMultiple']; - protected $inlineAttachmentFieldTypeList = ['text', 'wysiwyg']; + protected $inlineAttachmentFieldTypeList = ['wysiwyg']; public function upload($fileData) { @@ -95,16 +95,15 @@ class Attachment extends Record throw new BadRequest("Params 'field' and 'parentType' not passed along with 'file'."); } - $fieldType = $this->getMetadata()->get(['entityDefs', $relatedEntityType, 'fields', $field, 'type']); + if (!$role || !in_array($role, ['Attachment', 'Inline Attachment'])) { + throw new BadRequest("Not supported attachment 'role'."); + } - $this->checkAttachmentField($relatedEntityType, $field); + $this->checkAttachmentField($relatedEntityType, $field, $role); $size = mb_strlen($contents, '8bit'); if ($role === 'Attachment') { - if (!in_array($fieldType, $this->attachmentFieldTypeList)) { - throw new Error("Field type '{$fieldType}' is not allowed for attachment."); - } $maxSize = $this->getMetadata()->get(['entityDefs', $relatedEntityType, 'fields', $field, 'maxFileSize']); if (!$maxSize) { $maxSize = $this->getConfig()->get('attachmentUploadMaxSize'); @@ -116,9 +115,7 @@ class Attachment extends Record } } else if ($role === 'Inline Attachment') { - if (!in_array($fieldType, $this->inlineAttachmentFieldTypeList)) { - throw new Error("Field '{$field}' is not allowed to have inline attachment."); - } + $inlineAttachmentUploadMaxSize = $this->getConfig()->get('inlineAttachmentUploadMaxSize'); if ($inlineAttachmentUploadMaxSize) { if ($size > $inlineAttachmentUploadMaxSize * 1024 * 1024) { @@ -155,14 +152,16 @@ class Attachment extends Record } } - protected function checkAttachmentField($relatedEntityType, $field) + protected function checkAttachmentField($relatedEntityType, $field, $role = 'Attachment') { $fieldType = $this->getMetadata()->get(['entityDefs', $relatedEntityType, 'fields', $field, 'type']); if (!$fieldType) { throw new Error("Field '{$field}' does not exist."); } - if (!in_array($fieldType, $this->attachmentFieldTypeList)) { - throw new Error("Field type '{$fieldType}' is not allowed for attachment."); + + $attachmentFieldTypeListParam = lcfirst(str_replace(' ', '', $role)) . 'FieldTypeList'; + if (!in_array($fieldType, $this->$attachmentFieldTypeListParam)) { + throw new Error("Field type '{$fieldType}' is not allowed for {$role}."); } if (