Skip to content

Commit

Permalink
Dramatically speed up posters page by calling a single directory list…
Browse files Browse the repository at this point in the history
…ing instead of a ton of separate `has()` calls.
  • Loading branch information
BusterNeece committed Apr 2, 2024
1 parent 3e3660c commit 140dce2
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions backend/src/Controller/Dashboard/PostersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\Connection;
use Intervention\Image\ImageManager;
use League\Flysystem\StorageAttributes;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UploadedFileInterface;

Expand Down Expand Up @@ -69,18 +70,31 @@ public function listAction(

$nowDt = new \DateTimeImmutable('now', new \DateTimeZone('UTC'));

// Iterate files in the poster uploads directory once to avoid a bunch of repeat calls.
$fs = Media::getFilesystem();

$posterFiles = [];

/** @var StorageAttributes $posterFile */
foreach ($fs->listContents('img/posters/') as $posterFile) {
if (!$posterFile->isFile()) {
continue;
}

$posterFiles[$posterFile->path()] = $posterFile->path();
}

foreach ($qb->fetchAllAssociative() as $poster) {
// Get poster URL
$tryMediaUrls = [
'/img/posters/' . urlencode($poster['file']) . '_thumb.jpg',
'/img/posters/' . urlencode($poster['file']) . '_150x200.jpeg',
'img/posters/' . urlencode($poster['file']) . '_thumb.jpg',
'img/posters/' . urlencode($poster['file']) . '_150x200.jpeg',
];

$mediaUrl = '/static/img/no_poster_thumb.jpg';

foreach ($tryMediaUrls as $tryMediaUrl) {
if ($fs->has($tryMediaUrl)) {
if (isset($posterFiles[$tryMediaUrl])) {
$mediaUrl = mediaUrl($tryMediaUrl);
break;
}
Expand Down

0 comments on commit 140dce2

Please sign in to comment.