EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
Image upload max size with stream
Yuriif4241a2c53d2
diff --git a/application/Espo/Core/HttpClient/Client.php b/application/Espo/Core/HttpClient/Client.phpindex 225bf1052d..f43d46508a 100644--- a/application/Espo/Core/HttpClient/Client.php+++ b/application/Espo/Core/HttpClient/Client.php@@ -111,6 +111,7 @@ class Client),'allow_redirects' => false,'http_errors' => false,+ 'stream' => $this->options->stream,];if ($this->options->redirect->allow) {diff --git a/application/Espo/Core/HttpClient/Options.php b/application/Espo/Core/HttpClient/Options.phpindex 2d31ac0fef..995177fea6 100644--- a/application/Espo/Core/HttpClient/Options.php+++ b/application/Espo/Core/HttpClient/Options.php@@ -39,6 +39,7 @@ readonly class Options* Use named parameters when calling.** @param Protocol[] $protocols+ * @param bool $stream To stream the response body. As of v10.0.12.*/public function __construct(public array $protocols = [Protocol::https, Protocol::http],@@ -46,5 +47,6 @@ readonly class Optionspublic ?int $timeout = null,public ?int $connectTimeout = null,public InternalHostRestriction $internalHostRestriction = new InternalHostRestriction(),+ public bool $stream = false,) {}}diff --git a/application/Espo/Tools/Attachment/UploadUrlService.php b/application/Espo/Tools/Attachment/UploadUrlService.phpindex c69c1ac0ff..c131df4e52 100644--- a/application/Espo/Tools/Attachment/UploadUrlService.php+++ b/application/Espo/Tools/Attachment/UploadUrlService.php@@ -48,6 +48,8 @@ use const PATHINFO_EXTENSION;class UploadUrlService{+ private const int CHUNK_SIZE = 8 * 1024;+public function __construct(private AccessChecker $accessChecker,private Metadata $metadata,@@ -71,28 +73,29 @@ class UploadUrlServicethrow new ForbiddenSilent("Not allowed URL.");}- $attachment = $this->getAttachmentRepository()->getNew();-$this->accessChecker->check($data);- [$type, $contents] = $this->getImageDataByUrl($url) ?? [null, null];+ $attachment = $this->getAttachmentRepository()->getNew()+ ->setName($url)+ ->setTargetField($data->getField());++ $maxSize = $this->detailsObtainer->getUploadMaxSize($attachment);++ [$type, $contents] = $this->getImageDataByUrl($url, $maxSize) ?? [null, null];if (!$type || !$contents) {throw new ErrorSilent("Bad image data.");}$attachment- ->setName($url)->setType($type)->setContents($contents)- ->setRole(Attachment::ROLE_ATTACHMENT)- ->setTargetField($data->getField());+ ->setRole(Attachment::ROLE_ATTACHMENT);$attachment->set('parentType', $data->getParentType());$attachment->set('relatedType', $data->getRelatedType());$size = mb_strlen($contents, '8bit');- $maxSize = $this->detailsObtainer->getUploadMaxSize($attachment);if ($maxSize && $size > $maxSize) {throw new Error("File size should not exceed {$maxSize}Mb.");@@ -110,7 +113,7 @@ class UploadUrlService* @return ?array{string, string} A type and contents.* @throws Error*/- private function getImageDataByUrl(string $url): ?array+ private function getImageDataByUrl(string $url, int $maxSize): ?array{$client = $this->clientFactory->create(new Options(@@ -121,6 +124,7 @@ class UploadUrlServiceinternalHostRestriction: new HttpClient\Options\InternalHostRestriction(restrict: true,),+ stream: true,));@@ -138,7 +142,6 @@ class UploadUrlService$type = trim(explode(';', $type)[0]);}-if (!$type) {/** @var string $extension */$extension = preg_replace('#\?.*#', '', pathinfo($url, PATHINFO_EXTENSION));@@ -157,7 +160,21 @@ class UploadUrlServicereturn null;}- return [$type, (string) $response->getBody()];+ $body = $response->getBody();++ $contents = '';++ while (!$body->eof()) {+ $contents .= $body->read(self::CHUNK_SIZE);++ if ($maxSize && strlen($contents) > $maxSize) {+ $body->close();++ throw new Error("Image size should not exceed {$maxSize}Mb.");+ }+ }++ return [$type, $contents];}private function getAttachmentRepository(): AttachmentRepository