BxDolTranscoderQuery.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. define('BX_DOL_QUEUE_PRUNE_FAILED', 604800); ///< timeout in seconds when 'failed' items are deleted
  10. define('BX_DOL_QUEUE_PRUNE_PROCESSING', 86400); ///< timeout in seconds when 'processing' items are deleted, in case when something went wrong during processing
  11. /**
  12. * @see BxDolTranscoder
  13. */
  14. class BxDolTranscoderQuery extends BxDolDb
  15. {
  16. const TABLE_QUEUE = 'sys_transcoder_queue';
  17. protected $_aObject;
  18. protected $_sTableQueue;
  19. protected $_sTableFiles;
  20. protected $_sTableFilters;
  21. protected $_sHandlerPrefix;
  22. public function __construct($aObject, $bUseQueue = false)
  23. {
  24. parent::__construct();
  25. $this->_aObject = $aObject;
  26. $this->_sTableQueue = $bUseQueue ? self::TABLE_QUEUE : '';
  27. $this->_sTableFiles = '`sys_transcoder_images_files`';
  28. $this->_sTableFilters = '`sys_transcoder_filters`';
  29. $this->_sHandlerPrefix = 'sys_image_transcoder_';
  30. }
  31. static public function getTranscoderObject ($sObject)
  32. {
  33. $oDb = BxDolDb::getInstance();
  34. $sQuery = $oDb->prepare("SELECT * FROM `sys_objects_transcoder` WHERE `object` = ?", $sObject);
  35. return $oDb->getRow($sQuery);
  36. }
  37. static public function getTranscoderObjects ()
  38. {
  39. $oDb = BxDolDb::getInstance();
  40. $sQuery = $oDb->prepare("SELECT * FROM `sys_objects_transcoder`");
  41. return $oDb->getAll($sQuery);
  42. }
  43. public function getTranscoderFilters ($sFilterName = '')
  44. {
  45. $aBindings = array('object' => $this->_aObject['object']);
  46. $sWhere = '';
  47. if ($sFilterName) {
  48. $sWhere .= ' AND `filter` = :filter ';
  49. $aBindings['filter'] = $sFilterName;
  50. }
  51. $sQuery = "SELECT * FROM {$this->_sTableFilters} WHERE `transcoder_object` = :object $sWhere ORDER BY `order` ASC";
  52. return $this->fromMemory('BxDolTranscoderQuery::getTranscoderFilters' . $this->_aObject['object'] . $sFilterName, 'getAll', $sQuery, $aBindings);
  53. }
  54. public function updateHandler ($iFileId, $mixedHandler)
  55. {
  56. $aBindings = array(
  57. 'transcoder' => $this->_aObject['object'],
  58. 'file_id' => $iFileId,
  59. 'handler' => $mixedHandler
  60. );
  61. $sUpdateATime = '';
  62. if ($this->_aObject['atime_tracking']) {
  63. $aBindings['atime'] = time();
  64. $sUpdateATime = ", `atime` = :atime";
  65. }
  66. return $this->query("INSERT INTO {$this->_sTableFiles} SET `transcoder_object` = :transcoder, `file_id` = :file_id, `handler` = :handler $sUpdateATime ON DUPLICATE KEY UPDATE `file_id` = :file_id $sUpdateATime", $aBindings);
  67. }
  68. public function getFileIdByHandler ($mixedHandler)
  69. {
  70. $sQuery = $this->prepare("SELECT `file_id` FROM {$this->_sTableFiles} WHERE `transcoder_object` = ? AND `handler` = ? LIMIT 1", $this->_aObject['object'], (string)$mixedHandler);
  71. return $this->getOne($sQuery);
  72. }
  73. public function updateObjectModificationTime($iTime = 0)
  74. {
  75. $sQuery = $this->prepare("UPDATE `sys_objects_transcoder` SET `ts` = ? WHERE `object` = ?", $iTime ? $iTime : time(), $this->_aObject['object']);
  76. return $this->res($sQuery);
  77. }
  78. public function updateAccessTime($mixedHandler)
  79. {
  80. $iTime = time();
  81. $sQuery = $this->prepare("UPDATE {$this->_sTableFiles} SET `atime` = ? WHERE `transcoder_object` = ? AND `handler` = ? LIMIT 1", $iTime, $this->_aObject['object'], (string)$mixedHandler);
  82. return $this->res($sQuery);
  83. }
  84. public function deleteFileTraces($iFileId)
  85. {
  86. $sQuery = $this->prepare("DELETE FROM {$this->_sTableFiles} WHERE `transcoder_object` = ? AND `file_id` = ?", $this->_aObject['object'], $iFileId);
  87. return $this->res($sQuery);
  88. }
  89. public function getFilesForPruning ()
  90. {
  91. if (!$this->_aObject['atime_tracking'] || !$this->_aObject['atime_pruning'])
  92. return array();
  93. $sQuery = $this->prepare("SELECT * FROM {$this->_sTableFiles} WHERE `transcoder_object` = ? AND `atime` != 0 AND `atime` < ?", $this->_aObject['object'], time() - $this->_aObject['atime_pruning']);
  94. return $this->getAll($sQuery);
  95. }
  96. public function registerHandlers ()
  97. {
  98. if (!$this->registerHandler ('getAlertHandlerNameLocal', 'alert_response_file_delete_local', $this->_aObject['object'], $this->_aObject['storage_object']))
  99. return false;
  100. // add handler for original storage engine
  101. if ('Storage' == $this->_aObject['source_type']) // if original storage is "Storage", not "Folder"
  102. if (!$this->registerHandler ('getAlertHandlerNameOrig', 'alert_response_file_delete_orig', $this->_aObject['object'], $this->_aObject['source_params']['object']))
  103. return false;
  104. return true;
  105. }
  106. public function unregisterHandlers ()
  107. {
  108. if (!$this->unregisterHandler('getAlertHandlerNameLocal', $this->_aObject['storage_object']))
  109. return false;
  110. // remove handler for original storage engine
  111. if ('Storage' == $this->_aObject['source_type']) // if original storage is "Storage", not "Folder"
  112. if (!$this->unregisterHandler('getAlertHandlerNameOrig', $this->_aObject['source_params']['object']))
  113. return false;
  114. return true;
  115. }
  116. static public function getForDeletionFromQueue ($sServer, $iLimit = 10)
  117. {
  118. $oDb = BxDolDb::getInstance();
  119. // delete items which are stuck in processing status
  120. $sQuery = $oDb->prepare("DELETE FROM `" . self::TABLE_QUEUE . "` WHERE `server` = ? AND `status` = ? AND `changed` < ? - ?", $sServer, BX_DOL_QUEUE_PROCESSING, time(), BX_DOL_QUEUE_PRUNE_PROCESSING);
  121. $oDb->query($sQuery);
  122. // get files which are subject to delete: with status 'delete' and expired 'failed' items
  123. $sQuery = $oDb->prepare("SELECT * FROM `" . self::TABLE_QUEUE . "` WHERE `server` = ? AND ((`status` = ? AND `changed` < ? - ?) OR `status` = ?)", $sServer, BX_DOL_QUEUE_FAILED, time(), BX_DOL_QUEUE_PRUNE_FAILED, BX_DOL_QUEUE_DELETE);
  124. return $oDb->getAll($sQuery);
  125. }
  126. static public function deleteFromQueueById ($iId)
  127. {
  128. $oDb = BxDolDb::getInstance();
  129. $sQuery = $oDb->prepare("SELECT * FROM `" . self::TABLE_QUEUE . "` WHERE `id` = ?", $iId);
  130. return $oDb->query($sQuery);
  131. }
  132. static public function getCompletedFromQueue ($iLimit = 10)
  133. {
  134. $oDb = BxDolDb::getInstance();
  135. $sQuery = $oDb->prepare("SELECT * FROM `" . self::TABLE_QUEUE . "` WHERE `status` = 'complete' ORDER BY `added` ASC LIMIT ?", $iLimit);
  136. return $oDb->getAll($sQuery);
  137. }
  138. static public function getNextInQueue ($sServer, $iLimit = 5)
  139. {
  140. $oDb = BxDolDb::getInstance();
  141. $sQuery = $oDb->prepare("SELECT COUNT(*) FROM `" . self::TABLE_QUEUE . "` WHERE `server` = ? AND `status` = 'processing'", $sServer);
  142. if ($oDb->getOne($sQuery))
  143. return array();
  144. $sQuery = $oDb->prepare("SELECT * FROM `" . self::TABLE_QUEUE . "` WHERE `status` = 'pending' ORDER BY `added` ASC LIMIT ?", $iLimit);
  145. return $oDb->getAll($sQuery);
  146. }
  147. public function getQueueTable ()
  148. {
  149. return $this->_sTableQueue;
  150. }
  151. public function deleteFromQueue ($mixedId)
  152. {
  153. if (!$this->_sTableQueue)
  154. return false;
  155. $sQuery = $this->prepare("DELETE FROM `" . $this->_sTableQueue . "` WHERE `transcoder_object` = ? AND `file_id_source` = ?", $this->_aObject['object'], $mixedId);
  156. return $this->query($sQuery);
  157. }
  158. public function getFromQueue ($mixedId)
  159. {
  160. if (!$this->_sTableQueue)
  161. return false;
  162. $sQuery = $this->prepare("SELECT * FROM `" . $this->_sTableQueue . "` WHERE `transcoder_object` = ? AND `file_id_source` = ?", $this->_aObject['object'], $mixedId);
  163. return $this->getRow($sQuery);
  164. }
  165. public function addToQueue ($mixedId, $sFileUrl, $iProfileId)
  166. {
  167. if (!$this->_sTableQueue)
  168. return false;
  169. $sQuery = $this->prepare("INSERT INTO `" . $this->_sTableQueue . "` SET `transcoder_object` = ?, `profile_id` = ?, `file_url_source` = ?, `file_id_source` = ?, `status` = ?, `added` = ?, changed = ?", $this->_aObject['object'], $iProfileId, $sFileUrl, $mixedId, BX_DOL_QUEUE_PENDING, time(), time());
  170. return $this->query($sQuery);
  171. }
  172. public function updateQueueStatus ($mixedId, $sStatus, $sLog = '', $sServer = '', $mixedIdResult = '', $sFileUrlResult = '', $sFileExtResult = '')
  173. {
  174. if (!$this->_sTableQueue)
  175. return false;
  176. $aSet = array(
  177. 'status' => $sStatus,
  178. 'changed' => time()
  179. );
  180. if($sLog)
  181. $aSet['log'] = $sLog;
  182. if($sServer)
  183. $aSet['server'] = $sServer;
  184. if($mixedIdResult && $sFileUrlResult)
  185. $aSet = array_merge($aSet, array(
  186. 'file_id_result' => $mixedIdResult,
  187. 'file_url_result' => $sFileUrlResult,
  188. 'file_ext_result' => $sFileExtResult
  189. ));
  190. $sQuery = $this->prepare("UPDATE `" . $this->_sTableQueue . "` SET " . $this->arrayToSQL($aSet) . " WHERE `transcoder_object` = ? AND `file_id_source` = ?", $this->_aObject['object'], $mixedId);
  191. return $this->query($sQuery);
  192. }
  193. protected function registerHandler ($sHandlerNameFunc, $sServiceFunc, $sObject, $sUnit)
  194. {
  195. $sHandlerName = $this->$sHandlerNameFunc ();
  196. $iHandlerId = $this->getAlertHandlerId ($sHandlerName);
  197. if ($iHandlerId) // if handler already exists, do nothing
  198. return true;
  199. $sServiceCall = serialize(array(
  200. 'module' => 'system',
  201. 'method' => $sServiceFunc,
  202. 'params' => array($sObject),
  203. 'class' => 'TemplImageServices'
  204. ));
  205. $sQuery = $this->prepare("INSERT INTO `sys_alerts_handlers` SET `name` = ?, `service_call` = ?", $sHandlerName, $sServiceCall);
  206. if (!$this->query($sQuery))
  207. return false;
  208. $iHandlerId = $this->lastId();
  209. $sQuery = $this->prepare("INSERT INTO `sys_alerts` SET `unit` = ?, `action` = ?, `handler_id` = ?", $sUnit, 'file_deleted', $iHandlerId);
  210. if (!$this->query($sQuery)) {
  211. $this->unregisterHandlers ();
  212. return false;
  213. }
  214. return true;
  215. }
  216. protected function unregisterHandler ($sHandlerNameFunc, $sUnit)
  217. {
  218. $sHandlerName = $this->$sHandlerNameFunc ();
  219. $iHandlerId = $this->getAlertHandlerId ($sHandlerName);
  220. if (!$iHandlerId) // if handler is alrady missing, do nothing
  221. return true;
  222. $sQuery = $this->prepare("DELETE FROM `sys_alerts` WHERE `unit` = ? AND `action` = ? AND `handler_id` = ?", $sUnit, 'file_deleted', $iHandlerId);
  223. if (!$this->query($sQuery))
  224. return false;
  225. $sQuery = $this->prepare("DELETE FROM `sys_alerts_handlers` WHERE `id` = ?", $iHandlerId);
  226. return $this->query($sQuery);
  227. }
  228. protected function getAlertHandlerNameLocal ()
  229. {
  230. return $this->_sHandlerPrefix . 'local_file_delete_' . $this->_aObject['object'];
  231. }
  232. protected function getAlertHandlerNameOrig ()
  233. {
  234. return $this->_sHandlerPrefix . 'orig_file_delete_' . $this->_aObject['object'];
  235. }
  236. protected function getAlertHandlerId ($sHandlerName)
  237. {
  238. $sQuery = $this->prepare("SELECT `id` FROM `sys_alerts_handlers` WHERE `name` = ?", $sHandlerName);
  239. return $this->getOne($sQuery);
  240. }
  241. public function getTranscodedFileData($mixedHandler)
  242. {
  243. $sQuery = $this->prepare("SELECT `data` FROM {$this->_sTableFiles} WHERE `transcoder_object` = ? AND `handler` = ? LIMIT 1", $this->_aObject['object'], (string)$mixedHandler);
  244. $s = $this->getOne($sQuery);
  245. if (!$s)
  246. return false;
  247. return unserialize($s);
  248. }
  249. public function updateTranscodedFileData($mixedHandler, $mixedData)
  250. {
  251. $sData = $mixedData ? serialize($mixedData) : '';
  252. $sQuery = $this->prepare("UPDATE {$this->_sTableFiles} SET `data` = ? WHERE `transcoder_object` = ? AND `handler` = ? LIMIT 1", $sData, $this->_aObject['object'], (string)$mixedHandler);
  253. return $this->res($sQuery);
  254. }
  255. }
  256. /** @} */