示例#1
0
    /**
     * 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');
    }
示例#2
0
 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;
 }