Add remove-image button and actually delete orphaned upload files
Phase 1 of the dashboard roadmap. ImageUploadField now has a clear button, and every controller with an image/cover_image column deletes the old file from hifi/api/uploads/ when it's replaced or the owning record is deleted (including cascade cleanup for gallery brand/project deletes) - previously no controller ever unlinked a physical file. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
76bba33746
commit
63dcd23977
11 changed files with 181 additions and 20 deletions
|
|
@ -27,7 +27,20 @@ export default function ImageUploadField({ value, onChange, label = 'Bild' }) {
|
||||||
<div>
|
<div>
|
||||||
<label className="mb-1 block text-sm font-medium text-neutral-700 dark:text-neutral-300">{label}</label>
|
<label className="mb-1 block text-sm font-medium text-neutral-700 dark:text-neutral-300">{label}</label>
|
||||||
<div className="flex items-center gap-3">
|
<div className="flex items-center gap-3">
|
||||||
{value && <img src={value} alt="" className="h-12 w-12 rounded-md object-cover" />}
|
{value && (
|
||||||
|
<div className="relative">
|
||||||
|
<img src={value} alt="" className="h-12 w-12 rounded-md object-cover" />
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => onChange(null)}
|
||||||
|
title="Bild entfernen"
|
||||||
|
aria-label="Bild entfernen"
|
||||||
|
className="absolute -right-1.5 -top-1.5 flex h-5 w-5 items-center justify-center rounded-full bg-red-600 text-xs font-bold leading-none text-white shadow hover:bg-red-700"
|
||||||
|
>
|
||||||
|
×
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
<input type="file" accept="image/png,image/jpeg,image/webp" onChange={handleFile} className="text-sm" />
|
<input type="file" accept="image/png,image/jpeg,image/webp" onChange={handleFile} className="text-sm" />
|
||||||
{uploading && <span className="text-sm text-neutral-500">Lädt hoch…</span>}
|
{uploading && <span className="text-sm text-neutral-500">Lädt hoch…</span>}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ namespace App\Controllers;
|
||||||
use App\Config\Database;
|
use App\Config\Database;
|
||||||
use App\Support\Http;
|
use App\Support\Http;
|
||||||
use App\Support\Slug;
|
use App\Support\Slug;
|
||||||
|
use App\Support\Uploads;
|
||||||
|
|
||||||
class GalleryBrandController
|
class GalleryBrandController
|
||||||
{
|
{
|
||||||
|
|
@ -48,24 +49,59 @@ class GalleryBrandController
|
||||||
Http::error('Name erforderlich', 422);
|
Http::error('Name erforderlich', 422);
|
||||||
}
|
}
|
||||||
|
|
||||||
$stmt = Database::connection()->prepare(
|
$newCoverPath = trim($body['cover_image_path'] ?? '') ?: null;
|
||||||
|
$db = Database::connection();
|
||||||
|
$oldStmt = $db->prepare('SELECT cover_image_path FROM gallery_brands WHERE id = ?');
|
||||||
|
$oldStmt->execute([$id]);
|
||||||
|
$oldCoverPath = $oldStmt->fetchColumn();
|
||||||
|
|
||||||
|
$stmt = $db->prepare(
|
||||||
'UPDATE gallery_brands SET name = ?, slug = ?, cover_image_path = ?, sort_order = ? WHERE id = ?'
|
'UPDATE gallery_brands SET name = ?, slug = ?, cover_image_path = ?, sort_order = ? WHERE id = ?'
|
||||||
);
|
);
|
||||||
$stmt->execute([
|
$stmt->execute([
|
||||||
$name,
|
$name,
|
||||||
Slug::make($name),
|
Slug::make($name),
|
||||||
trim($body['cover_image_path'] ?? '') ?: null,
|
$newCoverPath,
|
||||||
(int) ($body['sort_order'] ?? 0),
|
(int) ($body['sort_order'] ?? 0),
|
||||||
$id,
|
$id,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
Uploads::deleteIfOrphaned($oldCoverPath ?: null, $newCoverPath);
|
||||||
|
|
||||||
Http::send(['ok' => true]);
|
Http::send(['ok' => true]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function destroy(array $params): void
|
public static function destroy(array $params): void
|
||||||
{
|
{
|
||||||
$stmt = Database::connection()->prepare('DELETE FROM gallery_brands WHERE id = ?');
|
$id = (int) $params['id'];
|
||||||
$stmt->execute([(int) $params['id']]);
|
$db = Database::connection();
|
||||||
|
|
||||||
|
$oldStmt = $db->prepare('SELECT cover_image_path FROM gallery_brands WHERE id = ?');
|
||||||
|
$oldStmt->execute([$id]);
|
||||||
|
$oldCoverPath = $oldStmt->fetchColumn();
|
||||||
|
|
||||||
|
// ON DELETE CASCADE raeumt gallery_projects/gallery_photos in der DB mit auf, aber die
|
||||||
|
// dazugehoerigen Bilddateien muessen wir vorher selbst einsammeln - nach dem DELETE sind
|
||||||
|
// die Zeilen (und damit die Pfade) weg.
|
||||||
|
$projectImagesStmt = $db->prepare('SELECT cover_image_path FROM gallery_projects WHERE gallery_brand_id = ?');
|
||||||
|
$projectImagesStmt->execute([$id]);
|
||||||
|
$projectImages = $projectImagesStmt->fetchAll(\PDO::FETCH_COLUMN);
|
||||||
|
|
||||||
|
$photoImagesStmt = $db->prepare(
|
||||||
|
'SELECT p.image_path FROM gallery_photos p
|
||||||
|
JOIN gallery_projects gp ON gp.id = p.gallery_project_id
|
||||||
|
WHERE gp.gallery_brand_id = ?'
|
||||||
|
);
|
||||||
|
$photoImagesStmt->execute([$id]);
|
||||||
|
$photoImages = $photoImagesStmt->fetchAll(\PDO::FETCH_COLUMN);
|
||||||
|
|
||||||
|
$stmt = $db->prepare('DELETE FROM gallery_brands WHERE id = ?');
|
||||||
|
$stmt->execute([$id]);
|
||||||
|
|
||||||
|
foreach ([$oldCoverPath, ...$projectImages, ...$photoImages] as $path) {
|
||||||
|
Uploads::deleteIfOrphaned($path ?: null, null);
|
||||||
|
}
|
||||||
|
|
||||||
Http::send(['ok' => true]);
|
Http::send(['ok' => true]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ namespace App\Controllers;
|
||||||
|
|
||||||
use App\Config\Database;
|
use App\Config\Database;
|
||||||
use App\Support\Http;
|
use App\Support\Http;
|
||||||
|
use App\Support\Uploads;
|
||||||
|
|
||||||
class GalleryPhotoController
|
class GalleryPhotoController
|
||||||
{
|
{
|
||||||
|
|
@ -50,8 +51,18 @@ class GalleryPhotoController
|
||||||
|
|
||||||
public static function destroy(array $params): void
|
public static function destroy(array $params): void
|
||||||
{
|
{
|
||||||
$stmt = Database::connection()->prepare('DELETE FROM gallery_photos WHERE id = ?');
|
$id = (int) $params['id'];
|
||||||
$stmt->execute([(int) $params['id']]);
|
$db = Database::connection();
|
||||||
|
|
||||||
|
$oldStmt = $db->prepare('SELECT image_path FROM gallery_photos WHERE id = ?');
|
||||||
|
$oldStmt->execute([$id]);
|
||||||
|
$oldImagePath = $oldStmt->fetchColumn();
|
||||||
|
|
||||||
|
$stmt = $db->prepare('DELETE FROM gallery_photos WHERE id = ?');
|
||||||
|
$stmt->execute([$id]);
|
||||||
|
|
||||||
|
Uploads::deleteIfOrphaned($oldImagePath ?: null, null);
|
||||||
|
|
||||||
Http::send(['ok' => true]);
|
Http::send(['ok' => true]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ namespace App\Controllers;
|
||||||
use App\Config\Database;
|
use App\Config\Database;
|
||||||
use App\Support\Http;
|
use App\Support\Http;
|
||||||
use App\Support\Slug;
|
use App\Support\Slug;
|
||||||
|
use App\Support\Uploads;
|
||||||
|
|
||||||
class GalleryProjectController
|
class GalleryProjectController
|
||||||
{
|
{
|
||||||
|
|
@ -55,25 +56,51 @@ class GalleryProjectController
|
||||||
Http::error('Name und Marke erforderlich', 422);
|
Http::error('Name und Marke erforderlich', 422);
|
||||||
}
|
}
|
||||||
|
|
||||||
$stmt = Database::connection()->prepare(
|
$newCoverPath = trim($body['cover_image_path'] ?? '') ?: null;
|
||||||
|
$db = Database::connection();
|
||||||
|
$oldStmt = $db->prepare('SELECT cover_image_path FROM gallery_projects WHERE id = ?');
|
||||||
|
$oldStmt->execute([$id]);
|
||||||
|
$oldCoverPath = $oldStmt->fetchColumn();
|
||||||
|
|
||||||
|
$stmt = $db->prepare(
|
||||||
'UPDATE gallery_projects SET gallery_brand_id = ?, name = ?, slug = ?, cover_image_path = ?, sort_order = ? WHERE id = ?'
|
'UPDATE gallery_projects SET gallery_brand_id = ?, name = ?, slug = ?, cover_image_path = ?, sort_order = ? WHERE id = ?'
|
||||||
);
|
);
|
||||||
$stmt->execute([
|
$stmt->execute([
|
||||||
$brandId,
|
$brandId,
|
||||||
$name,
|
$name,
|
||||||
Slug::make($name),
|
Slug::make($name),
|
||||||
trim($body['cover_image_path'] ?? '') ?: null,
|
$newCoverPath,
|
||||||
(int) ($body['sort_order'] ?? 0),
|
(int) ($body['sort_order'] ?? 0),
|
||||||
$id,
|
$id,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
Uploads::deleteIfOrphaned($oldCoverPath ?: null, $newCoverPath);
|
||||||
|
|
||||||
Http::send(['ok' => true]);
|
Http::send(['ok' => true]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function destroy(array $params): void
|
public static function destroy(array $params): void
|
||||||
{
|
{
|
||||||
$stmt = Database::connection()->prepare('DELETE FROM gallery_projects WHERE id = ?');
|
$id = (int) $params['id'];
|
||||||
$stmt->execute([(int) $params['id']]);
|
$db = Database::connection();
|
||||||
|
|
||||||
|
$oldStmt = $db->prepare('SELECT cover_image_path FROM gallery_projects WHERE id = ?');
|
||||||
|
$oldStmt->execute([$id]);
|
||||||
|
$oldCoverPath = $oldStmt->fetchColumn();
|
||||||
|
|
||||||
|
// ON DELETE CASCADE raeumt gallery_photos in der DB mit auf - deren Bilddateien
|
||||||
|
// muessen wir vorher einsammeln, danach sind die Zeilen (und Pfade) weg.
|
||||||
|
$photoImagesStmt = $db->prepare('SELECT image_path FROM gallery_photos WHERE gallery_project_id = ?');
|
||||||
|
$photoImagesStmt->execute([$id]);
|
||||||
|
$photoImages = $photoImagesStmt->fetchAll(\PDO::FETCH_COLUMN);
|
||||||
|
|
||||||
|
$stmt = $db->prepare('DELETE FROM gallery_projects WHERE id = ?');
|
||||||
|
$stmt->execute([$id]);
|
||||||
|
|
||||||
|
foreach ([$oldCoverPath, ...$photoImages] as $path) {
|
||||||
|
Uploads::deleteIfOrphaned($path ?: null, null);
|
||||||
|
}
|
||||||
|
|
||||||
Http::send(['ok' => true]);
|
Http::send(['ok' => true]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ namespace App\Controllers;
|
||||||
|
|
||||||
use App\Config\Database;
|
use App\Config\Database;
|
||||||
use App\Support\Http;
|
use App\Support\Http;
|
||||||
|
use App\Support\Uploads;
|
||||||
|
|
||||||
class ServiceController
|
class ServiceController
|
||||||
{
|
{
|
||||||
|
|
@ -44,8 +45,14 @@ class ServiceController
|
||||||
$id = (int) $params['id'];
|
$id = (int) $params['id'];
|
||||||
$body = Http::jsonBody();
|
$body = Http::jsonBody();
|
||||||
[$iconName, $title, $description] = self::validate($body);
|
[$iconName, $title, $description] = self::validate($body);
|
||||||
|
$newImagePath = $body['image_path'] ?? null;
|
||||||
|
|
||||||
$stmt = Database::connection()->prepare(
|
$db = Database::connection();
|
||||||
|
$oldStmt = $db->prepare('SELECT image_path FROM services WHERE id = ?');
|
||||||
|
$oldStmt->execute([$id]);
|
||||||
|
$oldImagePath = $oldStmt->fetchColumn();
|
||||||
|
|
||||||
|
$stmt = $db->prepare(
|
||||||
'UPDATE services SET icon_name = ?, title = ?, description = ?, image_path = ?, cta_label = ?, cta_url = ?, sort_order = ?
|
'UPDATE services SET icon_name = ?, title = ?, description = ?, image_path = ?, cta_label = ?, cta_url = ?, sort_order = ?
|
||||||
WHERE id = ?'
|
WHERE id = ?'
|
||||||
);
|
);
|
||||||
|
|
@ -53,20 +60,32 @@ class ServiceController
|
||||||
$iconName,
|
$iconName,
|
||||||
$title,
|
$title,
|
||||||
$description,
|
$description,
|
||||||
$body['image_path'] ?? null,
|
$newImagePath,
|
||||||
$body['cta_label'] ?? null,
|
$body['cta_label'] ?? null,
|
||||||
$body['cta_url'] ?? null,
|
$body['cta_url'] ?? null,
|
||||||
(int) ($body['sort_order'] ?? 0),
|
(int) ($body['sort_order'] ?? 0),
|
||||||
$id,
|
$id,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
Uploads::deleteIfOrphaned($oldImagePath ?: null, $newImagePath);
|
||||||
|
|
||||||
Http::send(['ok' => true]);
|
Http::send(['ok' => true]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function destroy(array $params): void
|
public static function destroy(array $params): void
|
||||||
{
|
{
|
||||||
$stmt = Database::connection()->prepare('DELETE FROM services WHERE id = ?');
|
$id = (int) $params['id'];
|
||||||
$stmt->execute([(int) $params['id']]);
|
$db = Database::connection();
|
||||||
|
|
||||||
|
$oldStmt = $db->prepare('SELECT image_path FROM services WHERE id = ?');
|
||||||
|
$oldStmt->execute([$id]);
|
||||||
|
$oldImagePath = $oldStmt->fetchColumn();
|
||||||
|
|
||||||
|
$stmt = $db->prepare('DELETE FROM services WHERE id = ?');
|
||||||
|
$stmt->execute([$id]);
|
||||||
|
|
||||||
|
Uploads::deleteIfOrphaned($oldImagePath ?: null, null);
|
||||||
|
|
||||||
Http::send(['ok' => true]);
|
Http::send(['ok' => true]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ namespace App\Controllers;
|
||||||
|
|
||||||
use App\Config\Database;
|
use App\Config\Database;
|
||||||
use App\Support\Http;
|
use App\Support\Http;
|
||||||
|
use App\Support\Uploads;
|
||||||
|
|
||||||
class SiteSettingsController
|
class SiteSettingsController
|
||||||
{
|
{
|
||||||
|
|
@ -64,6 +65,11 @@ class SiteSettingsController
|
||||||
$wheelHintInterval = (int) ($body['package_wheel_hint_interval'] ?? 8);
|
$wheelHintInterval = (int) ($body['package_wheel_hint_interval'] ?? 8);
|
||||||
$wheelHintInterval = max(2, min(120, $wheelHintInterval ?: 8));
|
$wheelHintInterval = max(2, min(120, $wheelHintInterval ?: 8));
|
||||||
|
|
||||||
|
$newHeroImagePath = trim($body['hero_image_path'] ?? '') ?: null;
|
||||||
|
$oldStmt = $db->prepare('SELECT hero_image_path FROM app_settings WHERE id = 1');
|
||||||
|
$oldStmt->execute();
|
||||||
|
$oldHeroImagePath = $oldStmt->fetchColumn();
|
||||||
|
|
||||||
$db->exec('INSERT IGNORE INTO app_settings (id) VALUES (1)');
|
$db->exec('INSERT IGNORE INTO app_settings (id) VALUES (1)');
|
||||||
$stmt = $db->prepare(
|
$stmt = $db->prepare(
|
||||||
'UPDATE app_settings SET phone = ?, whatsapp = ?, contact_email = ?, hero_image_path = ?, ga_measurement_id = ?, package_card_theme = ?, package_card_layout = ?, package_wheel_hint_interval = ? WHERE id = 1'
|
'UPDATE app_settings SET phone = ?, whatsapp = ?, contact_email = ?, hero_image_path = ?, ga_measurement_id = ?, package_card_theme = ?, package_card_layout = ?, package_wheel_hint_interval = ? WHERE id = 1'
|
||||||
|
|
@ -72,13 +78,15 @@ class SiteSettingsController
|
||||||
trim($body['phone'] ?? '') ?: null,
|
trim($body['phone'] ?? '') ?: null,
|
||||||
trim($body['whatsapp'] ?? '') ?: null,
|
trim($body['whatsapp'] ?? '') ?: null,
|
||||||
trim($body['contact_email'] ?? '') ?: null,
|
trim($body['contact_email'] ?? '') ?: null,
|
||||||
trim($body['hero_image_path'] ?? '') ?: null,
|
$newHeroImagePath,
|
||||||
trim($body['ga_measurement_id'] ?? '') ?: null,
|
trim($body['ga_measurement_id'] ?? '') ?: null,
|
||||||
$packageCardTheme,
|
$packageCardTheme,
|
||||||
$packageCardLayout,
|
$packageCardLayout,
|
||||||
$wheelHintInterval,
|
$wheelHintInterval,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
Uploads::deleteIfOrphaned($oldHeroImagePath ?: null, $newHeroImagePath);
|
||||||
|
|
||||||
self::show();
|
self::show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
47
hifi/api/src/Support/Uploads.php
Normal file
47
hifi/api/src/Support/Uploads.php
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Support;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Kuemmert sich darum, dass ein durch ImageUploadField ersetztes oder entferntes Bild
|
||||||
|
* nicht als verwaiste Datei in hifi/api/uploads/ liegen bleibt. Bisher loeschte kein
|
||||||
|
* einziger Controller je eine physische Datei - jedes Ersetzen/Entfernen eines Bildes
|
||||||
|
* hat die alte Datei einfach auf der Platte gelassen.
|
||||||
|
*/
|
||||||
|
class Uploads
|
||||||
|
{
|
||||||
|
private const DIR = __DIR__ . '/../../uploads/';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loescht $oldPath von der Platte, aber nur wenn es sich tatsaechlich von $newPath
|
||||||
|
* unterscheidet (sonst wuerde ein unveraendert gespeichertes Bild sich selbst loeschen)
|
||||||
|
* und der Dateiname (ueber basename() aufgeloest, kein Path-Traversal moeglich)
|
||||||
|
* wirklich innerhalb von hifi/api/uploads/ liegt.
|
||||||
|
*/
|
||||||
|
public static function deleteIfOrphaned(?string $oldPath, ?string $newPath): void
|
||||||
|
{
|
||||||
|
$oldPath = trim((string) $oldPath);
|
||||||
|
$newPath = trim((string) $newPath);
|
||||||
|
|
||||||
|
if ($oldPath === '' || $oldPath === $newPath) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$filename = basename($oldPath);
|
||||||
|
if ($filename === '' || $filename === '.' || $filename === '..') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$fullPath = self::DIR . $filename;
|
||||||
|
$resolved = realpath($fullPath);
|
||||||
|
$uploadsDir = realpath(self::DIR);
|
||||||
|
|
||||||
|
if ($resolved === false || $uploadsDir === false || !str_starts_with($resolved, $uploadsDir)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_file($resolved)) {
|
||||||
|
@unlink($resolved);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
1
hifi/assets/index-Cvi6-oeK.css
Normal file
1
hifi/assets/index-Cvi6-oeK.css
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
|
@ -43,9 +43,9 @@
|
||||||
"sameAs": ["https://www.youtube.com/@hifiplanet2812"]
|
"sameAs": ["https://www.youtube.com/@hifiplanet2812"]
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
<script type="module" crossorigin src="/assets/index-3HD67C-w.js"></script>
|
<script type="module" crossorigin src="/assets/index-PD_IcG1Q.js"></script>
|
||||||
<link rel="modulepreload" crossorigin href="/assets/lucide-icons-66ioQXWI.js">
|
<link rel="modulepreload" crossorigin href="/assets/lucide-icons-66ioQXWI.js">
|
||||||
<link rel="stylesheet" crossorigin href="/assets/index-Cys8aTN9.css">
|
<link rel="stylesheet" crossorigin href="/assets/index-Cvi6-oeK.css">
|
||||||
</head>
|
</head>
|
||||||
<body class="bg-white text-slate-900 dark:bg-slate-950 dark:text-slate-100">
|
<body class="bg-white text-slate-900 dark:bg-slate-950 dark:text-slate-100">
|
||||||
<div id="root"></div>
|
<div id="root"></div>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue