Thumbnail / Poster handling seems inefficient

Kirby

Well-known member
Affected version
2.2.4
\XFMG\Attachment\Media::onAssociation
PHP:
if ($tempMedia->thumbnail_date)
{
    $tempThumbPath = $tempMedia->getAbstractedTempThumbnailPath();
    $mediaThumbPath = $container->getAbstractedThumbnailPath();

    $tempFile = \XF\Util\File::copyAbstractedPathToTempFile($tempThumbPath);
    \XF\Util\File::copyFileToAbstractedPath($tempFile, $mediaThumbPath);

    $container->fastUpdate('thumbnail_date', $tempMedia->thumbnail_date);

    \XF\Util\File::deleteFromAbstractedPath($tempThumbPath);
}
if ($tempMedia->poster_date)
{
    $tempPosterPath = $tempMedia->getAbstractedTempPosterPath();
    $mediaPosterPath = $container->getAbstractedPosterPath();

    $tempFile = \XF\Util\File::copyAbstractedPathToTempFile($tempPosterPath);
    \XF\Util\File::copyFileToAbstractedPath($tempFile, $mediaPosterPath);

    $container->fastUpdate('poster_date', $tempMedia->poster_date);

    \XF\Util\File::deleteFromAbstractedPath($tempPosterPath);
}

This code basically moves the files from data://xfmg/temp to data://xfmg/thumbnail and data://xfmg/poster respectively by copying them to a tempfile, copying that tempfile to the new location and deleting the source.

A more efficient approach would be
PHP:
if ($tempMedia->thumbnail_date)
{
    $tempThumbPath = $tempMedia->getAbstractedTempThumbnailPath();
    $mediaThumbPath = $container->getAbstractedThumbnailPath();

    \XF::fs()->move($tempThumbPath, $mediaThumbPath);

    $container->fastUpdate('thumbnail_date', $tempMedia->thumbnail_date);
}
if ($tempMedia->poster_date)
{
    $tempPosterPath = $tempMedia->getAbstractedTempPosterPath();
    $mediaPosterPath = $container->getAbstractedPosterPath();

    \XF::fs()->move($tempPosterPath, $mediaPosterPath);

    $container->fastUpdate('poster_date', $tempMedia->poster_date);
}

This would allow the rename to take place without always having to download & upload the files (might still happen though if a rename can't be performed directly).
 
Top Bottom