BxDolTranscoder.php 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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_PENDING', 'pending'); ///< file is pending for processing
  10. define('BX_DOL_QUEUE_PROCESSING', 'processing'); ///< file is in the process
  11. define('BX_DOL_QUEUE_COMPLETE', 'complete'); ///< file is already processed
  12. define('BX_DOL_QUEUE_FAILED', 'failed'); ///< file processing was failed
  13. define('BX_DOL_QUEUE_DELETE', 'delete'); ///< file is subject to delete
  14. if (!defined('BX_DOL_MAX_RETINA_DIMENSION_MOBILE'))
  15. define('BX_DOL_MAX_RETINA_DIMENSION_MOBILE', 500); ///< max image dimension which will be processes as retine on mobiles
  16. if (!defined('BX_DOL_SKIP_ATIME_UPDATE'))
  17. define('BX_DOL_SKIP_ATIME_UPDATE', 43200); ///< time interval to not update last access time, it should be comaparatively low compared to atime_prining
  18. /**
  19. * Base class for @see BxDolTranscoderImage and @see BxDolTranscoderVideo transcoders
  20. */
  21. class BxDolTranscoder extends BxDolFactory implements iBxDolFactoryObject
  22. {
  23. protected $_sLog = ''; ///< file conversion log
  24. protected $_aObject; ///< object properties
  25. protected $_oStorage; ///< storage object, transcoded files are stored here
  26. protected $_oDb; ///< database queries object
  27. protected $_sRetinaSuffix = '@2x'; ///< handler suffix for retina image file
  28. protected $_sQueueTable = ''; ///< table with queued files for transcoding
  29. protected $_sQueueStorage = ''; ///< storage object to storage temporary files
  30. protected $_iDevicePixelRatio = false; ///< force device pixel ratio, 1 or 2
  31. /**
  32. * constructor
  33. */
  34. protected function __construct($aObject, $oStorage)
  35. {
  36. parent::__construct();
  37. $this->_aObject = $aObject;
  38. $this->_oStorage = $oStorage;
  39. $this->_oDb = null;
  40. }
  41. /**
  42. * Get transcode object instance.
  43. * @param $sObject - name of transcode object.
  44. * @return false on error or instance of transcoder class.
  45. */
  46. public static function getObjectInstance($sObject)
  47. {
  48. if (isset($GLOBALS['bxDolClasses'][__CLASS__ . '!' . $sObject]))
  49. return $GLOBALS['bxDolClasses'][__CLASS__ . '!'.$sObject];
  50. // get transcode object
  51. $aObject = BxDolTranscoderQuery::getTranscoderObject($sObject);
  52. if (!$aObject || !is_array($aObject))
  53. return false;
  54. if ($aObject['source_params'])
  55. $aObject['source_params'] = unserialize($aObject['source_params']);
  56. $aObject['filters'] = false; // filters are initialized on demand
  57. // create storage object to store transcoded data
  58. $oStorage = BxDolStorage::getObjectInstance($aObject['storage_object']);
  59. if (!$oStorage)
  60. return false;
  61. // create instance
  62. $sClass = $aObject['override_class_name'] ? $aObject['override_class_name'] : 'BxDolTranscoderImage';
  63. if (!empty($aObject['override_class_file']))
  64. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  65. $o = new $sClass ($aObject, $oStorage);
  66. return ($GLOBALS['bxDolClasses'][__CLASS__ . '!' . $sObject] = $o);
  67. }
  68. /**
  69. * Delete outdated transcoed data from all transcoeeed objects, by last access time.
  70. * It called on cron, usually every day.
  71. * @return total number of pruned/deleted files
  72. */
  73. static public function pruning ()
  74. {
  75. $iCount = 0;
  76. $aObjects = self::getTranscoderObjects();
  77. foreach ($aObjects as $aObject) {
  78. if (!$aObject['atime_tracking'] || !$aObject['atime_pruning'])
  79. continue;
  80. $oTranscoder = self::getObjectInstance($aObject['object']);
  81. if (!$oTranscoder)
  82. continue;
  83. $iCount += $oTranscoder->prune();
  84. }
  85. return $iCount;
  86. }
  87. /**
  88. * Get transcoder objects array
  89. * @return array
  90. */
  91. static protected function getTranscoderObjects ()
  92. {
  93. return BxDolTranscoderQuery::getTranscoderObjects ();
  94. }
  95. /**
  96. * Process files queued for transcoding
  97. * Can be performed on separate server.
  98. */
  99. static public function processQueue ($bQueuePruning = true)
  100. {
  101. if ($bQueuePruning)
  102. self::pruneQueue();
  103. $a = BxDolTranscoderQuery::getNextInQueue (gethostname());
  104. foreach ($a as $r) {
  105. $o = self::getObjectInstance($r['transcoder_object']);
  106. if (!$o) {
  107. BxDolTranscoderQuery::deleteFromQueueById($r['id']);
  108. continue;
  109. }
  110. $aQueue = $o->getDb()->getFromQueue($r['file_id_source']);
  111. if (!$aQueue || $aQueue['status'] != BX_DOL_QUEUE_PENDING)
  112. continue;
  113. $o->transcode($aQueue['file_id_source'], $aQueue['profile_id']);
  114. }
  115. }
  116. static public function pruneQueue ()
  117. {
  118. $a = BxDolTranscoderQuery::getForDeletionFromQueue (gethostname());
  119. if (!$a)
  120. return false;
  121. foreach ($a as $aQueue) {
  122. $o = self::getObjectInstance($aQueue['transcoder_object']);
  123. if ($o)
  124. $o->deleteFromQueue($aQueue['file_id_source']);
  125. }
  126. }
  127. /**
  128. * Store completed files from tmp queue storage to the final storage
  129. * Must be performed on the actual site.
  130. */
  131. static public function processCompleted ()
  132. {
  133. $a = BxDolTranscoderQuery::getCompletedFromQueue ();
  134. foreach ($a as $r) {
  135. $o = self::getObjectInstance($r['transcoder_object']);
  136. $aQueue = $o->getDb()->getFromQueue($r['file_id_source']);
  137. if (!$aQueue || $aQueue['status'] != BX_DOL_QUEUE_COMPLETE)
  138. continue;
  139. if ($aQueue['file_url_result'] && $aQueue['file_id_result']) {
  140. // get transcoded file from provided URL
  141. $sFileData = bx_file_get_contents ($aQueue['file_url_result']);
  142. if (false === $sFileData) {
  143. $o->getDb()->updateQueueStatus($aQueue['file_id_source'], BX_DOL_QUEUE_FAILED, "download file failed: {$aQueue['file_url_result']}\n");
  144. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: processCompleted failed, get transcoded file from URL ({$aQueue['file_url_result']}) failed", BX_LOG_ERR);
  145. continue;
  146. }
  147. $sTmpFile = $o->getTmpFilename ('.' . $aQueue['file_ext_result']);
  148. if (!file_put_contents($sTmpFile, $sFileData)) {
  149. $o->getDb()->updateQueueStatus($aQueue['file_id_source'], BX_DOL_QUEUE_FAILED, "store downloaded file failed\n");
  150. @unlink($sTmpFile);
  151. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: processCompleted failed, store transcoded file to tmp file ({$sTmpFile}) failed", BX_LOG_ERR);
  152. continue;
  153. }
  154. // store downloaded file in final storage
  155. if ($o->storeTranscodedFile($aQueue['file_id_source'], $sTmpFile, $aQueue['profile_id'])) {
  156. $o->getDb()->updateQueueStatus($aQueue['file_id_source'], BX_DOL_QUEUE_DELETE); // mark to delete it (deletion is performed on the server where it was transcoded)
  157. } else {
  158. $o->getDb()->updateQueueStatus($aQueue['file_id_source'], BX_DOL_QUEUE_FAILED, "store file failed:\n" . $o->getLog());
  159. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: processCompleted failed, store transcoded file ({$aQueue['file_id_source']}) in final storage failed: " . $o->getLog(), BX_LOG_ERR);
  160. }
  161. // delete tmp local file
  162. @unlink($sTmpFile);
  163. }
  164. }
  165. }
  166. /**
  167. * Register handlers array
  168. * It can be called upon module enable event.
  169. * @param $mixed array of transcoders objects, or just one object
  170. */
  171. static public function registerHandlersArray ($mixed)
  172. {
  173. self::_callFuncForObjectsArray ($mixed, 'registerHandlers');
  174. }
  175. /**
  176. * Register system handlers
  177. */
  178. static public function registerHandlersSystem()
  179. {
  180. $sPrefix = 'sys_';
  181. $aResults = array();
  182. $aTranscoders = BxDolTranscoderQuery::getTranscoderObjects();
  183. foreach($aTranscoders as $aTranscoder) {
  184. if(strncasecmp($aTranscoder['object'], $sPrefix, strlen($sPrefix)) !== 0)
  185. continue;
  186. $aResults[] = $aTranscoder['object'];
  187. }
  188. if(!empty($aResults))
  189. self::registerHandlersArray($aResults);
  190. }
  191. /**
  192. * Unregister handlers array
  193. * It can be called upon module disbale event.
  194. * @param $mixed array of transcoders objects, or just one object
  195. */
  196. static public function unregisterHandlersArray ($mixed)
  197. {
  198. self::_callFuncForObjectsArray ($mixed, 'unregisterHandlers');
  199. }
  200. /**
  201. * Cleanup (queue for deletion all resized files)
  202. * It can be called upon module disbale event.
  203. * @param $mixed array of transcoders objects, or just one object
  204. */
  205. static public function cleanupObjectsArray ($mixed)
  206. {
  207. self::_callFuncForObjectsArray ($mixed, 'cleanup');
  208. }
  209. /**
  210. * Called automatically, upon local(transcoded) file deletetion.
  211. */
  212. static public function onAlertResponseFileDeleteLocal ($oAlert, $sObject)
  213. {
  214. $oTranscoder = self::getObjectInstance($sObject);
  215. if (!$oTranscoder)
  216. return;
  217. if ($oAlert->sAction != 'file_deleted')
  218. return;
  219. $oTranscoder->onDeleteFileLocal($oAlert->iObject);
  220. }
  221. /**
  222. * Called automatically, upon original file deletetion.
  223. */
  224. static public function onAlertResponseFileDeleteOrig ($oAlert, $sObject)
  225. {
  226. $oTranscoder = self::getObjectInstance($sObject);
  227. if (!$oTranscoder)
  228. return;
  229. if ($oAlert->sAction != 'file_deleted')
  230. return;
  231. $oTranscoder->onDeleteFileOrig($oAlert->iObject);
  232. }
  233. /**
  234. * Called automatically, upon local(transcoded) file deletetion.
  235. */
  236. public function onDeleteFileLocal($iFileId)
  237. {
  238. return $this->_oDb->deleteFileTraces($iFileId);
  239. }
  240. /**
  241. * Called automatically, upon original file deletetion.
  242. */
  243. public function onDeleteFileOrig($mixedHandler)
  244. {
  245. // deleted files queued for transcoding
  246. $this->_oDb->deleteFromQueue ($mixedHandler);
  247. // delete main file
  248. $iFileId = $this->_oDb->getFileIdByHandler($mixedHandler);
  249. if ($iFileId && !$this->_oStorage->deleteFile($iFileId)) {
  250. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: onDeleteFileOrig failed for handler ({$mixedHandler}) delete failed", BX_LOG_ERR);
  251. return false;
  252. }
  253. // delete retina file
  254. $iFileId = $this->_oDb->getFileIdByHandler($mixedHandler . $this->_sRetinaSuffix);
  255. if ($iFileId && !$this->_oStorage->deleteFile($iFileId)) {
  256. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: onDeleteFileOrig failed for handler ({$mixedHandler}) delete retina file failed", BX_LOG_ERR);
  257. return false;
  258. }
  259. return true;
  260. }
  261. /**
  262. * Register necessary alert handlers for automatic deletetion of transcoded data if source file is deleted.
  263. * Make sure that you call it once, before first usage, for example upon module installation.
  264. */
  265. public function registerHandlers ()
  266. {
  267. if (!$this->_oDb->registerHandlers ())
  268. return false;
  269. return $this->clearCacheDB();
  270. }
  271. /**
  272. * Unregister alert handlers for automatic deletetion of transcoded data if source file is deleted.
  273. * Make sure that you call it once, for example upon module uninstallation.
  274. */
  275. public function unregisterHandlers ()
  276. {
  277. if (!$this->_oDb->unregisterHandlers ())
  278. return false;
  279. return $this->clearCacheDB();
  280. }
  281. /**
  282. * Delete (queue for deletion) all resized files for the current object
  283. */
  284. public function cleanup ()
  285. {
  286. return $this->_oStorage->queueFilesForDeletionFromObject();
  287. }
  288. /**
  289. * Get storage object where transcoded data is stored
  290. */
  291. public function getStorage()
  292. {
  293. return $this->_oStorage;
  294. }
  295. /**
  296. * Get database object instance
  297. */
  298. public function getDb()
  299. {
  300. return $this->_oDb;
  301. }
  302. /**
  303. * check if transcoder suppors given file mime type
  304. */
  305. public function isMimeTypeSupported($sMimeType)
  306. {
  307. return false;
  308. }
  309. /**
  310. * Get transcoded file url.
  311. * If transcoded file is ready then direct url to the file is returned.
  312. * If there is no transcoded data available, then special url is returned, upon opening this url image is transcoded automatically and redirects to the ready transcoed image.
  313. * @param $mixedHandler - file handler
  314. * @return file url, or false on error.
  315. */
  316. public function getFileUrl($mixedHandler)
  317. {
  318. if (bx_is_api() && !($this instanceof BxDolTranscoderVideo))
  319. return $this->getOrigFileUrl($mixedHandler);
  320. if ($this->isFileReady($mixedHandler)) {
  321. $mixedHandler = $this->processHandlerForRetinaDevice($mixedHandler);
  322. $iFileId = $this->_oDb->getFileIdByHandler($mixedHandler);
  323. if (!$iFileId) {
  324. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: getFileUrl failed for handler ({$mixedHandler}) source file id wasn't found", BX_LOG_ERR);
  325. return false;
  326. }
  327. $aFile = $this->_oStorage->getFile($iFileId);
  328. if (!$aFile) {
  329. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: getFileUrl failed for handler ({$mixedHandler}) source file wasn't found in the source storage", BX_LOG_ERR);
  330. return false;
  331. }
  332. if ($this->_aObject['atime_tracking'])
  333. $this->_oDb->updateAccessTime($mixedHandler);
  334. return $this->_oStorage->getFileUrlById($iFileId);
  335. }
  336. if ($this->_sQueueTable && !$this->addToQueue($mixedHandler)) { // add file to queue (when queue is enabled) if it isn't transcoded yet
  337. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: getFileUrl failed for handler ({$mixedHandler}), can't add file to queue", BX_LOG_ERR);
  338. return false;
  339. }
  340. return $this->getFileUrlNotReady($mixedHandler);
  341. }
  342. /**
  343. * alias for @see getFileUrl
  344. */
  345. public function getFileUrlById($mixedHandler)
  346. {
  347. if (bx_is_api() && !($this instanceof BxDolTranscoderVideo))
  348. return $this->getOrigFileUrl($mixedHandler);
  349. return $this->getFileUrl($mixedHandler);
  350. }
  351. /**
  352. * Check if transcoded data is available. No need to call it directly, it is called automatically when it is needed.
  353. * @param $mixedHandler - file handler
  354. * @param $isCheckOutdated - check if transcoded file is outdated
  355. * @return false if there is no ready transcoded file is available or it is outdated, true if file is ready
  356. */
  357. public function isFileReady ($mixedHandler, $isCheckOutdated = true)
  358. {
  359. $sMethodFileReady = 'isFileReady_' . $this->_aObject['source_type'];
  360. return $this->$sMethodFileReady($mixedHandler, $isCheckOutdated);
  361. }
  362. /**
  363. * Transcode file, no need to call it directly, it is called automatically when it is needed.
  364. * @param $mixedHandler - file handler
  365. * @param $iProfileId - optional profile id, to assign transcoded file ownership to, usually it is NOT assigned to any particular profile, so just leave it default
  366. * @return true on success, false on error
  367. */
  368. public function transcode ($mixedHandler, $iProfileId = 0)
  369. {
  370. $sExtChange = false;
  371. $bRet = true;
  372. $iPID = function_exists('posix_getpid') ? posix_getpid() : 0;
  373. $this->_oDb->updateQueueStatus($mixedHandler, BX_DOL_QUEUE_PROCESSING, '', gethostname(), '', '', '', $iPID);
  374. do {
  375. // create tmp file locally
  376. $sSuffix = $this->_sQueueStorage ? 'Queue' : $this->_aObject['source_type'];
  377. $sMethodStoreFile = 'storeFileLocally_' . $sSuffix;
  378. $sTmpFile = $this->$sMethodStoreFile($mixedHandler);
  379. if (!$sTmpFile) {
  380. $this->_oDb->updateQueueStatus($mixedHandler, BX_DOL_QUEUE_FAILED, "store local tmp file failed ($sMethodStoreFile)");
  381. $bRet = false;
  382. break;
  383. }
  384. // appply filters to tmp file
  385. $this->initFilters ();
  386. foreach ($this->_aObject['filters'] as $aParams) {
  387. $this->clearLog();
  388. $sMethodFilter = 'applyFilter_' . $aParams['filter'];
  389. if (!method_exists($this, $sMethodFilter))
  390. continue;
  391. if (!$this->$sMethodFilter($sTmpFile, $aParams['filter_params'])) {
  392. unlink($sTmpFile);
  393. $this->_oDb->updateQueueStatus($mixedHandler, BX_DOL_QUEUE_FAILED, "filter failed: $sMethodFilter\n" . $this->getLog());
  394. $bRet = false;
  395. break 2;
  396. }
  397. if (!empty($aParams['filter_params']['force_type'])) {
  398. switch ($aParams['filter_params']['force_type']) {
  399. case 'jpeg':
  400. case 'jpg':
  401. $sExtChange = 'jpg';
  402. break;
  403. default:
  404. $sExtChange = $aParams['filter_params']['force_type'];
  405. }
  406. }
  407. if ($sExtChange) {
  408. $iDotPos = strrpos($sTmpFile, '.');
  409. $sExtOld = false === $iDotPos ? '' : substr($sTmpFile, $iDotPos+1);
  410. if ($sExtOld != $sExtChange) {
  411. $sTmpFileOld = $sTmpFile;
  412. $sTmpFile = false === $iDotPos ? $sTmpFile . '.' . $sExtChange : substr_replace($sTmpFile, $sExtChange, $iDotPos+1, strlen($sExtOld));
  413. if (!rename($sTmpFileOld, $sTmpFile)) {
  414. @unlink($sTmpFileOld);
  415. $this->_oDb->updateQueueStatus($mixedHandler, BX_DOL_QUEUE_FAILED, "change file extension failed: $sTmpFileOld => $sTmpFile");
  416. $bRet = false;
  417. break 2;
  418. }
  419. }
  420. }
  421. }
  422. /**
  423. * @hooks
  424. * @hookdef hook-system-transcode 'system', 'transcode' - hook before file is transcoded
  425. * - $unit_name - equals `system`
  426. * - $action - equals `transcode`
  427. * - $object_id - file handler
  428. * - $sender_id - equals `false`
  429. * - $extra_params - array of additional params with the following array keys:
  430. * - `transcoder_object` - [array] transcoder object properties
  431. * - `object` - [object] transcoder object
  432. * - `db` - [object] database object
  433. * - `file_path` - [string] file path
  434. * - `ret` - [bool] by ref, not used
  435. * @hook @ref hook-system-transcode
  436. */
  437. bx_alert('system', 'transcode', $mixedHandler, false, array(
  438. 'transcoder_object' => $this->_aObject['object'],
  439. 'object' => $this,
  440. 'db' => $this->_oDb,
  441. 'file_path' => $sTmpFile,
  442. 'ret' => &$bRet,
  443. ));
  444. $mixedHandler = $this->processHandlerForRetinaDevice($mixedHandler);
  445. if ($this->_sQueueStorage) {
  446. $sFileUrlResult = '';
  447. if (!($iFileIdResult = $this->storeTranscodedFileInQueueStorage($mixedHandler, $sTmpFile, $sFileUrlResult)))
  448. $this->_oDb->updateQueueStatus($mixedHandler, BX_DOL_QUEUE_FAILED, "store queue tmp file failed:\n" . $this->getLog());
  449. else
  450. $this->_oDb->updateQueueStatus($mixedHandler, BX_DOL_QUEUE_COMPLETE, '', '', $iFileIdResult, $sFileUrlResult, $sExtChange);
  451. } else {
  452. $bRet = $this->storeTranscodedFile($mixedHandler, $sTmpFile, $iProfileId);
  453. if ($bRet)
  454. $this->deleteFromQueue($mixedHandler);
  455. else
  456. $this->_oDb->updateQueueStatus($mixedHandler, BX_DOL_QUEUE_FAILED, "store file failed:\n" . $this->getLog());
  457. }
  458. @unlink($sTmpFile);
  459. } while (false);
  460. /**
  461. * @hooks
  462. * @hookdef hook-system-transcoded 'system', 'transcoded' - hook after file is transcoded
  463. * - $unit_name - equals `system`
  464. * - $action - equals `transcoded`
  465. * - $object_id - file handler
  466. * - $sender_id - equals `false`
  467. * - $extra_params - array of additional params with the following array keys:
  468. * - `object` - [object] transcoder object
  469. * - `ret` - [bool] by ref, override return value
  470. * @hook @ref hook-system-transcoded
  471. */
  472. bx_alert($this->_aObject['object'], 'transcoded', $mixedHandler, false, array(
  473. 'object' => $this,
  474. 'ret' => &$bRet,
  475. ));
  476. return $bRet;
  477. }
  478. /**
  479. * Delete outdated files by last access time.
  480. * @return number of pruned/deleted files
  481. */
  482. public function prune ()
  483. {
  484. $aFiles = $this->_oDb->getFilesForPruning();
  485. if (!$aFiles)
  486. return false;
  487. $iCount = 0;
  488. foreach ($aFiles as $r)
  489. $iCount += $this->_oStorage->deleteFile($r['file_id']) ? 1 : 0;
  490. return $iCount;
  491. }
  492. public function forceDevicePixelRatio ($i)
  493. {
  494. if (!$i)
  495. $this->_iDevicePixelRatio = false;
  496. elseif ($i >= 2)
  497. $this->_iDevicePixelRatio = 2;
  498. else
  499. $this->_iDevicePixelRatio = 1;
  500. }
  501. public function getDevicePixelRatio ()
  502. {
  503. $iRet = 1;
  504. if($this->_iDevicePixelRatio)
  505. $iRet = $this->_iDevicePixelRatio;
  506. else if(!isset($this->_aObject['source_params']['disable_retina']) || !$this->_aObject['source_params']['disable_retina'])
  507. $iRet = bx_get_device_pixel_ratio();
  508. // don't resize images on mobile if they are already big enough
  509. if ($iRet > 1 && $this->isMobile() && $this->getMaxResizeDimention() >= BX_DOL_MAX_RETINA_DIMENSION_MOBILE)
  510. $iRet = 1;
  511. return $iRet;
  512. }
  513. public function getOrigFileUrl ($mixedHandler)
  514. {
  515. $sMethodStoreFile = 'getOrigFileUrl_' . $this->_aObject['source_type'];
  516. return $this->$sMethodStoreFile($mixedHandler);
  517. }
  518. /**
  519. * Get file url when file isn't transcoded yet
  520. */
  521. public function getFileUrlNotReady($mixedHandler)
  522. {
  523. return false;
  524. }
  525. public function getTmpFilename ($sOverrideName = false)
  526. {
  527. if ($sOverrideName)
  528. return BX_DIRECTORY_PATH_TMP . rand(10000, 99999) . $sOverrideName;
  529. return tempnam(BX_DIRECTORY_PATH_TMP, $this->_aObject['object']);
  530. }
  531. public function clearLog()
  532. {
  533. $this->_sLog = '';
  534. }
  535. public function addToLog($s)
  536. {
  537. $this->_sLog .= $s;
  538. }
  539. public function getLog()
  540. {
  541. return $this->_sLog;
  542. }
  543. public function deleteFromQueue ($mixedHandler)
  544. {
  545. $sErrMsg = "delete queue tmp file failed:\n";
  546. $a = $this->_oDb->getFromQueue($mixedHandler);
  547. if (!$a || $a['server'] != gethostname()) // deletion must be performed on the server where it was transcoded
  548. return false;
  549. if ($a['file_id_result'] && $this->_sQueueStorage && 0 !== strncmp($a['log'], $sErrMsg, strlen($sErrMsg))) {
  550. $oStorage = BxDolStorage::getObjectInstance($this->_sQueueStorage);
  551. if (!$oStorage->deleteFile($a['file_id_result'])) {
  552. $this->_oDb->updateQueueStatus($mixedHandler, BX_DOL_QUEUE_FAILED, $sErrMsg . $this->getLog());
  553. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: deleteFromQueue failed for handler ({$mixedHandler}): " . $this->getLog(), BX_LOG_ERR);
  554. return false;
  555. }
  556. }
  557. return $this->_oDb->deleteFromQueue($mixedHandler);
  558. }
  559. public function storeTranscodedFile ($mixedHandler, $sTmpFile, $iProfileId)
  560. {
  561. $sMethodIsPrivate = 'isPrivate_' . $this->_aObject['source_type'];
  562. $isPrivate = $this->$sMethodIsPrivate($mixedHandler);
  563. $iFileId = $this->_oStorage->storeFileFromPath ($sTmpFile, $isPrivate, $iProfileId);
  564. if (!$iFileId) {
  565. $this->addToLog($this->_oStorage->getErrorString());
  566. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: storeTranscodedFile failed for handler ({$mixedHandler}): " . $this->_oStorage->getErrorString(), BX_LOG_ERR);
  567. return false;
  568. }
  569. if (!$this->_oDb->updateHandler($iFileId, $mixedHandler)) {
  570. $this->_oStorage->deleteFile($iFileId);
  571. return false;
  572. }
  573. $this->_oStorage->afterUploadCleanup($iFileId, $iProfileId);
  574. return true;
  575. }
  576. protected function storeTranscodedFileInQueueStorage ($mixedHandler, $sTmpFile, &$sFileUrlResult)
  577. {
  578. if (!$this->_sQueueStorage)
  579. return false;
  580. $oStorage = BxDolStorage::getObjectInstance($this->_sQueueStorage);
  581. $iFileIdResult = $oStorage->storeFileFromPath ($sTmpFile, 1, 0);
  582. if (!$iFileIdResult) {
  583. $this->addToLog($oStorage->getErrorString());
  584. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: storeTranscodedFileInQueueStorage failed for handler ({$mixedHandler}): " . $oStorage->getErrorString(), BX_LOG_ERR);
  585. return false;
  586. }
  587. if (!($sFileUrlResult = $oStorage->getFileUrlById($iFileIdResult))) {
  588. $this->_oStorage->deleteFile($iFileIdResult);
  589. return false;
  590. }
  591. $oStorage->afterUploadCleanup($iFileIdResult, 0);
  592. return $iFileIdResult;
  593. }
  594. protected function addToQueue($mixedHandler, $iProfileId = 0)
  595. {
  596. if ($this->_oDb->getFromQueue($mixedHandler))
  597. return true;
  598. $sFileUrl = $this->getOrigFileUrl($mixedHandler);
  599. return $this->_oDb->addToQueue ($mixedHandler, $sFileUrl, $iProfileId);
  600. }
  601. protected function applyFilter_Resize ($sFile, $aParams)
  602. {
  603. $o = BxDolImageResize::getInstance();
  604. $o->removeCropOptions ();
  605. $o->setSize (isset($aParams['w']) ? round($aParams['w'] * $this->getDevicePixelRatio()) : null, isset($aParams['h']) ? round($aParams['h'] * $this->getDevicePixelRatio()) : null);
  606. if (isset($aParams['crop_resize']) && $aParams['crop_resize'])
  607. $o->setAutoCrop (true);
  608. elseif (isset($aParams['square_resize']) && $aParams['square_resize'])
  609. $o->setSquareResize (true);
  610. else
  611. $o->setSquareResize (false);
  612. // Optional per-transcoder JPEG quality. BxDolImageResize is a singleton reused for every
  613. // image in the request, so the previous quality is restored afterwards.
  614. $iQualityPrev = null;
  615. if (isset($aParams['quality']) && (int)$aParams['quality'] > 0) {
  616. $iQualityPrev = $o->getJpegQuality();
  617. $o->setJpegQuality((int)$aParams['quality']);
  618. }
  619. $iResult = $o->resize($sFile);
  620. if ($iQualityPrev !== null)
  621. $o->setJpegQuality($iQualityPrev);
  622. if (IMAGE_ERROR_SUCCESS == $iResult)
  623. return true;
  624. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: applyFilter_Resize failed for file ({$sFile}): " . $o->getError(), BX_LOG_ERR);
  625. return false;
  626. }
  627. protected function isFileReady_Folder ($mixedHandlerOrig, $isCheckOutdated = true)
  628. {
  629. $mixedHandler = $this->processHandlerForRetinaDevice($mixedHandlerOrig);
  630. $iFileId = $this->_oDb->getFileIdByHandler($mixedHandler);
  631. if (!$iFileId)
  632. return false;
  633. $aFile = $this->_oStorage->getFile($iFileId);
  634. if (!$aFile)
  635. return false;
  636. if ($isCheckOutdated) { // warning, $isCheckOutdated is partially supported for Folder source type - file modification is not checked
  637. if ($this->_aObject['ts'] > $aFile['modified'] || !$this->getFilePath_Folder($mixedHandlerOrig)) { // if we changed transcoder object params or original file is deleted
  638. // delete file, so it will be recreated next time
  639. if ($this->_oStorage->deleteFile($aFile['id']))
  640. return false;
  641. }
  642. }
  643. return true;
  644. }
  645. protected function isFileReady_Storage ($mixedHandlerOrig, $isCheckOutdated = true)
  646. {
  647. $mixedHandler = $this->processHandlerForRetinaDevice($mixedHandlerOrig);
  648. $iFileId = $this->_oDb->getFileIdByHandler($mixedHandler);
  649. if (!$iFileId)
  650. return false;
  651. $aFile = $this->_oStorage->getFile($iFileId);
  652. if (!$aFile)
  653. return false;
  654. if ($isCheckOutdated) {
  655. $oStorageOriginal = BxDolStorage::getObjectInstance($this->_aObject['source_params']['object']);
  656. if ($oStorageOriginal) {
  657. $aFileOriginal = $oStorageOriginal->getFile($mixedHandlerOrig);
  658. if (!$aFileOriginal || $aFileOriginal['modified'] > $aFile['modified'] || $this->_aObject['ts'] >= $aFile['modified']) { // if original file was changed OR we changed transcoder object params
  659. // delete file, so it will be recreated next time
  660. if ($this->_oStorage->deleteFile($aFile['id']))
  661. return false;
  662. }
  663. }
  664. }
  665. return true;
  666. }
  667. protected function isPrivate_Folder ($mixedHandler)
  668. {
  669. if ('no' == $this->_aObject['private'])
  670. return false;
  671. return true;
  672. }
  673. protected function isPrivate_Storage ($mixedHandler)
  674. {
  675. switch ($this->_aObject['private']) {
  676. case 'no':
  677. return false;
  678. case 'yes':
  679. return true;
  680. default:
  681. case 'auto':
  682. $oStorageOriginal = BxDolStorage::getObjectInstance($this->_aObject['source_params']['object']);
  683. if (!$oStorageOriginal)
  684. return true; // in case of error - make sure that file is not public accidentally
  685. $aFile = $oStorageOriginal->getFile($mixedHandler);
  686. return $oStorageOriginal->isFilePrivate($aFile['id']);
  687. }
  688. }
  689. protected function getFilePath_Folder ($mixedHandler)
  690. {
  691. $sPath = $this->_aObject['source_params']['path'] . $mixedHandler;
  692. if (!file_exists($sPath))
  693. $sPath = BX_DIRECTORY_PATH_ROOT . $sPath;
  694. if (!file_exists($sPath)) {
  695. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: getFilePath_Folder failed, file ({$sPath}) doesn't exist for handler ({$mixedHandler})", BX_LOG_ERR);
  696. return false;
  697. }
  698. return $sPath;
  699. }
  700. protected function storeFileLocally_Folder ($mixedHandler)
  701. {
  702. $sPath = $this->getFilePath_Folder($mixedHandler);
  703. if (!$sPath) {
  704. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: storeFileLocally_Folder failed, source file ({$sPath}) doesn't exist for handler ({$mixedHandler})", BX_LOG_ERR);
  705. return false;
  706. }
  707. $sTmpFile = $this->getTmpFilename ($mixedHandler);
  708. if (!copy($sPath, $sTmpFile)) {
  709. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: storeFileLocally_Folder failed, copy from file ({$sPath}) to file ({$sTmpFile}) failed", BX_LOG_ERR);
  710. return false;
  711. }
  712. return $sTmpFile;
  713. }
  714. protected function storeFileLocally_Storage ($mixedHandler)
  715. {
  716. $oStorageOriginal = BxDolStorage::getObjectInstance($this->_aObject['source_params']['object']);
  717. if (!$oStorageOriginal) {
  718. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: storeFileLocally_Storage failed, source storage({$this->_aObject['source_params']['object']}) isn't available", BX_LOG_ERR);
  719. return false;
  720. }
  721. $aFile = $oStorageOriginal->getFile($mixedHandler);
  722. if (!$aFile) {
  723. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: storeFileLocally_Storage failed, file({$mixedHandler}) wasn't found in source storage ({$this->_aObject['source_params']['object']})", BX_LOG_ERR);
  724. return false;
  725. }
  726. $sTmpFile = $this->getTmpFilename ($aFile['file_name']);
  727. // Prefer reading a local-engine source straight from disk. It avoids a
  728. // self-referential HTTP round trip for every transcode (a small speed-up),
  729. // and it works where the app cannot reach its own public URL from inside
  730. // (e.g. a container whose BX_DOL_URL_ROOT is not routable locally).
  731. $sLocalPath = $oStorageOriginal->getFileLocalPath($mixedHandler);
  732. if ($sLocalPath && @copy($sLocalPath, $sTmpFile))
  733. return $sTmpFile;
  734. // Fallback: fetch over HTTP (remote storage engines, or if the disk copy failed).
  735. $sUrl = $oStorageOriginal->getFileUrlById($mixedHandler);
  736. if (!$sUrl) {
  737. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: storeFileLocally_Storage failed, file({$mixedHandler}) wasn't found in source storage ({$this->_aObject['source_params']['object']})", BX_LOG_ERR);
  738. return false;
  739. }
  740. $sFileData = bx_file_get_contents ($sUrl, [], 'get', []);
  741. if (false === $sFileData) {
  742. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: storeFileLocally_Storage failed, getting file from URL({$sUrl}) failed", BX_LOG_ERR);
  743. return false;
  744. }
  745. if (!file_put_contents($sTmpFile, $sFileData)) {
  746. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: storeFileLocally_Storage failed, file save failed ({$sTmpFile})", BX_LOG_ERR);
  747. return false;
  748. }
  749. return $sTmpFile;
  750. }
  751. protected function storeFileLocally_Queue ($mixedHandler)
  752. {
  753. $aQueue = $this->_oDb->getFromQueue($mixedHandler);
  754. if (!$aQueue || !$aQueue['file_url_source']) {
  755. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: storeFileLocally_Queue failed, file ({$mixedHandler}) wasn't found in queue", BX_LOG_ERR);
  756. return false;
  757. }
  758. $sFileData = bx_file_get_contents ($aQueue['file_url_source'], [], 'get', []);
  759. if (false === $sFileData) {
  760. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: storeFileLocally_Queue failed, getting file from URL({$aQueue['file_url_source']}) failed", BX_LOG_ERR);
  761. return false;
  762. }
  763. $sTmpFile = $this->getTmpFilename ($mixedHandler);
  764. if (!file_put_contents($sTmpFile, $sFileData)) {
  765. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: storeFileLocally_Queue failed, file save failed ({$sTmpFile})", BX_LOG_ERR);
  766. return false;
  767. }
  768. return $sTmpFile;
  769. }
  770. protected function getOrigFileUrl_Folder ($mixedHandler)
  771. {
  772. $sPath = $this->_aObject['source_params']['path'] . $mixedHandler;
  773. if (!file_exists(BX_DIRECTORY_PATH_ROOT . $sPath)) {
  774. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: getOrigFileUrl_Folder failed, source file ({$sPath}) doesn't exist", BX_LOG_ERR);
  775. return false;
  776. }
  777. return BX_DOL_URL_ROOT . $sPath;
  778. }
  779. protected function getOrigFileUrl_Storage ($mixedHandler)
  780. {
  781. $oStorageOriginal = BxDolStorage::getObjectInstance($this->_aObject['source_params']['object']);
  782. if (!$oStorageOriginal) {
  783. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: getOrigFileUrl_Storage failed, source storage ({$this->_aObject['source_params']['object']}) doesn't exist", BX_LOG_ERR);
  784. return false;
  785. }
  786. return $oStorageOriginal->getFileUrlById($mixedHandler);
  787. }
  788. protected function initFilters ()
  789. {
  790. if (false !== $this->_aObject['filters']) // filters are already initilized
  791. return;
  792. // get transcoder filters
  793. $aFilters = $this->_oDb->getTranscoderFilters();
  794. $this->_aObject['filters'] = array();
  795. foreach ($aFilters as $aFilter) {
  796. $aFilter['filter_params'] = $aFilter['filter_params'] ? unserialize($aFilter['filter_params']) : array();
  797. $this->_aObject['filters'][] = $aFilter;
  798. }
  799. }
  800. protected function clearCacheDB()
  801. {
  802. $oCacheDb = $this->_oDb->getDbCacheObject();
  803. return $oCacheDb->removeAllByPrefix('db_');
  804. }
  805. public function getDevicePixelRatioHandlerSuffix ()
  806. {
  807. $iDevicePixelRatio = $this->getDevicePixelRatio ();
  808. if ($iDevicePixelRatio >= 2)
  809. return $this->_sRetinaSuffix;
  810. return '';
  811. }
  812. protected function processHandlerForRetinaDevice ($mixedHandler)
  813. {
  814. return '' . $mixedHandler . $this->getDevicePixelRatioHandlerSuffix ();
  815. }
  816. static protected function _callFuncForObjectsArray ($mixed, $sFunc)
  817. {
  818. if (!is_array($mixed))
  819. $mixed = array($mixed);
  820. foreach ($mixed as $sObject) {
  821. $oTranscoder = self::getObjectInstance($sObject);
  822. if ($oTranscoder)
  823. $oTranscoder->$sFunc();
  824. }
  825. }
  826. /**
  827. * Get filter params
  828. * @param $sName filter name
  829. * @return array of filter params if filter was found, or false if given filter isn't enabled for the transcoder
  830. */
  831. public function getFilterParams ($sName)
  832. {
  833. $this->initFilters ();
  834. foreach ($this->_aObject['filters'] as $aParams)
  835. if ($sName == $aParams['filter'])
  836. return $aParams['filter_params'];
  837. return false;
  838. }
  839. public function getMaxResizeDimention ($sFilter = 'Resize', $sKey = '')
  840. {
  841. $a = $this->_oDb->getTranscoderFilters($sFilter);
  842. if (!$a)
  843. return 0;
  844. $aFilter = array_pop($a);
  845. $aFilterParams = $aFilter['filter_params'] ? unserialize($aFilter['filter_params']) : array();
  846. if ($sKey)
  847. return isset($aFilterParams[$sKey]) ? $aFilterParams[$sKey] : 0;
  848. return $aFilterParams ? max(isset($aFilterParams['w']) ? $aFilterParams['w'] : 0, isset($aFilterParams['h']) ? $aFilterParams['h'] : 0) : 0;
  849. }
  850. public function isMobile ()
  851. {
  852. return bx_is_mobile();
  853. }
  854. }
  855. /** @} */