- Affected version
- 2.2.4
\XFMG\Attachment\Media::onNewAttachment
PHP:
public function onNewAttachment(Attachment $attachment, \XF\FileWrapper $file)
{
/** @var \XFMG\Service\Media\TempCreator $tempCreator */
$tempCreator = \XF::app()->service('XFMG:Media\TempCreator');
$tempCreator->setAttachment($attachment);
if ($file->getExif())
{
$tempCreator->setExif($file->getExif());
}
$tempCreator->save();
}
When this is called, the attachment upload has been completed, the file has been copied to
data
and the original uploaded file does still exist.\XFMG\Service\Media\TempCreator::_save
PHP:
protected function _save()
{
$mediaTemp = $this->mediaTemp;
$mediaTemp->save();
$abstractedThumbnailPath = $mediaTemp->getAbstractedTempThumbnailPath();
$abstractedPosterPath = $mediaTemp->getAbstractedTempPosterPath();
$thumbnailDate = 0;
$posterDate = 0;
/** @var \XFMG\Service\Media\ThumbnailGenerator $thumbnailGenerator */
$thumbnailGenerator = $this->service('XFMG:Media\ThumbnailGenerator');
$updates = [];
if ($this->attachment)
{
$attachment = $this->attachment;
$updates += [
'title' => $attachment->Data->filename,
'attachment_id' => $attachment->attachment_id
];
if ($thumbnailGenerator->createTempThumbnailFromAttachment($attachment, $abstractedThumbnailPath, $mediaTemp->media_type))
{
$thumbnailDate = time();
}
if ($thumbnailGenerator->createTempPosterFromAttachment($attachment, $abstractedPosterPath, $mediaTemp->media_type))
{
$posterDate = time();
}
This code generates poster and tumbnail by calling methods
XFMG\Service\Media\ThumbnailGenerator::createTempThumbnailFromAttachment
and XFMG\Service\Media\ThumbnailGenerator::createTempPosterFromAttachment
respectively.Both methods then call
XFMG\Service\Media\ThumbnailGenerator::getTempFrameFromFfMpeg
PHP:
public function getTempFrameFromFfMpeg($abstractedSourcePath, $mediaType)
{
if (isset($this->cachedFrames[$abstractedSourcePath]))
{
return $this->cachedFrames[$abstractedSourcePath];
}
$sourceFile = \XF\Util\File::copyAbstractedPathToTempFile($abstractedSourcePath);
This does copy (copy #1) the uploaded video teo a temporary file.
Further down in
\XFMG\Service\Media\TempCreator::_save
another copy (copy #2) gets created
PHP:
if ($mediaTemp->media_type == 'video')
{
$abstractedPath = $attachment->Data->getAbstractedDataPath();
$tempPath = \XF\Util\File::copyAbstractedPathToTempFile($abstractedPath);
So in total 2 additional copies of the uploaded video are taken, even though a temporary file with the same content does already exist at [icode[$_FILES['upload']['tmp_name'][/icode].
This does not only take up 3x the filesize of the video for temporary files during upload processing, it also does waste traffic if
data
is on external storage (S3, etc.)This should be changed so the original upload tmp is used for all those operations (generating thumb, poster & getting metadata).