BxDolTranscoder.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolStorage');
  10. bx_import('BxDolTranscoder');
  11. bx_import('BxDolTranscoderQuery');
  12. define('BX_DOL_QUEUE_PENDING', 'pending'); ///< file is pending for processing
  13. define('BX_DOL_QUEUE_PROCESSING', 'processing'); ///< file is in the process
  14. define('BX_DOL_QUEUE_COMPLETE', 'complete'); ///< file is already processed
  15. define('BX_DOL_QUEUE_FAILED', 'failed'); ///< file processing was failed
  16. define('BX_DOL_QUEUE_DELETE', 'delete'); ///< file is subject to delete
  17. /**
  18. * Base class for @see BxDolTranscoderImage and @see BxDolTranscoderVideo transcoders
  19. */
  20. class BxDolTranscoder extends BxDol implements iBxDolFactoryObject
  21. {
  22. protected $_sLog = ''; ///< file conversion log
  23. protected $_aObject; ///< object properties
  24. protected $_oStorage; ///< storage object, transcoded files are stored here
  25. protected $_oDb; ///< database queries object
  26. protected $_sRetinaSuffix = '@2x'; ///< handler suffix for retina image file
  27. protected $_sQueueTable = ''; ///< table with queued files for transcoding
  28. protected $_sQueueStorage = ''; ///< storage object to storage temporary files
  29. /**
  30. * constructor
  31. */
  32. protected function __construct($aObject, $oStorage)
  33. {
  34. parent::__construct();
  35. $this->_aObject = $aObject;
  36. $this->_oStorage = $oStorage;
  37. $this->_oDb = null;
  38. }
  39. /**
  40. * Get transcode object instance.
  41. * @param $sObject - name of transcode object.
  42. * @return false on error or instance of transcoder class.
  43. */
  44. public static function getObjectInstance($sObject)
  45. {
  46. if (isset($GLOBALS['bxDolClasses'][__CLASS__ . '!' . $sObject]))
  47. return $GLOBALS['bxDolClasses'][__CLASS__ . '!'.$sObject];
  48. // get transcode object
  49. $aObject = BxDolTranscoderQuery::getTranscoderObject($sObject);
  50. if (!$aObject || !is_array($aObject))
  51. return false;
  52. if ($aObject['source_params'])
  53. $aObject['source_params'] = unserialize($aObject['source_params']);
  54. $aObject['filters'] = false; // filters are initialized on demand
  55. // create storage object to store transcoded data
  56. $oStorage = BxDolStorage::getObjectInstance($aObject['storage_object']);
  57. if (!$oStorage)
  58. return false;
  59. // create instance
  60. $sClass = $aObject['override_class_name'] ? $aObject['override_class_name'] : 'BxDolTranscoderImage';
  61. if (!empty($aObject['override_class_file']))
  62. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  63. else
  64. bx_import($sClass);
  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. $aQueue = $o->getDb()->getFromQueue($r['file_id_source']);
  107. if (!$aQueue || $aQueue['status'] != BX_DOL_QUEUE_PENDING)
  108. continue;
  109. $o->transcode($aQueue['file_id_source'], $aQueue['profile_id']);
  110. }
  111. }
  112. static public function pruneQueue ()
  113. {
  114. $a = BxDolTranscoderQuery::getForDeletionFromQueue (gethostname());
  115. if (!$a)
  116. return false;
  117. foreach ($a as $aQueue) {
  118. $o = self::getObjectInstance($aQueue['transcoder_object']);
  119. $o->deleteFromQueue($aQueue['file_id_source']);
  120. }
  121. }
  122. /**
  123. * Store completed files from tmp queue storage to the final storage
  124. * Must be performed on the actual site.
  125. */
  126. static public function processCompleted ()
  127. {
  128. $a = BxDolTranscoderQuery::getCompletedFromQueue ();
  129. foreach ($a as $r) {
  130. $o = self::getObjectInstance($r['transcoder_object']);
  131. $aQueue = $o->getDb()->getFromQueue($r['file_id_source']);
  132. if (!$aQueue || $aQueue['status'] != BX_DOL_QUEUE_COMPLETE)
  133. continue;
  134. if ($aQueue['file_url_result'] && $aQueue['file_id_result']) {
  135. // get transcoded file from provided URL
  136. $sFileData = bx_file_get_contents ($aQueue['file_url_result']);
  137. if (false === $sFileData) {
  138. $o->getDb()->updateQueueStatus($aQueue['file_id_source'], BX_DOL_QUEUE_FAILED, "download file failed: {$aQueue['file_url_result']}\n");
  139. continue;
  140. }
  141. $sTmpFile = $o->getTmpFilename ('.' . $aQueue['file_ext_result']);
  142. if (!file_put_contents($sTmpFile, $sFileData)) {
  143. $o->getDb()->updateQueueStatus($aQueue['file_id_source'], BX_DOL_QUEUE_FAILED, "store downloaded file failed\n");
  144. @unlink($sTmpFile);
  145. continue;
  146. }
  147. // store downloaded file in final storage
  148. if ($o->storeTranscodedFile($aQueue['file_id_source'], $sTmpFile, $aQueue['profile_id']))
  149. $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)
  150. else
  151. $o->getDb()->updateQueueStatus($aQueue['file_id_source'], BX_DOL_QUEUE_FAILED, "store file failed:\n" . $o->getLog());
  152. // delete tmp local file
  153. @unlink($sTmpFile);
  154. }
  155. }
  156. }
  157. /**
  158. * Register handlers array
  159. * It can be called upon module enable event.
  160. * @param $mixed array of transcoders objects, or just one object
  161. */
  162. static public function registerHandlersArray ($mixed)
  163. {
  164. self::_callFuncForObjectsArray ($mixed, 'registerHandlers');
  165. }
  166. /**
  167. * Unregister handlers array
  168. * It can be called upon module disbale event.
  169. * @param $mixed array of transcoders objects, or just one object
  170. */
  171. static public function unregisterHandlersArray ($mixed)
  172. {
  173. self::_callFuncForObjectsArray ($mixed, 'unregisterHandlers');
  174. }
  175. /**
  176. * Cleanup (queue for deletion all resized files)
  177. * It can be called upon module disbale event.
  178. * @param $mixed array of transcoders objects, or just one object
  179. */
  180. static public function cleanupObjectsArray ($mixed)
  181. {
  182. self::_callFuncForObjectsArray ($mixed, 'cleanup');
  183. }
  184. /**
  185. * Called automatically, upon local(transcoded) file deletetion.
  186. */
  187. static public function onAlertResponseFileDeleteLocal ($oAlert, $sObject)
  188. {
  189. $oTranscoder = self::getObjectInstance($sObject);
  190. if (!$oTranscoder)
  191. return;
  192. if ($oAlert->sAction != 'file_deleted')
  193. return;
  194. $oTranscoder->onDeleteFileLocal($oAlert->iObject);
  195. }
  196. /**
  197. * Called automatically, upon original file deletetion.
  198. */
  199. static public function onAlertResponseFileDeleteOrig ($oAlert, $sObject)
  200. {
  201. $oTranscoder = self::getObjectInstance($sObject);
  202. if (!$oTranscoder)
  203. return;
  204. if ($oAlert->sAction != 'file_deleted')
  205. return;
  206. $oTranscoder->onDeleteFileOrig($oAlert->iObject);
  207. }
  208. /**
  209. * Called automatically, upon local(transcoded) file deletetion.
  210. */
  211. public function onDeleteFileLocal($iFileId)
  212. {
  213. return $this->_oDb->deleteFileTraces($iFileId);
  214. }
  215. /**
  216. * Called automatically, upon original file deletetion.
  217. */
  218. public function onDeleteFileOrig($mixedHandler)
  219. {
  220. // delete main file
  221. $iFileId = $this->_oDb->getFileIdByHandler($mixedHandler);
  222. if (!$iFileId)
  223. return false;
  224. if (!$this->_oStorage->deleteFile($iFileId))
  225. return false;
  226. // delete retina file
  227. $iFileId = $this->_oDb->getFileIdByHandler($mixedHandler . $this->_sRetinaSuffix);
  228. if ($iFileId)
  229. $this->_oStorage->deleteFile($iFileId);
  230. return true;
  231. }
  232. /**
  233. * Register necessary alert handlers for automatic deletetion of transcoded data if source file is deleted.
  234. * Make sure that you call it once, before first usage, for example upon module installation.
  235. */
  236. public function registerHandlers ()
  237. {
  238. if (!$this->_oDb->registerHandlers ())
  239. return false;
  240. return $this->clearCacheDB();
  241. }
  242. /**
  243. * Unregister alert handlers for automatic deletetion of transcoded data if source file is deleted.
  244. * Make sure that you call it once, for example upon module uninstallation.
  245. */
  246. public function unregisterHandlers ()
  247. {
  248. if (!$this->_oDb->unregisterHandlers ())
  249. return false;
  250. return $this->clearCacheDB();
  251. }
  252. /**
  253. * Delete (queue for deletion) all resized files for the current object
  254. */
  255. public function cleanup ()
  256. {
  257. return $this->_oStorage->queueFilesForDeletionFromObject();
  258. }
  259. /**
  260. * Get storage object where transcoded data is stored
  261. */
  262. public function getStorage()
  263. {
  264. return $this->_oStorage;
  265. }
  266. /**
  267. * Get database object instance
  268. */
  269. public function getDb()
  270. {
  271. return $this->_oDb;
  272. }
  273. /**
  274. * Get transcoded file url.
  275. * If transcoded file is ready then direct url to the file is returned.
  276. * 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.
  277. * @params $mixedHandler - file handler
  278. * @return file url, or false on error.
  279. */
  280. public function getFileUrl($mixedHandler)
  281. {
  282. if ($this->isFileReady($mixedHandler)) {
  283. $mixedHandler = $this->processHandlerForRetinaDevice($mixedHandler);
  284. $iFileId = $this->_oDb->getFileIdByHandler($mixedHandler);
  285. if (!$iFileId)
  286. return false;
  287. $aFile = $this->_oStorage->getFile($iFileId);
  288. if (!$aFile)
  289. return false;
  290. if ($this->_aObject['atime_tracking'])
  291. $this->_oDb->updateAccessTime($mixedHandler);
  292. return $this->_oStorage->getFileUrlById($iFileId);
  293. }
  294. if ($this->_sQueueTable && !$this->addToQueue($mixedHandler)) // add file to queue (when queue is enabled) if it isn't transcoded yet
  295. return false;
  296. return $this->getFileUrlNotReady($mixedHandler);
  297. }
  298. /**
  299. * Check if transcoded data is available. No need to call it directly, it is called automatically when it is needed.
  300. * @params $mixedHandler - file handler
  301. * @params $isCheckOutdated - check if transcoded file is outdated
  302. * @return false if there is no ready transcoded file is available or it is outdated, true if file is ready
  303. */
  304. public function isFileReady ($mixedHandler, $isCheckOutdated = true)
  305. {
  306. $sMethodFileReady = 'isFileReady_' . $this->_aObject['source_type'];
  307. return $this->$sMethodFileReady($mixedHandler, $isCheckOutdated);
  308. }
  309. /**
  310. * Transcode file, no need to call it directly, it is called automatically when it is needed.
  311. * @params $mixedHandler - file handler
  312. * @params $iProfileId - optional profile id, to assign transcoded file ownership to, usually it is NOT assigned to any particular profile, so just leave it default
  313. * @return true on success, false on error
  314. */
  315. public function transcode ($mixedHandler, $iProfileId = 0)
  316. {
  317. $sExtChange = false;
  318. $this->_oDb->updateQueueStatus($mixedHandler, BX_DOL_QUEUE_PROCESSING, '', gethostname());
  319. // create tmp file locally
  320. $sSuffix = $this->_sQueueStorage ? 'Queue' : $this->_aObject['source_type'];
  321. $sMethodStoreFile = 'storeFileLocally_' . $sSuffix;
  322. $sTmpFile = $this->$sMethodStoreFile($mixedHandler);
  323. if (!$sTmpFile) {
  324. $this->_oDb->updateQueueStatus($mixedHandler, BX_DOL_QUEUE_FAILED, "store local tmp file failed ($sMethodStoreFile)");
  325. return false;
  326. }
  327. // appply filters to tmp file
  328. $this->initFilters ();
  329. foreach ($this->_aObject['filters'] as $aParams) {
  330. $this->clearLog();
  331. $sMethodFilter = 'applyFilter_' . $aParams['filter'];
  332. if (!method_exists($this, $sMethodFilter))
  333. continue;
  334. if (!$this->$sMethodFilter($sTmpFile, $aParams['filter_params'])) {
  335. unlink($sTmpFile);
  336. $this->_oDb->updateQueueStatus($mixedHandler, BX_DOL_QUEUE_FAILED, "filter failed: $sMethodFilter\n" . $this->getLog());
  337. return false;
  338. }
  339. if (!empty($aParams['filter_params']['force_type'])) {
  340. switch ($aParams['filter_params']['force_type']) {
  341. case 'jpeg':
  342. case 'jpg':
  343. $sExtChange = 'jpg';
  344. break;
  345. default:
  346. $sExtChange = $aParams['filter_params']['force_type'];
  347. }
  348. }
  349. }
  350. if ($sExtChange) {
  351. $iDotPos = strrpos($sTmpFile, '.');
  352. $sExtOld = false === $iDotPos ? '' : substr($sTmpFile, $iDotPos+1);
  353. if ($sExtOld != $sExtChange) {
  354. $sTmpFileOld = $sTmpFile;
  355. $sTmpFile = false === $iDotPos ? $sTmpFile . '.' . $sExtChange : substr_replace($sTmpFile, $sExtChange, $iDotPos+1, strlen($sExtOld));
  356. if (!rename($sTmpFileOld, $sTmpFile)) {
  357. @unlink($sTmpFileOld);
  358. $this->_oDb->updateQueueStatus($mixedHandler, BX_DOL_QUEUE_FAILED, "change file extension failed: $sTmpFileOld => $sTmpFile");
  359. return false;
  360. }
  361. }
  362. }
  363. $mixedHandler = $this->processHandlerForRetinaDevice($mixedHandler);
  364. $bRet = true;
  365. if ($this->_sQueueStorage) {
  366. $sFileUrlResult = '';
  367. if (!($iFileIdResult = $this->storeTranscodedFileInQueueStorage($mixedHandler, $sTmpFile, $sFileUrlResult)))
  368. $this->_oDb->updateQueueStatus($mixedHandler, BX_DOL_QUEUE_FAILED, "store queue tmp file failed:\n" . $this->getLog());
  369. else
  370. $this->_oDb->updateQueueStatus($mixedHandler, BX_DOL_QUEUE_COMPLETE, '', '', $iFileIdResult, $sFileUrlResult, $sExtChange);
  371. } else {
  372. $bRet = $this->storeTranscodedFile($mixedHandler, $sTmpFile, $iProfileId);
  373. if ($bRet)
  374. $this->deleteFromQueue($mixedHandler);
  375. else
  376. $this->_oDb->updateQueueStatus($mixedHandler, BX_DOL_QUEUE_FAILED, "store file failed:\n" . $this->getLog());
  377. }
  378. @unlink($sTmpFile);
  379. return $bRet;
  380. }
  381. /**
  382. * Delete outdated files by last access time.
  383. * @return number of pruned/deleted files
  384. */
  385. public function prune ()
  386. {
  387. $aFiles = $this->_oDb->getFilesForPruning();
  388. if (!$aFiles)
  389. return false;
  390. $iCount = 0;
  391. foreach ($aFiles as $r)
  392. $iCount += $this->_oStorage->deleteFile($r['file_id']) ? 1 : 0;
  393. return $iCount;
  394. }
  395. public function getDevicePixelRatio ()
  396. {
  397. if (isset($_COOKIE['devicePixelRatio']) && (!isset($this->_aObject['source_params']['disable_retina']) || !$this->_aObject['source_params']['disable_retina'])) {
  398. $iDevicePixelRatio = intval($_COOKIE['devicePixelRatio']);
  399. if ($iDevicePixelRatio >= 2)
  400. return 2;
  401. }
  402. return 1;
  403. }
  404. public function getOrigFileUrl ($mixedHandler)
  405. {
  406. $sMethodStoreFile = 'getOrigFileUrl_' . $this->_aObject['source_type'];
  407. return $this->$sMethodStoreFile($mixedHandler);
  408. }
  409. /**
  410. * Get file url when file isn't transcoded yet
  411. */
  412. public function getFileUrlNotReady($mixedHandler)
  413. {
  414. return false;
  415. }
  416. public function getTmpFilename ($sOverrideName = false)
  417. {
  418. if ($sOverrideName)
  419. return BX_DIRECTORY_PATH_TMP . rand(10000, 99999) . $sOverrideName;
  420. return tempnam(BX_DIRECTORY_PATH_TMP, $this->_aObject['object']);
  421. }
  422. public function clearLog()
  423. {
  424. $this->_sLog = '';
  425. }
  426. public function addToLog($s)
  427. {
  428. $this->_sLog .= $s;
  429. }
  430. public function getLog()
  431. {
  432. return $this->_sLog;
  433. }
  434. public function deleteFromQueue ($mixedHandler)
  435. {
  436. $sErrMsg = "delete queue tmp file failed:\n";
  437. $a = $this->_oDb->getFromQueue($mixedHandler);
  438. if (!$a || $a['server'] != gethostname()) // deletion must be performed on the server where it was transcoded
  439. return false;
  440. if ($a['file_id_result'] && $this->_sQueueStorage && 0 != strncmp($a['log'], $sErrMsg, strlen($sErrMsg))) {
  441. $oStorage = BxDolStorage::getObjectInstance($this->_sQueueStorage);
  442. if (!$oStorage->deleteFile($a['file_id_result'])) {
  443. $this->_oDb->updateQueueStatus($mixedHandler, BX_DOL_QUEUE_FAILED, $sErrMsg . $this->getLog());
  444. return false;
  445. }
  446. }
  447. return $this->_oDb->deleteFromQueue($mixedHandler);
  448. }
  449. public function storeTranscodedFile ($mixedHandler, $sTmpFile, $iProfileId)
  450. {
  451. $sMethodIsPrivate = 'isPrivate_' . $this->_aObject['source_type'];
  452. $isPrivate = $this->$sMethodIsPrivate($mixedHandler);
  453. $iFileId = $this->_oStorage->storeFileFromPath ($sTmpFile, $isPrivate, $iProfileId);
  454. if (!$iFileId) {
  455. $this->addToLog($this->_oStorage->getErrorString());
  456. return false;
  457. }
  458. if (!$this->_oDb->updateHandler($iFileId, $mixedHandler)) {
  459. $this->_oStorage->deleteFile($iFileId);
  460. return false;
  461. }
  462. $this->_oStorage->afterUploadCleanup($iFileId, $iProfileId);
  463. return true;
  464. }
  465. protected function storeTranscodedFileInQueueStorage ($mixedHandler, $sTmpFile, &$sFileUrlResult)
  466. {
  467. if (!$this->_sQueueStorage)
  468. return false;
  469. $oStorage = BxDolStorage::getObjectInstance($this->_sQueueStorage);
  470. $iFileIdResult = $oStorage->storeFileFromPath ($sTmpFile, 1, 0);
  471. if (!$iFileIdResult) {
  472. $this->addToLog($oStorage->getErrorString());
  473. return false;
  474. }
  475. if (!($sFileUrlResult = $oStorage->getFileUrlById($iFileIdResult))) {
  476. $this->_oStorage->deleteFile($iFileIdResult);
  477. return false;
  478. }
  479. $oStorage->afterUploadCleanup($iFileIdResult, 0);
  480. return $iFileIdResult;
  481. }
  482. protected function addToQueue($mixedHandler, $iProfileId = 0)
  483. {
  484. if ($this->_oDb->getFromQueue($mixedHandler))
  485. return true;
  486. $sFileUrl = $this->getOrigFileUrl($mixedHandler);
  487. return $this->_oDb->addToQueue ($mixedHandler, $sFileUrl, $iProfileId);
  488. }
  489. protected function isFileReady_Folder ($mixedHandlerOrig, $isCheckOutdated = true)
  490. {
  491. $mixedHandler = $this->processHandlerForRetinaDevice($mixedHandlerOrig);
  492. $iFileId = $this->_oDb->getFileIdByHandler($mixedHandler);
  493. if (!$iFileId)
  494. return false;
  495. $aFile = $this->_oStorage->getFile($iFileId);
  496. if (!$aFile)
  497. return false;
  498. if ($isCheckOutdated) { // warning, $isCheckOutdated is partially supported for Folder source type - file modification is not checked
  499. if ($this->_aObject['ts'] > $aFile['modified'] || !$this->getFilePath_Folder($mixedHandlerOrig)) { // if we changed transcoder object params or original file is deleted
  500. // delete file, so it will be recreated next time
  501. if ($this->_oStorage->deleteFile($aFile['id']))
  502. return false;
  503. }
  504. }
  505. return true;
  506. }
  507. protected function isFileReady_Storage ($mixedHandlerOrig, $isCheckOutdated = true)
  508. {
  509. $mixedHandler = $this->processHandlerForRetinaDevice($mixedHandlerOrig);
  510. $iFileId = $this->_oDb->getFileIdByHandler($mixedHandler);
  511. if (!$iFileId)
  512. return false;
  513. $aFile = $this->_oStorage->getFile($iFileId);
  514. if (!$aFile)
  515. return false;
  516. if ($isCheckOutdated) {
  517. $oStorageOriginal = BxDolStorage::getObjectInstance($this->_aObject['source_params']['object']);
  518. if ($oStorageOriginal) {
  519. $aFileOriginal = $oStorageOriginal->getFile($mixedHandlerOrig);
  520. if (!$aFileOriginal || $aFileOriginal['modified'] > $aFile['modified'] || $this->_aObject['ts'] > $aFile['modified']) { // if original file was changed OR we changed transcoder object params
  521. // delete file, so it will be recreated next time
  522. if ($this->_oStorage->deleteFile($aFile['id']))
  523. return false;
  524. }
  525. }
  526. }
  527. return true;
  528. }
  529. protected function isPrivate_Folder ($mixedHandler)
  530. {
  531. if ('no' == $this->_aObject['private'])
  532. return false;
  533. return true;
  534. }
  535. protected function isPrivate_Storage ($mixedHandler)
  536. {
  537. switch ($this->_aObject['private']) {
  538. case 'no':
  539. return false;
  540. case 'yes':
  541. return true;
  542. default:
  543. case 'auto':
  544. $oStorageOriginal = BxDolStorage::getObjectInstance($this->_aObject['source_params']['object']);
  545. if (!$oStorageOriginal)
  546. return true; // in case of error - make sure that file is not public accidentally
  547. $aFile = $oStorageOriginal->getFile($mixedHandler);
  548. return $oStorageOriginal->isFilePrivate($aFile['id']);
  549. }
  550. }
  551. protected function getFilePath_Folder ($mixedHandler)
  552. {
  553. $sPath = $this->_aObject['source_params']['path'] . $mixedHandler;
  554. if (!file_exists($sPath))
  555. $sPath = BX_DIRECTORY_PATH_ROOT . $sPath;
  556. if (!file_exists($sPath))
  557. return false;
  558. return $sPath;
  559. }
  560. protected function storeFileLocally_Folder ($mixedHandler)
  561. {
  562. $sPath = $this->getFilePath_Folder($mixedHandler);
  563. if (!$sPath)
  564. return false;
  565. $sTmpFile = $this->getTmpFilename ($mixedHandler);
  566. if (!copy($sPath, $sTmpFile))
  567. return false;
  568. return $sTmpFile;
  569. }
  570. protected function storeFileLocally_Storage ($mixedHandler)
  571. {
  572. $oStorageOriginal = BxDolStorage::getObjectInstance($this->_aObject['source_params']['object']);
  573. if (!$oStorageOriginal)
  574. return false;
  575. $aFile = $oStorageOriginal->getFile($mixedHandler);
  576. if (!$aFile)
  577. return false;
  578. $sUrl = $oStorageOriginal->getFileUrlById($mixedHandler);
  579. if (!$sUrl)
  580. return false;
  581. $sFileData = bx_file_get_contents ($sUrl);
  582. if (false === $sFileData)
  583. return false;
  584. $sTmpFile = $this->getTmpFilename ($aFile['file_name']);
  585. if (!file_put_contents($sTmpFile, $sFileData))
  586. return false;
  587. return $sTmpFile;
  588. }
  589. protected function storeFileLocally_Queue ($mixedHandler)
  590. {
  591. $aQueue = $this->_oDb->getFromQueue($mixedHandler);
  592. if (!$aQueue || !$aQueue['file_url_source'])
  593. return false;
  594. $sFileData = bx_file_get_contents ($aQueue['file_url_source']);
  595. if (false === $sFileData)
  596. return false;
  597. $sTmpFile = $this->getTmpFilename ($mixedHandler);
  598. if (!file_put_contents($sTmpFile, $sFileData))
  599. return false;
  600. return $sTmpFile;
  601. }
  602. protected function getOrigFileUrl_Folder ($mixedHandler)
  603. {
  604. $sPath = $this->_aObject['source_params']['path'] . $mixedHandler;
  605. if (!file_exists(BX_DIRECTORY_PATH_ROOT . $sPath))
  606. return false;
  607. return BX_DOL_URL_ROOT . $sPath;
  608. }
  609. protected function getOrigFileUrl_Storage ($mixedHandler)
  610. {
  611. $oStorageOriginal = BxDolStorage::getObjectInstance($this->_aObject['source_params']['object']);
  612. if (!$oStorageOriginal)
  613. return false;
  614. return $oStorageOriginal->getFileUrlById($mixedHandler);
  615. }
  616. protected function initFilters ()
  617. {
  618. if (false !== $this->_aObject['filters']) // filters are already initilized
  619. return;
  620. // get transcoder filters
  621. $aFilters = $this->_oDb->getTranscoderFilters();
  622. $this->_aObject['filters'] = array();
  623. foreach ($aFilters as $aFilter) {
  624. if ($aFilter['filter_params'])
  625. $aFilter['filter_params'] = unserialize($aFilter['filter_params']);
  626. $this->_aObject['filters'][] = $aFilter;
  627. }
  628. }
  629. protected function clearCacheDB()
  630. {
  631. $oCacheDb = $this->_oDb->getDbCacheObject();
  632. return $oCacheDb->removeAllByPrefix('db_');
  633. }
  634. public function getDevicePixelRatioHandlerSuffix ()
  635. {
  636. $iDevicePixelRatio = $this->getDevicePixelRatio ();
  637. if ($iDevicePixelRatio >= 2)
  638. return $this->_sRetinaSuffix;
  639. return '';
  640. }
  641. protected function processHandlerForRetinaDevice ($mixedHandler)
  642. {
  643. return '' . $mixedHandler . $this->getDevicePixelRatioHandlerSuffix ();
  644. }
  645. static protected function _callFuncForObjectsArray ($mixed, $sFunc)
  646. {
  647. if (!is_array($mixed))
  648. $mixed = array($mixed);
  649. foreach ($mixed as $sObject) {
  650. $oTranscoder = self::getObjectInstance($sObject);
  651. if ($oTranscoder)
  652. $oTranscoder->$sFunc();
  653. }
  654. }
  655. }
  656. /** @} */