BxDolTranscoder.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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 TridentCore Trident 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. /**
  15. * Base class for @see BxDolTranscoderImage and @see BxDolTranscoderVideo transcoders
  16. */
  17. class BxDolTranscoder extends BxDol implements iBxDolFactoryObject
  18. {
  19. protected $_sLog = ''; ///< file conversion log
  20. protected $_aObject; ///< object properties
  21. protected $_oStorage; ///< storage object, transcoded files are stored here
  22. protected $_oDb; ///< database queries object
  23. protected $_sRetinaSuffix = '@2x'; ///< handler suffix for retina image file
  24. protected $_sQueueTable = ''; ///< table with queued files for transcoding
  25. protected $_sQueueStorage = ''; ///< storage object to storage temporary files
  26. /**
  27. * constructor
  28. */
  29. protected function __construct($aObject, $oStorage)
  30. {
  31. parent::__construct();
  32. $this->_aObject = $aObject;
  33. $this->_oStorage = $oStorage;
  34. $this->_oDb = null;
  35. }
  36. /**
  37. * Get transcode object instance.
  38. * @param $sObject - name of transcode object.
  39. * @return false on error or instance of transcoder class.
  40. */
  41. public static function getObjectInstance($sObject)
  42. {
  43. if (isset($GLOBALS['bxDolClasses'][__CLASS__ . '!' . $sObject]))
  44. return $GLOBALS['bxDolClasses'][__CLASS__ . '!'.$sObject];
  45. // get transcode object
  46. $aObject = BxDolTranscoderQuery::getTranscoderObject($sObject);
  47. if (!$aObject || !is_array($aObject))
  48. return false;
  49. if ($aObject['source_params'])
  50. $aObject['source_params'] = unserialize($aObject['source_params']);
  51. $aObject['filters'] = false; // filters are initialized on demand
  52. // create storage object to store transcoded data
  53. $oStorage = BxDolStorage::getObjectInstance($aObject['storage_object']);
  54. if (!$oStorage)
  55. return false;
  56. // create instance
  57. $sClass = $aObject['override_class_name'] ? $aObject['override_class_name'] : 'BxDolTranscoderImage';
  58. if (!empty($aObject['override_class_file']))
  59. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  60. $o = new $sClass ($aObject, $oStorage);
  61. return ($GLOBALS['bxDolClasses'][__CLASS__ . '!' . $sObject] = $o);
  62. }
  63. /**
  64. * Delete outdated transcoed data from all transcoeeed objects, by last access time.
  65. * It called on cron, usually every day.
  66. * @return total number of pruned/deleted files
  67. */
  68. static public function pruning ()
  69. {
  70. $iCount = 0;
  71. $aObjects = self::getTranscoderObjects();
  72. foreach ($aObjects as $aObject) {
  73. if (!$aObject['atime_tracking'] || !$aObject['atime_pruning'])
  74. continue;
  75. $oTranscoder = self::getObjectInstance($aObject['object']);
  76. if (!$oTranscoder)
  77. continue;
  78. $iCount += $oTranscoder->prune();
  79. }
  80. return $iCount;
  81. }
  82. /**
  83. * Get transcoder objects array
  84. * @return array
  85. */
  86. static protected function getTranscoderObjects ()
  87. {
  88. return BxDolTranscoderQuery::getTranscoderObjects ();
  89. }
  90. /**
  91. * Process files queued for transcoding
  92. * Can be performed on separate server.
  93. */
  94. static public function processQueue ($bQueuePruning = true)
  95. {
  96. if ($bQueuePruning)
  97. self::pruneQueue();
  98. $a = BxDolTranscoderQuery::getNextInQueue (gethostname());
  99. foreach ($a as $r) {
  100. $o = self::getObjectInstance($r['transcoder_object']);
  101. $aQueue = $o->getDb()->getFromQueue($r['file_id_source']);
  102. if (!$aQueue || $aQueue['status'] != BX_DOL_QUEUE_PENDING)
  103. continue;
  104. $o->transcode($aQueue['file_id_source'], $aQueue['profile_id']);
  105. }
  106. }
  107. static public function pruneQueue ()
  108. {
  109. $a = BxDolTranscoderQuery::getForDeletionFromQueue (gethostname());
  110. if (!$a)
  111. return false;
  112. foreach ($a as $aQueue) {
  113. $o = self::getObjectInstance($aQueue['transcoder_object']);
  114. if ($o)
  115. $o->deleteFromQueue($aQueue['file_id_source']);
  116. else
  117. echoDbgLog('Transcoder object undefined - ' . $aQueue['transcoder_object']);
  118. }
  119. }
  120. /**
  121. * Store completed files from tmp queue storage to the final storage
  122. * Must be performed on the actual site.
  123. */
  124. static public function processCompleted ()
  125. {
  126. $a = BxDolTranscoderQuery::getCompletedFromQueue ();
  127. foreach ($a as $r) {
  128. $o = self::getObjectInstance($r['transcoder_object']);
  129. $aQueue = $o->getDb()->getFromQueue($r['file_id_source']);
  130. if (!$aQueue || $aQueue['status'] != BX_DOL_QUEUE_COMPLETE)
  131. continue;
  132. if ($aQueue['file_url_result'] && $aQueue['file_id_result']) {
  133. // get transcoded file from provided URL
  134. $sFileData = bx_file_get_contents ($aQueue['file_url_result']);
  135. if (false === $sFileData) {
  136. $o->getDb()->updateQueueStatus($aQueue['file_id_source'], BX_DOL_QUEUE_FAILED, "download file failed: {$aQueue['file_url_result']}\n");
  137. continue;
  138. }
  139. $sTmpFile = $o->getTmpFilename ('.' . $aQueue['file_ext_result']);
  140. if (!file_put_contents($sTmpFile, $sFileData)) {
  141. $o->getDb()->updateQueueStatus($aQueue['file_id_source'], BX_DOL_QUEUE_FAILED, "store downloaded file failed\n");
  142. @unlink($sTmpFile);
  143. continue;
  144. }
  145. // store downloaded file in final storage
  146. if ($o->storeTranscodedFile($aQueue['file_id_source'], $sTmpFile, $aQueue['profile_id']))
  147. $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)
  148. else
  149. $o->getDb()->updateQueueStatus($aQueue['file_id_source'], BX_DOL_QUEUE_FAILED, "store file failed:\n" . $o->getLog());
  150. // delete tmp local file
  151. @unlink($sTmpFile);
  152. }
  153. }
  154. }
  155. /**
  156. * Register handlers array
  157. * It can be called upon module enable event.
  158. * @param $mixed array of transcoders objects, or just one object
  159. */
  160. static public function registerHandlersArray ($mixed)
  161. {
  162. self::_callFuncForObjectsArray ($mixed, 'registerHandlers');
  163. }
  164. /**
  165. * Unregister handlers array
  166. * It can be called upon module disbale event.
  167. * @param $mixed array of transcoders objects, or just one object
  168. */
  169. static public function unregisterHandlersArray ($mixed)
  170. {
  171. self::_callFuncForObjectsArray ($mixed, 'unregisterHandlers');
  172. }
  173. /**
  174. * Cleanup (queue for deletion all resized files)
  175. * It can be called upon module disbale event.
  176. * @param $mixed array of transcoders objects, or just one object
  177. */
  178. static public function cleanupObjectsArray ($mixed)
  179. {
  180. self::_callFuncForObjectsArray ($mixed, 'cleanup');
  181. }
  182. /**
  183. * Called automatically, upon local(transcoded) file deletetion.
  184. */
  185. static public function onAlertResponseFileDeleteLocal ($oAlert, $sObject)
  186. {
  187. $oTranscoder = self::getObjectInstance($sObject);
  188. if (!$oTranscoder)
  189. return;
  190. if ($oAlert->sAction != 'file_deleted')
  191. return;
  192. $oTranscoder->onDeleteFileLocal($oAlert->iObject);
  193. }
  194. /**
  195. * Called automatically, upon original file deletetion.
  196. */
  197. static public function onAlertResponseFileDeleteOrig ($oAlert, $sObject)
  198. {
  199. $oTranscoder = self::getObjectInstance($sObject);
  200. if (!$oTranscoder)
  201. return;
  202. if ($oAlert->sAction != 'file_deleted')
  203. return;
  204. $oTranscoder->onDeleteFileOrig($oAlert->iObject);
  205. }
  206. /**
  207. * Called automatically, upon local(transcoded) file deletetion.
  208. */
  209. public function onDeleteFileLocal($iFileId)
  210. {
  211. return $this->_oDb->deleteFileTraces($iFileId);
  212. }
  213. /**
  214. * Called automatically, upon original file deletetion.
  215. */
  216. public function onDeleteFileOrig($mixedHandler)
  217. {
  218. // deleted files queued for transcoding
  219. $this->_oDb->deleteFromQueue ($mixedHandler);
  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. * alias for @see getFileUrl
  300. */
  301. public function getFileUrlById($mixedHandler)
  302. {
  303. return $this->getFileUrl($mixedHandler);
  304. }
  305. /**
  306. * Check if transcoded data is available. No need to call it directly, it is called automatically when it is needed.
  307. * @params $mixedHandler - file handler
  308. * @params $isCheckOutdated - check if transcoded file is outdated
  309. * @return false if there is no ready transcoded file is available or it is outdated, true if file is ready
  310. */
  311. public function isFileReady ($mixedHandler, $isCheckOutdated = true)
  312. {
  313. $sMethodFileReady = 'isFileReady_' . $this->_aObject['source_type'];
  314. return $this->$sMethodFileReady($mixedHandler, $isCheckOutdated);
  315. }
  316. /**
  317. * Transcode file, no need to call it directly, it is called automatically when it is needed.
  318. * @params $mixedHandler - file handler
  319. * @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
  320. * @return true on success, false on error
  321. */
  322. public function transcode ($mixedHandler, $iProfileId = 0)
  323. {
  324. $sExtChange = false;
  325. $this->_oDb->updateQueueStatus($mixedHandler, BX_DOL_QUEUE_PROCESSING, '', gethostname());
  326. // create tmp file locally
  327. $sSuffix = $this->_sQueueStorage ? 'Queue' : $this->_aObject['source_type'];
  328. $sMethodStoreFile = 'storeFileLocally_' . $sSuffix;
  329. $sTmpFile = $this->$sMethodStoreFile($mixedHandler);
  330. if (!$sTmpFile) {
  331. $this->_oDb->updateQueueStatus($mixedHandler, BX_DOL_QUEUE_FAILED, "store local tmp file failed ($sMethodStoreFile)");
  332. return false;
  333. }
  334. // appply filters to tmp file
  335. $this->initFilters ();
  336. foreach ($this->_aObject['filters'] as $aParams) {
  337. $this->clearLog();
  338. $sMethodFilter = 'applyFilter_' . $aParams['filter'];
  339. if (!method_exists($this, $sMethodFilter))
  340. continue;
  341. if (!$this->$sMethodFilter($sTmpFile, $aParams['filter_params'])) {
  342. unlink($sTmpFile);
  343. $this->_oDb->updateQueueStatus($mixedHandler, BX_DOL_QUEUE_FAILED, "filter failed: $sMethodFilter\n" . $this->getLog());
  344. return false;
  345. }
  346. if (!empty($aParams['filter_params']['force_type'])) {
  347. switch ($aParams['filter_params']['force_type']) {
  348. case 'jpeg':
  349. case 'jpg':
  350. $sExtChange = 'jpg';
  351. break;
  352. default:
  353. $sExtChange = $aParams['filter_params']['force_type'];
  354. }
  355. }
  356. if ($sExtChange) {
  357. $iDotPos = strrpos($sTmpFile, '.');
  358. $sExtOld = false === $iDotPos ? '' : substr($sTmpFile, $iDotPos+1);
  359. if ($sExtOld != $sExtChange) {
  360. $sTmpFileOld = $sTmpFile;
  361. $sTmpFile = false === $iDotPos ? $sTmpFile . '.' . $sExtChange : substr_replace($sTmpFile, $sExtChange, $iDotPos+1, strlen($sExtOld));
  362. if (!rename($sTmpFileOld, $sTmpFile)) {
  363. @unlink($sTmpFileOld);
  364. $this->_oDb->updateQueueStatus($mixedHandler, BX_DOL_QUEUE_FAILED, "change file extension failed: $sTmpFileOld => $sTmpFile");
  365. return false;
  366. }
  367. }
  368. }
  369. }
  370. $mixedHandler = $this->processHandlerForRetinaDevice($mixedHandler);
  371. $bRet = true;
  372. if ($this->_sQueueStorage) {
  373. $sFileUrlResult = '';
  374. if (!($iFileIdResult = $this->storeTranscodedFileInQueueStorage($mixedHandler, $sTmpFile, $sFileUrlResult)))
  375. $this->_oDb->updateQueueStatus($mixedHandler, BX_DOL_QUEUE_FAILED, "store queue tmp file failed:\n" . $this->getLog());
  376. else
  377. $this->_oDb->updateQueueStatus($mixedHandler, BX_DOL_QUEUE_COMPLETE, '', '', $iFileIdResult, $sFileUrlResult, $sExtChange);
  378. } else {
  379. $bRet = $this->storeTranscodedFile($mixedHandler, $sTmpFile, $iProfileId);
  380. if ($bRet)
  381. $this->deleteFromQueue($mixedHandler);
  382. else
  383. $this->_oDb->updateQueueStatus($mixedHandler, BX_DOL_QUEUE_FAILED, "store file failed:\n" . $this->getLog());
  384. }
  385. @unlink($sTmpFile);
  386. return $bRet;
  387. }
  388. /**
  389. * Delete outdated files by last access time.
  390. * @return number of pruned/deleted files
  391. */
  392. public function prune ()
  393. {
  394. $aFiles = $this->_oDb->getFilesForPruning();
  395. if (!$aFiles)
  396. return false;
  397. $iCount = 0;
  398. foreach ($aFiles as $r)
  399. $iCount += $this->_oStorage->deleteFile($r['file_id']) ? 1 : 0;
  400. return $iCount;
  401. }
  402. public function getDevicePixelRatio ()
  403. {
  404. if (isset($_COOKIE['devicePixelRatio']) && (!isset($this->_aObject['source_params']['disable_retina']) || !$this->_aObject['source_params']['disable_retina'])) {
  405. $iDevicePixelRatio = intval($_COOKIE['devicePixelRatio']);
  406. if ($iDevicePixelRatio >= 2)
  407. return 2;
  408. }
  409. return 1;
  410. }
  411. public function getOrigFileUrl ($mixedHandler)
  412. {
  413. $sMethodStoreFile = 'getOrigFileUrl_' . $this->_aObject['source_type'];
  414. return $this->$sMethodStoreFile($mixedHandler);
  415. }
  416. /**
  417. * Get file url when file isn't transcoded yet
  418. */
  419. public function getFileUrlNotReady($mixedHandler)
  420. {
  421. return false;
  422. }
  423. public function getTmpFilename ($sOverrideName = false)
  424. {
  425. if ($sOverrideName)
  426. return BX_DIRECTORY_PATH_TMP . rand(10000, 99999) . $sOverrideName;
  427. return tempnam(BX_DIRECTORY_PATH_TMP, $this->_aObject['object']);
  428. }
  429. public function clearLog()
  430. {
  431. $this->_sLog = '';
  432. }
  433. public function addToLog($s)
  434. {
  435. $this->_sLog .= $s;
  436. }
  437. public function getLog()
  438. {
  439. return $this->_sLog;
  440. }
  441. public function deleteFromQueue ($mixedHandler)
  442. {
  443. $sErrMsg = "delete queue tmp file failed:\n";
  444. $a = $this->_oDb->getFromQueue($mixedHandler);
  445. if (!$a || $a['server'] != gethostname()) // deletion must be performed on the server where it was transcoded
  446. return false;
  447. if ($a['file_id_result'] && $this->_sQueueStorage && 0 != strncmp($a['log'], $sErrMsg, strlen($sErrMsg))) {
  448. $oStorage = BxDolStorage::getObjectInstance($this->_sQueueStorage);
  449. if (!$oStorage->deleteFile($a['file_id_result'])) {
  450. $this->_oDb->updateQueueStatus($mixedHandler, BX_DOL_QUEUE_FAILED, $sErrMsg . $this->getLog());
  451. return false;
  452. }
  453. }
  454. return $this->_oDb->deleteFromQueue($mixedHandler);
  455. }
  456. public function storeTranscodedFile ($mixedHandler, $sTmpFile, $iProfileId)
  457. {
  458. $sMethodIsPrivate = 'isPrivate_' . $this->_aObject['source_type'];
  459. $isPrivate = $this->$sMethodIsPrivate($mixedHandler);
  460. $iFileId = $this->_oStorage->storeFileFromPath ($sTmpFile, $isPrivate, $iProfileId);
  461. if (!$iFileId) {
  462. $this->addToLog($this->_oStorage->getErrorString());
  463. return false;
  464. }
  465. if (!$this->_oDb->updateHandler($iFileId, $mixedHandler)) {
  466. $this->_oStorage->deleteFile($iFileId);
  467. return false;
  468. }
  469. $this->_oStorage->afterUploadCleanup($iFileId, $iProfileId);
  470. return true;
  471. }
  472. protected function storeTranscodedFileInQueueStorage ($mixedHandler, $sTmpFile, &$sFileUrlResult)
  473. {
  474. if (!$this->_sQueueStorage)
  475. return false;
  476. $oStorage = BxDolStorage::getObjectInstance($this->_sQueueStorage);
  477. $iFileIdResult = $oStorage->storeFileFromPath ($sTmpFile, 1, 0);
  478. if (!$iFileIdResult) {
  479. $this->addToLog($oStorage->getErrorString());
  480. return false;
  481. }
  482. if (!($sFileUrlResult = $oStorage->getFileUrlById($iFileIdResult))) {
  483. $this->_oStorage->deleteFile($iFileIdResult);
  484. return false;
  485. }
  486. $oStorage->afterUploadCleanup($iFileIdResult, 0);
  487. return $iFileIdResult;
  488. }
  489. protected function addToQueue($mixedHandler, $iProfileId = 0)
  490. {
  491. if ($this->_oDb->getFromQueue($mixedHandler))
  492. return true;
  493. $sFileUrl = $this->getOrigFileUrl($mixedHandler);
  494. return $this->_oDb->addToQueue ($mixedHandler, $sFileUrl, $iProfileId);
  495. }
  496. protected function applyFilter_Resize ($sFile, $aParams)
  497. {
  498. $o = BxDolImageResize::getInstance();
  499. $o->removeCropOptions ();
  500. $o->setSize (isset($aParams['w']) ? round($aParams['w'] * $this->getDevicePixelRatio()) : null, isset($aParams['h']) ? round($aParams['h'] * $this->getDevicePixelRatio()) : null);
  501. if (isset($aParams['crop_resize']) && $aParams['crop_resize'])
  502. $o->setAutoCrop (true);
  503. elseif (isset($aParams['square_resize']) && $aParams['square_resize'])
  504. $o->setSquareResize (true);
  505. else
  506. $o->setSquareResize (false);
  507. if (IMAGE_ERROR_SUCCESS == $o->resize($sFile))
  508. return true;
  509. return false;
  510. }
  511. protected function isFileReady_Folder ($mixedHandlerOrig, $isCheckOutdated = true)
  512. {
  513. $mixedHandler = $this->processHandlerForRetinaDevice($mixedHandlerOrig);
  514. $iFileId = $this->_oDb->getFileIdByHandler($mixedHandler);
  515. if (!$iFileId)
  516. return false;
  517. $aFile = $this->_oStorage->getFile($iFileId);
  518. if (!$aFile)
  519. return false;
  520. if ($isCheckOutdated) { // warning, $isCheckOutdated is partially supported for Folder source type - file modification is not checked
  521. if ($this->_aObject['ts'] > $aFile['modified'] || !$this->getFilePath_Folder($mixedHandlerOrig)) { // if we changed transcoder object params or original file is deleted
  522. // delete file, so it will be recreated next time
  523. if ($this->_oStorage->deleteFile($aFile['id']))
  524. return false;
  525. }
  526. }
  527. return true;
  528. }
  529. protected function isFileReady_Storage ($mixedHandlerOrig, $isCheckOutdated = true)
  530. {
  531. $mixedHandler = $this->processHandlerForRetinaDevice($mixedHandlerOrig);
  532. $iFileId = $this->_oDb->getFileIdByHandler($mixedHandler);
  533. if (!$iFileId)
  534. return false;
  535. $aFile = $this->_oStorage->getFile($iFileId);
  536. if (!$aFile)
  537. return false;
  538. if ($isCheckOutdated) {
  539. $oStorageOriginal = BxDolStorage::getObjectInstance($this->_aObject['source_params']['object']);
  540. if ($oStorageOriginal) {
  541. $aFileOriginal = $oStorageOriginal->getFile($mixedHandlerOrig);
  542. if (!$aFileOriginal || $aFileOriginal['modified'] > $aFile['modified'] || $this->_aObject['ts'] >= $aFile['modified']) { // if original file was changed OR we changed transcoder object params
  543. // delete file, so it will be recreated next time
  544. if ($this->_oStorage->deleteFile($aFile['id']))
  545. return false;
  546. }
  547. }
  548. }
  549. return true;
  550. }
  551. protected function isPrivate_Folder ($mixedHandler)
  552. {
  553. if ('no' == $this->_aObject['private'])
  554. return false;
  555. return true;
  556. }
  557. protected function isPrivate_Storage ($mixedHandler)
  558. {
  559. switch ($this->_aObject['private']) {
  560. case 'no':
  561. return false;
  562. case 'yes':
  563. return true;
  564. default:
  565. case 'auto':
  566. $oStorageOriginal = BxDolStorage::getObjectInstance($this->_aObject['source_params']['object']);
  567. if (!$oStorageOriginal)
  568. return true; // in case of error - make sure that file is not public accidentally
  569. $aFile = $oStorageOriginal->getFile($mixedHandler);
  570. return $oStorageOriginal->isFilePrivate($aFile['id']);
  571. }
  572. }
  573. protected function getFilePath_Folder ($mixedHandler)
  574. {
  575. $sPath = $this->_aObject['source_params']['path'] . $mixedHandler;
  576. if (!file_exists($sPath))
  577. $sPath = BX_DIRECTORY_PATH_ROOT . $sPath;
  578. if (!file_exists($sPath))
  579. return false;
  580. return $sPath;
  581. }
  582. protected function storeFileLocally_Folder ($mixedHandler)
  583. {
  584. $sPath = $this->getFilePath_Folder($mixedHandler);
  585. if (!$sPath)
  586. return false;
  587. $sTmpFile = $this->getTmpFilename ($mixedHandler);
  588. if (!copy($sPath, $sTmpFile))
  589. return false;
  590. return $sTmpFile;
  591. }
  592. protected function storeFileLocally_Storage ($mixedHandler)
  593. {
  594. $oStorageOriginal = BxDolStorage::getObjectInstance($this->_aObject['source_params']['object']);
  595. if (!$oStorageOriginal)
  596. return false;
  597. $aFile = $oStorageOriginal->getFile($mixedHandler);
  598. if (!$aFile)
  599. return false;
  600. $sUrl = $oStorageOriginal->getFileUrlById($mixedHandler);
  601. if (!$sUrl)
  602. return false;
  603. $sFileData = bx_file_get_contents ($sUrl);
  604. if (false === $sFileData)
  605. return false;
  606. $sTmpFile = $this->getTmpFilename ($aFile['file_name']);
  607. if (!file_put_contents($sTmpFile, $sFileData))
  608. return false;
  609. return $sTmpFile;
  610. }
  611. protected function storeFileLocally_Queue ($mixedHandler)
  612. {
  613. $aQueue = $this->_oDb->getFromQueue($mixedHandler);
  614. if (!$aQueue || !$aQueue['file_url_source'])
  615. return false;
  616. $sFileData = bx_file_get_contents ($aQueue['file_url_source']);
  617. if (false === $sFileData)
  618. return false;
  619. $sTmpFile = $this->getTmpFilename ($mixedHandler);
  620. if (!file_put_contents($sTmpFile, $sFileData))
  621. return false;
  622. return $sTmpFile;
  623. }
  624. protected function getOrigFileUrl_Folder ($mixedHandler)
  625. {
  626. $sPath = $this->_aObject['source_params']['path'] . $mixedHandler;
  627. if (!file_exists(BX_DIRECTORY_PATH_ROOT . $sPath))
  628. return false;
  629. return BX_DOL_URL_ROOT . $sPath;
  630. }
  631. protected function getOrigFileUrl_Storage ($mixedHandler)
  632. {
  633. $oStorageOriginal = BxDolStorage::getObjectInstance($this->_aObject['source_params']['object']);
  634. if (!$oStorageOriginal)
  635. return false;
  636. return $oStorageOriginal->getFileUrlById($mixedHandler);
  637. }
  638. protected function initFilters ()
  639. {
  640. if (false !== $this->_aObject['filters']) // filters are already initilized
  641. return;
  642. // get transcoder filters
  643. $aFilters = $this->_oDb->getTranscoderFilters();
  644. $this->_aObject['filters'] = array();
  645. foreach ($aFilters as $aFilter) {
  646. $aFilter['filter_params'] = $aFilter['filter_params'] ? unserialize($aFilter['filter_params']) : array();
  647. $this->_aObject['filters'][] = $aFilter;
  648. }
  649. }
  650. protected function clearCacheDB()
  651. {
  652. $oCacheDb = $this->_oDb->getDbCacheObject();
  653. return $oCacheDb->removeAllByPrefix('db_');
  654. }
  655. public function getDevicePixelRatioHandlerSuffix ()
  656. {
  657. $iDevicePixelRatio = $this->getDevicePixelRatio ();
  658. if ($iDevicePixelRatio >= 2)
  659. return $this->_sRetinaSuffix;
  660. return '';
  661. }
  662. protected function processHandlerForRetinaDevice ($mixedHandler)
  663. {
  664. return '' . $mixedHandler . $this->getDevicePixelRatioHandlerSuffix ();
  665. }
  666. static protected function _callFuncForObjectsArray ($mixed, $sFunc)
  667. {
  668. if (!is_array($mixed))
  669. $mixed = array($mixed);
  670. foreach ($mixed as $sObject) {
  671. $oTranscoder = self::getObjectInstance($sObject);
  672. if ($oTranscoder)
  673. $oTranscoder->$sFunc();
  674. }
  675. }
  676. /**
  677. * Get filter params
  678. * @param $sName filter name
  679. * @return array of filter params if filter was found, or false if given filter isn't enabled for the transcoder
  680. */
  681. protected function getFilterParams ($sName)
  682. {
  683. $this->initFilters ();
  684. foreach ($this->_aObject['filters'] as $aParams)
  685. if ($sName == $aParams['filter'])
  686. return $aParams['filter_params'];
  687. return false;
  688. }
  689. }
  690. /** @} */