1
0

BxDolTranscoder.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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");
  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");
  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());
  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");
  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");
  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");
  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");
  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");
  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());
  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());
  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());
  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. if (IMAGE_ERROR_SUCCESS == $o->resize($sFile))
  613. return true;
  614. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: applyFilter_Resize failed for file ({$sFile}): " . $o->getError());
  615. return false;
  616. }
  617. protected function isFileReady_Folder ($mixedHandlerOrig, $isCheckOutdated = true)
  618. {
  619. $mixedHandler = $this->processHandlerForRetinaDevice($mixedHandlerOrig);
  620. $iFileId = $this->_oDb->getFileIdByHandler($mixedHandler);
  621. if (!$iFileId)
  622. return false;
  623. $aFile = $this->_oStorage->getFile($iFileId);
  624. if (!$aFile)
  625. return false;
  626. if ($isCheckOutdated) { // warning, $isCheckOutdated is partially supported for Folder source type - file modification is not checked
  627. if ($this->_aObject['ts'] > $aFile['modified'] || !$this->getFilePath_Folder($mixedHandlerOrig)) { // if we changed transcoder object params or original file is deleted
  628. // delete file, so it will be recreated next time
  629. if ($this->_oStorage->deleteFile($aFile['id']))
  630. return false;
  631. }
  632. }
  633. return true;
  634. }
  635. protected function isFileReady_Storage ($mixedHandlerOrig, $isCheckOutdated = true)
  636. {
  637. $mixedHandler = $this->processHandlerForRetinaDevice($mixedHandlerOrig);
  638. $iFileId = $this->_oDb->getFileIdByHandler($mixedHandler);
  639. if (!$iFileId)
  640. return false;
  641. $aFile = $this->_oStorage->getFile($iFileId);
  642. if (!$aFile)
  643. return false;
  644. if ($isCheckOutdated) {
  645. $oStorageOriginal = BxDolStorage::getObjectInstance($this->_aObject['source_params']['object']);
  646. if ($oStorageOriginal) {
  647. $aFileOriginal = $oStorageOriginal->getFile($mixedHandlerOrig);
  648. if (!$aFileOriginal || $aFileOriginal['modified'] > $aFile['modified'] || $this->_aObject['ts'] >= $aFile['modified']) { // if original file was changed OR we changed transcoder object params
  649. // delete file, so it will be recreated next time
  650. if ($this->_oStorage->deleteFile($aFile['id']))
  651. return false;
  652. }
  653. }
  654. }
  655. return true;
  656. }
  657. protected function isPrivate_Folder ($mixedHandler)
  658. {
  659. if ('no' == $this->_aObject['private'])
  660. return false;
  661. return true;
  662. }
  663. protected function isPrivate_Storage ($mixedHandler)
  664. {
  665. switch ($this->_aObject['private']) {
  666. case 'no':
  667. return false;
  668. case 'yes':
  669. return true;
  670. default:
  671. case 'auto':
  672. $oStorageOriginal = BxDolStorage::getObjectInstance($this->_aObject['source_params']['object']);
  673. if (!$oStorageOriginal)
  674. return true; // in case of error - make sure that file is not public accidentally
  675. $aFile = $oStorageOriginal->getFile($mixedHandler);
  676. return $oStorageOriginal->isFilePrivate($aFile['id']);
  677. }
  678. }
  679. protected function getFilePath_Folder ($mixedHandler)
  680. {
  681. $sPath = $this->_aObject['source_params']['path'] . $mixedHandler;
  682. if (!file_exists($sPath))
  683. $sPath = BX_DIRECTORY_PATH_ROOT . $sPath;
  684. if (!file_exists($sPath)) {
  685. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: getFilePath_Folder failed, file ({$sPath}) doesn't exist for handler ({$mixedHandler})");
  686. return false;
  687. }
  688. return $sPath;
  689. }
  690. protected function storeFileLocally_Folder ($mixedHandler)
  691. {
  692. $sPath = $this->getFilePath_Folder($mixedHandler);
  693. if (!$sPath) {
  694. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: storeFileLocally_Folder failed, source file ({$sPath}) doesn't exist for handler ({$mixedHandler})");
  695. return false;
  696. }
  697. $sTmpFile = $this->getTmpFilename ($mixedHandler);
  698. if (!copy($sPath, $sTmpFile)) {
  699. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: storeFileLocally_Folder failed, copy from file ({$sPath}) to file ({$sTmpFile}) failed");
  700. return false;
  701. }
  702. return $sTmpFile;
  703. }
  704. protected function storeFileLocally_Storage ($mixedHandler)
  705. {
  706. $oStorageOriginal = BxDolStorage::getObjectInstance($this->_aObject['source_params']['object']);
  707. if (!$oStorageOriginal) {
  708. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: storeFileLocally_Storage failed, source storage({$this->_aObject['source_params']['object']}) isn't available");
  709. return false;
  710. }
  711. $aFile = $oStorageOriginal->getFile($mixedHandler);
  712. if (!$aFile) {
  713. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: storeFileLocally_Storage failed, file({$mixedHandler}) wasn't found in source storage ({$this->_aObject['source_params']['object']})");
  714. return false;
  715. }
  716. $sUrl = $oStorageOriginal->getFileUrlById($mixedHandler);
  717. if (!$sUrl) {
  718. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: storeFileLocally_Storage failed, file({$mixedHandler}) wasn't found in source storage ({$this->_aObject['source_params']['object']})");
  719. return false;
  720. }
  721. $sFileData = bx_file_get_contents ($sUrl, [], 'get', []);
  722. if (false === $sFileData) {
  723. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: storeFileLocally_Storage failed, getting file from URL({$sUrl}) failed");
  724. return false;
  725. }
  726. $sTmpFile = $this->getTmpFilename ($aFile['file_name']);
  727. if (!file_put_contents($sTmpFile, $sFileData)) {
  728. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: storeFileLocally_Storage failed, file save failed ({$sTmpFile})");
  729. return false;
  730. }
  731. return $sTmpFile;
  732. }
  733. protected function storeFileLocally_Queue ($mixedHandler)
  734. {
  735. $aQueue = $this->_oDb->getFromQueue($mixedHandler);
  736. if (!$aQueue || !$aQueue['file_url_source']) {
  737. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: storeFileLocally_Queue failed, file ({$mixedHandler}) wasn't found in queue");
  738. return false;
  739. }
  740. $sFileData = bx_file_get_contents ($aQueue['file_url_source'], [], 'get', []);
  741. if (false === $sFileData) {
  742. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: storeFileLocally_Queue failed, getting file from URL({$aQueue['file_url_source']}) failed");
  743. return false;
  744. }
  745. $sTmpFile = $this->getTmpFilename ($mixedHandler);
  746. if (!file_put_contents($sTmpFile, $sFileData)) {
  747. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: storeFileLocally_Queue failed, file save failed ({$sTmpFile})");
  748. return false;
  749. }
  750. return $sTmpFile;
  751. }
  752. protected function getOrigFileUrl_Folder ($mixedHandler)
  753. {
  754. $sPath = $this->_aObject['source_params']['path'] . $mixedHandler;
  755. if (!file_exists(BX_DIRECTORY_PATH_ROOT . $sPath)) {
  756. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: getOrigFileUrl_Folder failed, source file ({$sPath}) doesn't exist");
  757. return false;
  758. }
  759. return BX_DOL_URL_ROOT . $sPath;
  760. }
  761. protected function getOrigFileUrl_Storage ($mixedHandler)
  762. {
  763. $oStorageOriginal = BxDolStorage::getObjectInstance($this->_aObject['source_params']['object']);
  764. if (!$oStorageOriginal) {
  765. bx_log('sys_transcoder', "[{$this->_aObject['object']}] ERROR: getOrigFileUrl_Storage failed, source storage ({$this->_aObject['source_params']['object']}) doesn't exist");
  766. return false;
  767. }
  768. return $oStorageOriginal->getFileUrlById($mixedHandler);
  769. }
  770. protected function initFilters ()
  771. {
  772. if (false !== $this->_aObject['filters']) // filters are already initilized
  773. return;
  774. // get transcoder filters
  775. $aFilters = $this->_oDb->getTranscoderFilters();
  776. $this->_aObject['filters'] = array();
  777. foreach ($aFilters as $aFilter) {
  778. $aFilter['filter_params'] = $aFilter['filter_params'] ? unserialize($aFilter['filter_params']) : array();
  779. $this->_aObject['filters'][] = $aFilter;
  780. }
  781. }
  782. protected function clearCacheDB()
  783. {
  784. $oCacheDb = $this->_oDb->getDbCacheObject();
  785. return $oCacheDb->removeAllByPrefix('db_');
  786. }
  787. public function getDevicePixelRatioHandlerSuffix ()
  788. {
  789. $iDevicePixelRatio = $this->getDevicePixelRatio ();
  790. if ($iDevicePixelRatio >= 2)
  791. return $this->_sRetinaSuffix;
  792. return '';
  793. }
  794. protected function processHandlerForRetinaDevice ($mixedHandler)
  795. {
  796. return '' . $mixedHandler . $this->getDevicePixelRatioHandlerSuffix ();
  797. }
  798. static protected function _callFuncForObjectsArray ($mixed, $sFunc)
  799. {
  800. if (!is_array($mixed))
  801. $mixed = array($mixed);
  802. foreach ($mixed as $sObject) {
  803. $oTranscoder = self::getObjectInstance($sObject);
  804. if ($oTranscoder)
  805. $oTranscoder->$sFunc();
  806. }
  807. }
  808. /**
  809. * Get filter params
  810. * @param $sName filter name
  811. * @return array of filter params if filter was found, or false if given filter isn't enabled for the transcoder
  812. */
  813. public function getFilterParams ($sName)
  814. {
  815. $this->initFilters ();
  816. foreach ($this->_aObject['filters'] as $aParams)
  817. if ($sName == $aParams['filter'])
  818. return $aParams['filter_params'];
  819. return false;
  820. }
  821. public function getMaxResizeDimention ($sFilter = 'Resize', $sKey = '')
  822. {
  823. $a = $this->_oDb->getTranscoderFilters($sFilter);
  824. if (!$a)
  825. return 0;
  826. $aFilter = array_pop($a);
  827. $aFilterParams = $aFilter['filter_params'] ? unserialize($aFilter['filter_params']) : array();
  828. if ($sKey)
  829. return isset($aFilterParams[$sKey]) ? $aFilterParams[$sKey] : 0;
  830. return $aFilterParams ? max(isset($aFilterParams['w']) ? $aFilterParams['w'] : 0, isset($aFilterParams['h']) ? $aFilterParams['h'] : 0) : 0;
  831. }
  832. public function isMobile ()
  833. {
  834. return bx_is_mobile();
  835. }
  836. }
  837. /** @} */