Просмотр исходного кода

Ticket #5486 - Embeds: stored image must store original file id instead of full URL.

AntonLV 5 месяцев назад
Родитель
Сommit
c8d5eb459f

+ 13 - 3
inc/classes/BxDolEmbed.php

@@ -107,7 +107,7 @@ class BxDolEmbed extends BxDolFactoryObject
             $aLocal = $this->_oDb->getLocalInfo($sUrl, $sTheme);
 
             $bRevalidate = false;
-            if($this->_iLifetime && (int)$aLocal['added'] + $this->_iLifetime < time()) {
+            if($this->_iLifetime && ($iAdded = (int)($aLocal['added'] ?? 0)) && $iAdded + $this->_iLifetime < time()) {
                 $this->_oDb->deleteLocal([
                     'url' => $sUrl,
                     'theme' => $sTheme
@@ -131,6 +131,11 @@ class BxDolEmbed extends BxDolFactoryObject
         // override this function in particular embed provider class
     }
 
+    public function onGetDataFromApi ($iId)
+    {
+        // override this function in particular embed provider class
+    }
+
     public function process ()
     {
         $aEmbeds = $this->_oDb->getLocalUnprocessed();
@@ -144,6 +149,8 @@ class BxDolEmbed extends BxDolFactoryObject
             if(($sData = $this->getDataFromApi($aEmbed['url'], $aEmbed['theme']))) {
                 $this->_oDb->updateLocal(['data' => $sData], ['id' => $aEmbed['id']]);
 
+                $this->onGetDataFromApi($aEmbed['id']);
+
                 if($bSockets)
                     $oSockets->sendEvent($this->_sSocketName, $aEmbed['id'], 'processed', $sData);
             }
@@ -173,8 +180,11 @@ class BxDolEmbed extends BxDolFactoryObject
     protected function _getData($sUrl, $sTheme)
     {
         $sData = $this->getDataFromApi($sUrl, $sTheme);
-        if($sData)
-            $this->_oDb->insertLocal($sUrl, $sTheme, $sData);
+        if($sData) {
+            $iId = $this->_oDb->insertLocal($sUrl, $sTheme, $sData);
+
+            $this->onGetDataFromApi($iId);
+        }
 
         return $sData;
     }

+ 7 - 0
inc/classes/BxDolEmbedQuery.php

@@ -46,6 +46,13 @@ class BxDolEmbedQuery extends BxDolFactoryObjectQuery
         ]);
     }
 
+    public function getLocalInfoById ($iId)
+    {
+        return $this->getRow("SELECT * FROM `" . $this->_sTableData . "` WHERE `id` = :id", [
+            'id' => $iId
+        ]);
+    }
+
     public function getLocalUnprocessed ()
     {
         return $this->getAll("SELECT * FROM `" . $this->_sTableData . "` WHERE `data` = ''");

+ 30 - 4
template/scripts/BxBaseEmbedSystem.php

@@ -14,11 +14,17 @@
  */
 class BxBaseEmbedSystem extends BxDolEmbed
 {
+    protected $_sStorage;
+    protected $_iProfileId;
+
     public function __construct ($aObject, $oTemplate)
     {
         $this->_sTableData = 'sys_embeded_data';
 
         parent::__construct ($aObject, $oTemplate);
+
+        $this->_sStorage = 'sys_images';
+        $this->_iProfileId = 0;
     }
 
     public function getLinkHTML ($sLink, $sTitle = '', $sMaxWidth = '')
@@ -45,7 +51,7 @@ class BxBaseEmbedSystem extends BxDolEmbed
         }
 
         $sImage = $sLogo = '';
-        if(($oStorage = BxDolStorage::getObjectInstance('sys_images')) !== false) {
+        if(($oStorage = BxDolStorage::getObjectInstance($this->_sStorage)) !== false) {
             if(($iImageId = $aData['image_id'] ?? 0) || (($iImageId = $aData['image'] ?? '') && is_numeric($iImageId)))
                 $sImage = $oStorage->getFileUrlById($iImageId);
 
@@ -99,15 +105,35 @@ class BxBaseEmbedSystem extends BxDolEmbed
 
         return json_encode($a);
     }
-    
+
+    public function onGetDataFromApi ($iId)
+    {
+        if(!$this->_sTableData)
+            return;
+
+        $aLocal = $this->_oDb->getLocalInfoById($iId);
+        if(!$aLocal || !is_array($aLocal))
+            return;
+
+        if(($aData = json_decode($aLocal['data'], true)) && is_array($aData)) {
+            $oStorage = BxDolStorage::getObjectInstance($this->_sStorage);
+            if($oStorage === false)
+                return;
+
+            foreach(['image_id', 'logo_id'] as $sKey)
+                if(($iMediaId = $aData[$sKey] ?? false))
+                    $oStorage->updateGhostsContentId($iMediaId, $this->_iProfileId, $iId, true);
+        }
+    }
+
     protected function _storeImages (&$a)
     {
-        $oStorage = BxDolStorage::getObjectInstance('sys_images');
+        $oStorage = BxDolStorage::getObjectInstance($this->_sStorage);
         if($oStorage === false)
             return;
 
         foreach(['image', 'logo'] as $sKey)
-            if(($sMediaUrl = $a[$sKey] ?? false) && ($iMediaId = $oStorage->storeFileFromUrl($sMediaUrl, false))) {
+            if(($sMediaUrl = $a[$sKey] ?? false) && ($iMediaId = $oStorage->storeFileFromUrl($sMediaUrl, false, $this->_iProfileId))) {
                 $a = array_merge($a, [
                     $sKey . '_src' => $a[$sKey],
                     $sKey . '_id' => $iMediaId