/** * This function will regenerate the thumbnail of videos * @param int $id * @param bool $returnThumb * @return bool */ public function _fetchThumbnail($id = 0, $returnThumb = false) { if (!COwnerHelper::isRegisteredUser()) { return; } if (!$id) { return false; } $table = JTable::getInstance('Video', 'CTable'); $table->load($id); $config = CFactory::getConfig(); if ($table->type == 'file') { // We can only recreate the thumbnail for local video file only // it's not possible to process remote video file with ffmpeg if ($table->storage != 'file') { $this->setError(JText::_('COM_COMMUNITY_INVALID_FILE_REQUEST') . ': ' . 'FFmpeg cannot process remote video.'); return false; } $videoLib = new CVideoLibrary(); $videoFullPath = JPATH::clean(JPATH_ROOT . '/' . $table->path); if (!JFile::exists($videoFullPath)) { return false; } // Read duration $videoInfo = $videoLib->getVideoInfo($videoFullPath); if (!$videoInfo) { return false; } else { $videoFrame = CVideosHelper::formatDuration((int) ($videoInfo['duration']['sec'] / 2), 'HH:MM:SS'); // Create thumbnail $oldThumb = $table->thumb; $thumbFolder = CVideoLibrary::getPath($table->creator, 'thumb'); $thumbSize = CVideoLibrary::thumbSize(); $thumbFilename = $videoLib->createVideoThumb($videoFullPath, $thumbFolder, $videoFrame, $thumbSize); } if (!$thumbFilename) { return false; } } else { if (!CRemoteHelper::curlExists()) { $this->setError(JText::_('COM_COMMUNITY_CURL_NOT_EXISTS')); return false; } $videoLib = new CVideoLibrary(); $videoObj = $videoLib->getProvider($table->path); if ($videoObj == false) { $this->setError($videoLib->getError()); return false; } if (!$videoObj->isValid()) { $this->setError($videoObj->getError()); return false; } $remoteThumb = $videoObj->getThumbnail(); $thumbData = CRemoteHelper::getContent($remoteThumb, true); if (empty($thumbData)) { $this->setError(JText::_('COM_COMMUNITY_INVALID_FILE_REQUEST') . ': ' . $remoteThumb); return false; } // split the header and body list($headers, $body) = explode("\r\n\r\n", $thumbData, 2); preg_match('/Content-Type: image\\/(.*)/i', $headers, $matches); if (!empty($matches)) { $thumbPath = CVideoLibrary::getPath($table->creator, 'thumb'); $thumbFileName = CFileHelper::getRandomFilename($thumbPath); $tmpThumbPath = $thumbPath . '/' . $thumbFileName; if (!JFile::write($tmpThumbPath, $body)) { $this->setError(JText::_('COM_COMMUNITY_INVALID_FILE_REQUEST') . ': ' . $thumbFileName); return false; } // We'll remove the old or none working thumbnail after this $oldThumb = $table->thumb; // Get the image type first so we can determine what extensions to use $info = getimagesize($tmpThumbPath); $mime = image_type_to_mime_type($info[2]); $thumbExtension = CImageHelper::getExtension($mime); $thumbFilename = $thumbFileName . $thumbExtension; $thumbPath = $thumbPath . '/' . $thumbFilename; if (!JFile::move($tmpThumbPath, $thumbPath)) { $this->setError(JText::_('WARNFS_ERR02') . ': ' . $thumbFileName); return false; } // Resize the thumbnails //CImageHelper::resizeProportional( $thumbPath , $thumbPath , $mime , CVideoLibrary::thumbSize('width') , CVideoLibrary::thumbSize('height') ); list($width, $height) = explode('x', $config->get('videosThumbSize')); CImageHelper::resizeAspectRatio($thumbPath, $thumbPath, $width, $height); } else { $this->setError(JText::_('COM_COMMUNITY_PHOTOS_IMAGE_NOT_PROVIDED_ERROR')); return false; } } // Update the DB with new thumbnail $thumb = $config->get('videofolder') . '/' . VIDEO_FOLDER_NAME . '/' . $table->creator . '/' . VIDEO_THUMB_FOLDER_NAME . '/' . $thumbFilename; $table->set('thumb', $thumb); $table->store(); // If this video storage is not on local, we move it to remote storage // and remove the old thumb if existed if ($table->storage != 'file') { // && ($table->storage == $storageType)) $config = CFactory::getConfig(); $storageType = $config->getString('videostorage'); $storage = CStorage::getStorage($storageType); $storage->delete($oldThumb); $localThumb = JPATH::clean(JPATH_ROOT . '/' . $table->thumb); $tempThumbname = JPATH::clean(JPATH_ROOT . '/' . md5($table->thumb)); if (JFile::exists($localThumb)) { JFile::copy($localThumb, $tempThumbname); } if (JFile::exists($tempThumbname)) { $storage->put($table->thumb, $tempThumbname); JFile::delete($localThumb); JFile::delete($tempThumbname); } } else { if (JFile::exists(JPATH_ROOT . '/' . $oldThumb)) { JFile::delete(JPATH_ROOT . '/' . $oldThumb); } } if ($returnThumb) { return $table->getThumbnail(); } return true; }
public function upload() { $document = JFactory::getDocument(); $mainframe = JFactory::getApplication(); $my = $this->plugin->get('user'); $creatorType = JRequest::getVar('creatortype', VIDEO_USER_TYPE); $groupid = $creatorType == VIDEO_GROUP_TYPE ? JRequest::getInt('groupid', 0) : 0; $config = CFactory::getConfig(); CFactory::load('helpers', 'videos'); CFactory::load('libraries', 'videos'); $redirect = CVideosHelper::getVideoReturnUrlFromRequest(); // Process according to video creator type if (!empty($groupid)) { CFactory::load('helpers', 'group'); $allowManageVideos = CGroupHelper::allowManageVideo($groupid); $creatorType = VIDEO_GROUP_TYPE; $videoLimit = $config->get('groupvideouploadlimit'); CError::assert($allowManageVideos, '', '!empty', __FILE__, __LINE__); } else { $creatorType = VIDEO_USER_TYPE; $videoLimit = $config->get('videouploadlimit'); } // Check is video upload is permitted CFactory::load('helpers', 'limits'); if (CLimitsHelper::exceededVideoUpload($my->id, $creatorType)) { $message = JText::sprintf('CC VIDEOS CREATION REACH LIMIT', $videoLimit); $this->setError($message); return false; } if (!$config->get('enablevideos')) { $this->setError(JText::_('CC VIDEO DISABLED', 'notice')); return false; } if (!$config->get('enablevideosupload')) { $this->setError(JText::_('CC VIDEO UPLOAD DISABLED', 'notice')); return false; } // Check if the video file is valid $files = JRequest::get('files'); $videoFile = !empty($files['video']) ? $files['video'] : array(); if (empty($files) || empty($videoFile['name']) && $videoFile['size'] < 1) { $this->setError(JText::_('CC VIDEO UPLOAD ERROR', 'error')); return false; } // Check file type. $fileType = $videoFile['type']; // Override from iphone $fileType = 'video/quicktime'; $allowable = CVideosHelper::getValidMIMEType(); if (!in_array($fileType, $allowable)) { $this->setError(JText::sprintf('CC VIDEO FILE TYPE NOT SUPPORTED', $fileType)); return false; } // Check if the video file exceeds file size limit $uploadLimit = $config->get('maxvideouploadsize') * 1024 * 1024; $videoFileSize = sprintf("%u", filesize($videoFile['tmp_name'])); if ($uploadLimit > 0 && $videoFileSize > $uploadLimit) { $this->setError(JText::sprintf('CC VIDEO FILE SIZE EXCEEDED', $uploadLimit)); return false; } // Passed all checking, attempt to save the video file CFactory::load('helpers', 'file'); $folderPath = CVideoLibrary::getPath($my->id, 'original'); $randomFileName = CFileHelper::getRandomFilename($folderPath, $videoFile['name'], ''); $destination = JPATH::clean($folderPath . DS . $randomFileName); if (!CFileHelper::upload($videoFile, $destination)) { $this->setError(JText::_('CC VIDEO UPLOAD FAILED', 'error')); return false; } $config = CFactory::getConfig(); $videofolder = $config->get('videofolder'); CFactory::load('models', 'videos'); $video = JTable::getInstance('Video', 'CTable'); $video->set('path', $videofolder . '/originalvideos/' . $my->id . '/' . $randomFileName); $video->set('title', JRequest::getVar('title')); $video->set('description', JRequest::getVar('description')); $video->set('category_id', JRequest::getInt('category', 0, 'post')); $video->set('permissions', JRequest::getInt('privacy', 0, 'post')); $video->set('creator', $my->id); $video->set('creator_type', $creatorType); $video->set('groupid', $groupid); $video->set('filesize', $videoFileSize); if (!$video->store()) { $this->setError(JText::_('CC VIDEO SAVE ERROR', 'error')); return false; } // Trigger for onVideoCreate $this->_triggerEvent('onVideoCreate', $video); // Video saved, redirect return $video; }