public function rebuildThumbnail(array $media, array $imageInfo, $deleteExisting = true, $skipThumbnail = 0) { $originalThumbFile = $this->getMediaThumbnailFilePath($media); $media['file_hash'] = $imageInfo['file_hash']; $options = XenForo_Application::getOptions(); $thumbFile = $this->getMediaThumbnailFilePath($media); $thumbImage = false; if ($skipThumbnail) { XenForo_Helper_File::safeRename($originalThumbFile, $thumbFile); } else { if ($media['media_type'] == 'image_upload') { $originalFile = $this->getOriginalDataFilePath($media, true); $thumbImage = new XenGallery_Helper_Image($originalFile); } else { if ($media['media_type'] == 'video_upload') { $originalFile = $this->getAttachmentDataFilePath($media); $tempThumbFile = false; if ($options->get('xengalleryVideoTranscoding', 'thumbnail')) { try { $video = new XenGallery_Helper_Video($originalFile); $tempThumbFile = $video->getKeyFrame(); } catch (XenForo_Exception $e) { } } if (!$tempThumbFile) { $tempThumbFile = tempnam(XenForo_Helper_File::getTempDir(), 'xfmg'); if ($tempThumbFile) { @copy($options->xengalleryDefaultNoThumb, $tempThumbFile); } } $thumbImage = new XenGallery_Helper_Image($tempThumbFile); } } if ($thumbImage) { $thumbImage->resize($options->xengalleryThumbnailDimension['width'], $options->xengalleryThumbnailDimension['height'], 'crop'); $thumbnailed = $thumbImage->saveToPath($thumbFile); if ($thumbnailed && $deleteExisting) { @unlink($originalThumbFile); } } } $mediaWriter = XenForo_DataWriter::create('XenGallery_DataWriter_Media'); $mediaWriter->setExistingData($media); $mediaWriter->bulkSet(array('last_edit_date' => XenForo_Application::$time, 'thumbnail_date' => $media['thumbnail_date'])); $mediaWriter->save(); if ($media['album_id']) { $albumWriter = XenForo_DataWriter::create('XenGallery_DataWriter_Album'); $albumWriter->setExistingData($media['album_id']); if (!$albumWriter->get('manual_media_cache') && !$albumWriter->get('album_thumbnail_date')) { $mediaItems = $this->getMediaForAlbumCache($media['album_id']); $albumWriter->bulkSet(array('last_update_date' => XenForo_Application::$time, 'media_cache' => serialize($mediaItems))); $albumWriter->save(); } } }
public function importMedia($oldId, $tempFile, $contentType = '', array $xengalleryMedia = array(), array $xfAttachment = array(), array $xfAttachmentData = array()) { $db = $this->_getDb(); XenForo_Db::beginTransaction($db); $attachmentId = 0; if ($xfAttachment) { /** @var $attachmentDw XenForo_DataWriter_Attachment */ $attachmentDw = XenForo_DataWriter::create('XenForo_DataWriter_Attachment'); $attachmentDw->setImportMode(true); $attachmentDw->bulkSet($xfAttachment); $attachmentDw->save(); $attachmentId = $attachmentDw->get('attachment_id'); } $newId = false; if ($xengalleryMedia) { /** @var $mediaDw XenGallery_DataWriter_Media */ $mediaDw = XenForo_DataWriter::create('XenGallery_DataWriter_Media'); $mediaDw->setImportMode(true); $mediaDw->set('imported', XenForo_Application::$time); if ($this->_retainKeys) { $mediaDw->set('media_id', $oldId); } $xengalleryMedia['attachment_id'] = $attachmentId; $mediaDw->bulkSet($xengalleryMedia); if ($mediaDw->save()) { $newId = $mediaDw->get('media_id'); $this->_getImportModel()->logImportData('xengallery_media', $oldId, $newId); } $media = $mediaDw->getMergedData(); if ($media['likes'] && $media['like_users'] && $contentType) { $this->_convertLikesToNewContentType($oldId, $newId, $contentType, 'xengallery_media'); } } if ($xfAttachmentData) { $fileIsVideo = false; $db->update('xf_attachment', array('content_id' => $media['media_id']), 'attachment_id = ' . $db->quote($attachmentId)); $options = XenForo_Application::getOptions(); $upload = new XenForo_Upload($xfAttachmentData['filename'], $tempFile); if ($upload->isImage()) { $image = new XenGallery_Helper_Image($tempFile); $image->importMode = true; $dimensions = array('width' => $image->getWidth(), 'height' => $image->getHeight()); $tempThumbFile = tempnam(XenForo_Helper_File::getTempDir(), 'xfmg'); if ($tempThumbFile && $image) { $resized = $image->resize($dimensions['thumbnail_width'] = $options->xengalleryThumbnailDimension['width'], $dimensions['thumbnail_height'] = $options->xengalleryThumbnailDimension['height'], 'crop'); if (!$resized) { return false; } $image->saveToPath($tempThumbFile); unset($image); } else { return false; } } else { $dimensions = array(); $fileIsVideo = true; $tempThumbFile = false; if ($options->get('xengalleryVideoTranscoding', 'thumbnail')) { try { $video = new XenGallery_Helper_Video($upload->getTempFile()); $tempThumbFile = $video->getKeyFrame(); list($width, $height) = $video->getVideoDimensions(); $dimensions['width'] = $width; $dimensions['height'] = $height; } catch (XenForo_Exception $e) { } } if (!$tempThumbFile) { $tempThumbFile = tempnam(XenForo_Helper_File::getTempDir(), 'xfmg'); if ($tempThumbFile) { @copy($options->xengalleryDefaultNoThumb, $tempThumbFile); } } $image = new XenGallery_Helper_Image($tempThumbFile); if ($image) { $image->resize($dimensions['thumbnail_width'] = $options->xengalleryThumbnailDimension['width'], $dimensions['thumbnail_height'] = $options->xengalleryThumbnailDimension['height'], 'crop'); $image->saveToPath($tempThumbFile); unset($image); } } $mediaModel = $this->getModelFromCache('XenGallery_Model_Media'); try { $dataDw = XenForo_DataWriter::create('XenForo_DataWriter_AttachmentData'); $filename = $upload->getFileName(); if ($fileIsVideo) { $filename = strtr($filename, strtolower(substr(strrchr($filename, '.'), 1)), 'mp4'); $dataDw->set('file_path', $mediaModel->getVideoFilePath()); } $dataDw->set('filename', $filename); $dataDw->bulkSet($dimensions); $dataDw->bulkSet($xfAttachmentData); $dataDw->setExtraData(XenForo_DataWriter_AttachmentData::DATA_TEMP_FILE, $tempFile); if ($tempThumbFile) { $dataDw->setExtraData(XenForo_DataWriter_AttachmentData::DATA_TEMP_THUMB_FILE, $tempThumbFile); } $dataDw->setExtraData(XenGallery_DataWriter_AttachmentData::DATA_XMG_FILE_IS_VIDEO, $fileIsVideo); $dataDw->setExtraData(XenGallery_DataWriter_AttachmentData::DATA_XMG_DATA, true); $dataDw->save(); $attachmentData = $dataDw->getMergedData(); $db->update('xf_attachment', array('data_id' => $attachmentData['data_id']), 'attachment_id = ' . $db->quote($attachmentId)); } catch (Exception $e) { if ($tempThumbFile) { @unlink($tempThumbFile); } throw $e; } if ($tempThumbFile) { @unlink($tempThumbFile); } } if ($newId) { XenForo_Db::commit($db); } else { XenForo_Db::rollback($db); } return $newId; }
/** * Inserts uploaded attachment data. * * @param XenForo_Upload $file Uploaded attachment info. Assumed to be valid * @param integer $userId User ID uploading * @param array $exif Exif data to cache * * @return integer Attachment data ID */ public function insertUploadedAttachmentData(XenForo_Upload $file, $userId, array $exif = array()) { $dimensions = array(); $fileIsVideo = false; $tempThumbFile = false; $options = XenForo_Application::getOptions(); if ($file->isImage()) { $dimensions = array('width' => $file->getImageInfoField('width'), 'height' => $file->getImageInfoField('height')); if (XenForo_Image_Abstract::canResize($dimensions['width'], $dimensions['height'])) { $imageFile = $file->getTempFile(); } else { $imageFile = $options->xengalleryDefaultNoThumb; } $tempThumbFile = tempnam(XenForo_Helper_File::getTempDir(), 'xfmg'); if ($tempThumbFile) { @copy($imageFile, $tempThumbFile); } } else { $fileIsVideo = true; if ($options->get('xengalleryVideoTranscoding', 'thumbnail')) { try { $video = new XenGallery_Helper_Video($file->getTempFile()); $tempThumbFile = $video->getKeyFrame(); list($width, $height) = $video->getVideoDimensions(); $dimensions['width'] = $width; $dimensions['height'] = $height; } catch (XenForo_Exception $e) { } } if (!$tempThumbFile) { $tempThumbFile = tempnam(XenForo_Helper_File::getTempDir(), 'xfmg'); if ($tempThumbFile) { @copy($options->xengalleryDefaultNoThumb, $tempThumbFile); } } } if ($tempThumbFile) { $image = new XenGallery_Helper_Image($tempThumbFile); if ($image) { $image->resize($dimensions['thumbnail_width'] = $options->xengalleryThumbnailDimension['width'], $dimensions['thumbnail_height'] = $options->xengalleryThumbnailDimension['height'], 'crop'); $image->saveToPath($tempThumbFile); unset($image); } } $mediaModel = $this->_getMediaModel(); try { $dataDw = XenForo_DataWriter::create('XenForo_DataWriter_AttachmentData'); $filename = $file->getFileName(); $dataDw->set('user_id', $userId); if ($fileIsVideo) { $filename = strtr($filename, strtolower(substr(strrchr($filename, '.'), 1)), 'mp4'); $dataDw->set('file_path', $mediaModel->getVideoFilePath()); } $dataDw->set('filename', $filename); $dataDw->bulkSet($dimensions); $dataDw->setExtraData(XenForo_DataWriter_AttachmentData::DATA_TEMP_FILE, $file->getTempFile()); if ($tempThumbFile) { $dataDw->setExtraData(XenForo_DataWriter_AttachmentData::DATA_TEMP_THUMB_FILE, $tempThumbFile); } $dataDw->setExtraData(XenGallery_DataWriter_AttachmentData::DATA_XMG_FILE_IS_VIDEO, $fileIsVideo); $dataDw->setExtraData(XenGallery_DataWriter_AttachmentData::DATA_XMG_DATA, true); $dataDw->save(); } catch (Exception $e) { if ($tempThumbFile) { @unlink($tempThumbFile); } throw $e; } if ($tempThumbFile) { @unlink($tempThumbFile); } $exif = $this->_getMediaModel()->sanitizeExifData($exif); $db = $this->_getDb(); $db->query(' INSERT IGNORE INTO xengallery_exif_cache (data_id, media_exif_data_cache_full, cache_date) VALUES (?, ?, ?) ', array($dataDw->get('data_id'), @json_encode($exif), XenForo_Application::$time)); return $dataDw->get('data_id'); }
public function execute(array $deferred, array $data, $targetRunTime, &$status) { $data = array_merge(array('position' => 0, 'batch' => 10), $data); $data['batch'] = max(1, $data['batch']); /* @var $mediaModel XenGallery_Model_Media */ $mediaModel = XenForo_Model::create('XenGallery_Model_Media'); $mediaIds = $mediaModel->getMediaIdsInRange($data['position'], $data['batch'], 'all'); if (sizeof($mediaIds) == 0) { return true; } $options = XenForo_Application::getOptions(); $fetchOptions = array('join' => XenGallery_Model_Media::FETCH_ATTACHMENT); $media = $mediaModel->getMediaByIds($mediaIds, $fetchOptions); $media = $mediaModel->prepareMediaItems($media); foreach ($media as $item) { $data['position'] = $item['media_id']; if ($item['thumbnail_date']) { continue; } $thumbnailPath = $mediaModel->getMediaThumbnailFilePath($item); $dataPath = $mediaModel->getOriginalDataFilePath($item, true); if ($item['media_type'] == 'image_upload') { XenForo_Helper_File::createDirectory(dirname($thumbnailPath), true); if (!file_exists($dataPath) || !is_readable($dataPath)) { continue; } $image = new XenGallery_Helper_Image($dataPath); if ($image) { $image->resize($dimensions['thumbnail_width'] = $options->xengalleryThumbnailDimension['width'], $dimensions['thumbnail_height'] = $options->xengalleryThumbnailDimension['height'], 'crop'); $image->saveToPath($thumbnailPath); unset($image); $attachmentDataWriter = XenForo_DataWriter::create('XenForo_DataWriter_AttachmentData'); $attachmentDataWriter->setExistingData($item['data_id']); $attachmentDataWriter->bulkSet($dimensions); $attachmentDataWriter->save(); } } else { if ($item['media_type'] == 'video_upload') { XenForo_Helper_File::createDirectory(dirname($thumbnailPath), true); if (!file_exists($dataPath) || !is_readable($dataPath)) { continue; } $tempThumbFile = false; if ($options->get('xengalleryVideoTranscoding', 'thumbnail')) { try { $video = new XenGallery_Helper_Video($dataPath); $tempThumbFile = $video->getKeyFrame(); } catch (XenForo_Exception $e) { } } if (!$tempThumbFile) { $tempThumbFile = tempnam(XenForo_Helper_File::getTempDir(), 'xfmg'); if ($tempThumbFile) { @copy($options->xengalleryDefaultNoThumb, $tempThumbFile); } } $image = new XenGallery_Helper_Image($tempThumbFile); if ($image) { $image->resize($dimensions['thumbnail_width'] = $options->xengalleryThumbnailDimension['width'], $dimensions['thumbnail_height'] = $options->xengalleryThumbnailDimension['height'], 'crop'); $image->saveToPath($thumbnailPath); unset($image); $attachmentDataWriter = XenForo_DataWriter::create('XenForo_DataWriter_AttachmentData'); $attachmentDataWriter->setExistingData($item['data_id']); $attachmentDataWriter->bulkSet($dimensions); $attachmentDataWriter->save(); } } else { if ($item['media_type'] == 'video_embed') { preg_match('/\\[media=(.*?)\\](.*?)\\[\\/media\\]/is', $item['media_tag'], $parts); $mediaModel->getVideoThumbnailUrlFromParts($parts, true); } } } /** @var $mediaWriter XenGallery_DataWriter_Media */ $mediaWriter = XenForo_DataWriter::create('XenGallery_DataWriter_Media'); $mediaWriter->setImportMode(true); $mediaWriter->setExistingData($item['media_id']); $mediaWriter->set('last_edit_date', XenForo_Application::$time); $mediaWriter->save(); } $actionPhrase = new XenForo_Phrase('rebuilding'); $typePhrase = new XenForo_Phrase('xengallery_rebuild_thumbnails'); $status = sprintf('%s... %s (%s)', $actionPhrase, $typePhrase, XenForo_Locale::numberFormat($data['position'])); return $data; }