Aleksandr Trofimov 3 недель назад
Родитель
Сommit
c8a663f681
2 измененных файлов с 26 добавлено и 3 удалено
  1. 16 3
      inc/classes/BxDolAiChatImages.php
  2. 10 0
      tests/units/inc/classes/BxDolAiChatImagesTest.php

+ 16 - 3
inc/classes/BxDolAiChatImages.php

@@ -14,7 +14,6 @@
 class BxDolAiChatImages
 {
     const STORAGE = 'sys_agents_chat_images';
-    const MAX_BYTES = 8388608;
     const MAX_PER_TURN = 4;
 
     public static function getInstance()
@@ -41,7 +40,8 @@ class BxDolAiChatImages
         $iSize = (int)($aFile['size'] ?? 0);
         if ($sTmp === '' || !is_uploaded_file($sTmp))
             return ['error' => 'No file'];
-        if ($iSize <= 0 || $iSize > self::MAX_BYTES)
+        $iMax = $this->maxBytes();
+        if ($iSize <= 0 || ($iMax > 0 && $iSize > $iMax))
             return ['error' => 'File is too large'];
 
         $sMime = strtolower((string)($aFile['type'] ?? ''));
@@ -303,12 +303,25 @@ class BxDolAiChatImages
             $sCode = null;
             $sBin = bx_file_get_contents($sUrl, [], 'get', [], $sCode, [], 10, [CURLOPT_FOLLOWLOCATION => false]);
         }
-        if (!is_string($sBin) || $sBin === '' || strlen($sBin) > self::MAX_BYTES)
+        $iMax = $this->maxBytes();
+        if (!is_string($sBin) || $sBin === '' || ($iMax > 0 && strlen($sBin) > $iMax))
             return '';
 
         return base64_encode($sBin);
     }
 
+    /**
+     * `max_file_size` of the chat images storage, in bytes. 0 means that object sets no limit.
+     */
+    public function maxBytes()
+    {
+        $oStorage = BxDolStorage::getObjectInstance(self::STORAGE);
+        if (!$oStorage)
+            return 0;
+
+        return (int)($oStorage->getObjectData()['max_file_size'] ?? 0);
+    }
+
     /**
      * Local path of a file in the chat images storage, '' when the URL is not ours.
      */

+ 10 - 0
tests/units/inc/classes/BxDolAiChatImagesTest.php

@@ -34,6 +34,16 @@ class BxDolAiChatImagesTest extends \PHPUnit\Framework\TestCase
         $this->assertSame('', $o->sanitizeUrl('http://' . $sHost . '@169.254.169.254/storage.php'));
     }
 
+    public function testMaxBytesFollowsStorage()
+    {
+        $oStorage = BxDolStorage::getObjectInstance('sys_agents_chat_images');
+        if (!$oStorage)
+            $this->markTestSkipped('Chat images storage is not installed.');
+
+        $iMax = (int)($oStorage->getObjectData()['max_file_size'] ?? 0);
+        $this->assertSame($iMax, (new BxDolAiChatImages())->maxBytes());
+    }
+
     public function testAllowsStorageDomain()
     {
         $sDomain = trim((string)getParam('sys_storage_s3_domain'));