/** * Given an array of attachment data or a file path string, ascertains whether the * file will need to be transcoded. Returns true if transcoding is required. False otherwise. * * @param string|array $data * * @return bool */ public function requiresTranscoding($data) { if (!is_array($data)) { $filePath = $data; } else { $filePath = $this->getAttachmentDataFilePath($data); } $videoInfo = new XenGallery_VideoInfo_Preparer($filePath); $result = $videoInfo->getInfo(); if (!$result->isValid() || $result->requiresTranscoding()) { return true; } return false; }
public function finalizeTranscode(array $queueRecord, $outputFile) { $db = XenForo_Application::getDb(); XenForo_Db::beginTransaction($db); $db->delete('xengallery_transcode_queue', 'transcode_queue_id = ' . $db->quote($queueRecord['transcode_queue_id'])); $queueData = $queueRecord['queue_data']; $media = $queueData['media']; /** @var XenGallery_Model_Transcode $transcodeModel */ $transcodeModel = XenForo_Model::create('XenGallery_Model_Transcode'); if (!file_exists($outputFile)) { // transcode failure $params = array('username' => $media['username'], 'title' => $media['media_title']); $this->_transcodeException($media, 'xengallery_video_by_x_named_y_failed_transcoding', $params); } $videoInfo = new XenGallery_VideoInfo_Preparer($outputFile); $result = $videoInfo->getInfo(); /** @var XenForo_Model_Attachment $attachmentModel */ $attachmentModel = XenForo_Model::create('XenForo_Model_Attachment'); $attachment = $attachmentModel->getAttachmentById($queueData['attachmentId']); if (!$result->isValid() || $result->requiresTranscoding()) { $params = array('username' => $media['username'], 'title' => $media['media_title']); $this->_transcodeException($media, 'xengallery_video_by_x_named_y_failed_transcoding', $params); } clearstatcache(); $fields = array('file_hash' => md5_file($outputFile), 'file_size' => filesize($outputFile)); try { $dataDw = XenForo_DataWriter::create('XenForo_DataWriter_AttachmentData'); $dataDw->setExistingData($attachment['data_id']); $dataDw->bulkSet($fields); $dataDw->save(); } catch (XenForo_Exception $e) { $params = array('username' => $media['username'], 'title' => $media['media_title']); $this->_transcodeException($media, 'xengallery_video_by_x_named_y_failed_transcoding', $params); } /** @var XenGallery_Model_Media $mediaModel */ $mediaModel = XenForo_Model::create('XenGallery_Model_Media'); $data = $dataDw->getMergedData(); $filePath = $attachmentModel->getAttachmentDataFilePath($data); $originalThumbPath = $mediaModel->getMediaThumbnailFilePath($attachment); $thumbPath = $mediaModel->getMediaThumbnailFilePath($data); XenForo_Helper_File::safeRename($originalThumbPath, $thumbPath); XenForo_Helper_File::safeRename($outputFile, $filePath); @unlink($queueData['filename']); $mediaDw = XenForo_DataWriter::create('XenGallery_DataWriter_Media', XenForo_DataWriter::ERROR_SILENT); $tagger = null; if (isset($queueData['tags']) && isset($queueData['tagger_permissions'])) { $tagModel = XenForo_Model::create('XenForo_Model_Tag'); $tagger = $tagModel->getTagger('xengallery_media')->setPermissionsFromContext($queueData)->setTags($tagModel->splitTags($queueData['tags'])); } if (!empty($queueData['customFields'])) { $mediaDw->setCustomFields($queueData['customFields'], $queueData['customFieldsShown']); } $mediaDw->bulkSet($media); if ($mediaDw->save()) { $mediaModel->markMediaViewed($mediaDw->getMergedData(), $media); if ($tagger) { $tagger->setContent($mediaDw->get('media_id'), true)->save(); } $attachmentData = array('content_type' => 'xengallery_media', 'content_id' => $mediaDw->get('media_id'), 'temp_hash' => '', 'unassociated' => 0); $db->update('xf_attachment', $attachmentData, "attachment_id = {$attachment['attachment_id']}"); $mediaDw->updateUserMediaQuota(); $transcodeModel->sendTranscodeAlert($mediaDw->getMergedData(), true); } else { $params = array('username' => $media['username'], 'title' => $media['media_title']); $this->_transcodeException($media, 'xengallery_media_uploaded_by_x_named_y_failed_creation', $params); } XenForo_Db::commit($db); $this->_requeueDeferred(); }