Less home code more F3
This commit is contained in:
@@ -50,6 +50,25 @@ class Media extends DB\SQL\Mapper
|
||||
);
|
||||
}
|
||||
|
||||
public function findByIds(array $ids): array
|
||||
{
|
||||
$ids = array_filter(array_unique(array_map('intval', $ids)));
|
||||
if ($ids === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$placeholders = implode(',', array_fill(0, count($ids), '?'));
|
||||
$results = $this->find(["id IN ($placeholders)", ...array_values($ids)]);
|
||||
|
||||
$map = [];
|
||||
foreach ($results ?: [] as $m) {
|
||||
$row = $this->decorate($m->cast());
|
||||
$map[$row['id']] = $row;
|
||||
}
|
||||
|
||||
return $map;
|
||||
}
|
||||
|
||||
public function findById(int $id): ?array
|
||||
{
|
||||
if ($id <= 0) {
|
||||
@@ -66,18 +85,19 @@ class Media extends DB\SQL\Mapper
|
||||
return $this->dry() ? null : $this->decorate($this->cast());
|
||||
}
|
||||
|
||||
// Reçoit le chemin absolu déposé par Web::receive() et le nom d'origine
|
||||
// pour dériver un texte alternatif lisible.
|
||||
// Traite le fichier temporaire déposé par Web::receive() et publie l'image.
|
||||
public function upload(string $srcPath, string $originalName = ''): int
|
||||
{
|
||||
$target = null;
|
||||
$image = null;
|
||||
$committed = false; // Contrôle le nettoyage de $target dans finally.
|
||||
|
||||
try {
|
||||
$meta = self::inspectUpload($srcPath);
|
||||
$image = self::openImageResource($srcPath, $meta['mime']);
|
||||
|
||||
[$format, $extension] = self::targetFormat($meta['mime']);
|
||||
// Nom aléatoire : empêche le path traversal et la devinabilité des URLs.
|
||||
$fileName = bin2hex(random_bytes(16)) . '.' . $extension;
|
||||
$target = app_public_media_dir() . '/' . $fileName;
|
||||
|
||||
@@ -93,19 +113,15 @@ class Media extends DB\SQL\Mapper
|
||||
$this->created_at = app_now();
|
||||
$this->save();
|
||||
$this->db->commit();
|
||||
$committed = true;
|
||||
} catch (Throwable $e) {
|
||||
$this->db->rollback();
|
||||
if ($target !== null && is_file($target)) {
|
||||
@unlink($target);
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return (int) $this->get('id');
|
||||
} catch (RuntimeException $e) {
|
||||
throw $e;
|
||||
} catch (Throwable) {
|
||||
throw new RuntimeException('Impossible d\'enregistrer cette image.');
|
||||
} catch (Throwable $e) {
|
||||
throw $e instanceof RuntimeException ? $e : new RuntimeException('Impossible d\'enregistrer cette image.');
|
||||
} finally {
|
||||
if ($image instanceof GdImage) {
|
||||
imagedestroy($image);
|
||||
@@ -113,6 +129,9 @@ class Media extends DB\SQL\Mapper
|
||||
if (is_file($srcPath)) {
|
||||
@unlink($srcPath);
|
||||
}
|
||||
if (!$committed && $target !== null && is_file($target)) {
|
||||
@unlink($target);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,16 +148,12 @@ class Media extends DB\SQL\Mapper
|
||||
|
||||
public function delete(int $id): void
|
||||
{
|
||||
$item = $this->findById($id);
|
||||
if ($item === null) {
|
||||
$this->load(['id = ?', $id]);
|
||||
if ($this->dry()) {
|
||||
throw new RuntimeException('Image introuvable.');
|
||||
}
|
||||
|
||||
if ($this->isUsed($item)) {
|
||||
throw new RuntimeException('Cette image est encore utilisée par un article.');
|
||||
}
|
||||
|
||||
$path = app_public_media_dir() . '/' . $item['file_name'];
|
||||
$path = app_public_media_dir() . '/' . $this->file_name;
|
||||
|
||||
$this->db->begin();
|
||||
try {
|
||||
@@ -203,6 +218,8 @@ class Media extends DB\SQL\Mapper
|
||||
return $image;
|
||||
}
|
||||
|
||||
// PNG/WebP → PNG pour préserver la transparence de manière fiable.
|
||||
// JPG reste en JPG (pas de canal alpha).
|
||||
private static function targetFormat(string $mime): array
|
||||
{
|
||||
return match ($mime) {
|
||||
@@ -239,15 +256,6 @@ class Media extends DB\SQL\Mapper
|
||||
return mb_strtoupper(mb_substr($name, 0, 1)) . mb_strtolower(mb_substr($name, 1));
|
||||
}
|
||||
|
||||
// Une seule requête SQL pour les deux cas d'utilisation (couverture et body).
|
||||
private function isUsed(array $item): bool
|
||||
{
|
||||
return $this->db->exec(
|
||||
'SELECT 1 FROM posts WHERE cover_media_id = ? OR body_markdown LIKE ? LIMIT 1',
|
||||
[$item['id'], '%media:' . $item['file_name'] . '%']
|
||||
) !== [];
|
||||
}
|
||||
|
||||
private function decorate(array $row): array
|
||||
{
|
||||
$alt = (string) $row['alt'];
|
||||
|
||||
Reference in New Issue
Block a user