Compare commits

...
4 Commits
Author SHA1 Message Date
Lewis Dale 519611d06b Fix it again 2023-08-27 22:12:28 +01:00
Lewis Dale a0580ad085 Enforce uniqueness 2023-08-27 22:03:56 +01:00
Lewis Dale e2cf30a307 Actually fix the query and caching 2023-08-27 22:01:43 +01:00
Lewis Dale a9e8b61e0b More logging for broken query 2023-08-27 21:47:16 +01:00
4 changed files with 20 additions and 11 deletions

No files matched your search

+1 -1
View File
@@ -25,7 +25,7 @@ try {
header("Cache-Control: max-age=86400, public");
echo $image;
} catch (\Exception $e) {
$logger->error($e->getMessage());
$logger->error($e->getMessage(), ['exception' => $e, 'path' => $imgPath, 'params' => $params]);
http_response_code(500);
echo "Internal Server Error";
}
+1
View File
@@ -48,6 +48,7 @@ class Resizer
private function createVariant(Image $original, ResizeParams $params): string
{
$quality = $params->quality ?: -1;
$this->logger->info("Resizing image", ['params'=>$params]);
$original->toWidth($params->width ?? intval($_ENV["DEFAULT_WIDTH"]));
+17 -9
View File
@@ -24,24 +24,32 @@ class SqliteImageDb implements ImageDb {
quality INTEGER NULLABLE,
content TEXT NOT NULL
)');
// Create a unique index on the path and params
$this->db->exec('CREATE UNIQUE INDEX IF NOT EXISTS images_path_width_height_quality ON images (path, width, height, quality)');
}
public function findImage(string $path, ResizeParams $params): Image | null {
$query = "SELECT * FROM images WHERE path = :path";
$query .= $params->width ? " AND width = :width" : " AND width IS NULL";
$query .= $params->height ? " AND height = :height" : " AND height IS NULL";
$query .= $params->quality ? " AND quality = :quality" : " AND quality IS NULL";
$query = 'SELECT * FROM images WHERE path = :path';
$query .= $params->width ? ' AND width = :width' : ' AND width IS NULL';
$query .= $params->height ? ' AND height = :height' : ' AND height IS NULL';
$query .= $params->quality ? ' AND quality = :quality' : ' AND quality IS NULL';
$stmt = $this->db->prepare($query);
$stmt->execute([
$queryParams = [
':path' => $path,
':width' => $params->width,
':height' => $params->height,
':quality' => $params->quality
]);
];
if ($params->width !== null) $queryParams[':width'] = $params->width;
if ($params->height !== null) $queryParams[':height'] = $params->height;
if ($params->quality !== null) $queryParams[':quality'] = $params->quality;
$stmt->execute($queryParams);
$this->logger->info("Querying images", ['query' => $stmt->queryString]);
$stmt->execute();
$result = $stmt->fetchObject();
return $result ? Image::fromString(base64_decode($result->content)) : null;
+1 -1
View File
@@ -12,7 +12,7 @@ class ResizeParams
public function __construct(array $params)
{
$this->width = intval($params['width']) ?: intval($_ENV["DEFAULT_WIDTH"]);
$this->width = isset($params['width']) ? intval($params['width']) : intval($_ENV["DEFAULT_WIDTH"]);
$this->height = intval($params['height']) ?: null;
$this->quality = isset($params['quality']) ? intval($params['quality']) : null;
}