Files
Epicnabbo-Catalogus-Updated…/Updated_Cms/app/Models/WebsiteAd.php
T
Remco 6a72aef110 🆙 More fixes 🆙
2026-01-19 20:46:38 +01:00

74 lines
2.4 KiB
PHP

<?php
namespace App\Models;
use App\Services\SettingsService;
use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
/**
* @property string $image
*/
class WebsiteAd extends Model
{
protected $fillable = [
'image',
];
/**
* @return \Illuminate\Database\Eloquent\Casts\Attribute<string, never>
*/
protected function imageUrl(): \Illuminate\Database\Eloquent\Casts\Attribute
{
return \Illuminate\Database\Eloquent\Casts\Attribute::make(get: function () {
$settingsService = app(SettingsService::class);
/** @var string $adsPicturePath */
$adsPicturePath = Cache::remember('ads_picture_path', 3600, fn () => $settingsService->getOrDefault('ads_picture_path'));
if (! str_starts_with($adsPicturePath, 'http')) {
$adsPicturePath = rtrim((string) (config('app.url') ?? ''), '/') . '/' . ltrim($adsPicturePath, '/');
}
return rtrim($adsPicturePath, '/') . '/' . $this->image;
});
}
#[\Override]
protected static function booted()
{
static::deleting(function (WebsiteAd $websiteAd): void {
try {
$websiteAd->configureAdsDisk();
logger()->info('Attempting to delete image file:', ['file' => $websiteAd->image]);
if ($websiteAd->image && Storage::disk('ads')->exists($websiteAd->image)) {
Storage::disk('ads')->delete($websiteAd->image);
logger()->info('Image file deleted:', ['file' => $websiteAd->image]);
} else {
logger()->warning('Image file not found:', ['file' => $websiteAd->image]);
}
} catch (Exception $e) {
logger()->error('Failed to delete image file:', [
'file' => $websiteAd->image,
'error' => $e->getMessage(),
]);
}
});
}
protected function configureAdsDisk(): void
{
$settingsService = app(SettingsService::class);
$adsPath = Cache::remember('ads_path_filesystem', 3600, fn () => $settingsService->getOrDefault('ads_path_filesystem'));
config(['filesystems.disks.ads' => [
'driver' => 'local',
'root' => $adsPath,
]]);
}
}