You've already forked Atomcms-edit
Initial commit
This commit is contained in:
Executable
+82
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Services\SettingsService;
|
||||
use Carbon\Carbon;
|
||||
use Exception;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $image
|
||||
* @property Carbon|null $created_at
|
||||
* @property Carbon|null $updated_at
|
||||
* @property-read string $imageUrl
|
||||
*
|
||||
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteAd pluck($column, $key = null)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteAd insert($values)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteAd truncate()
|
||||
* @method static mixed toArray()
|
||||
*/
|
||||
class WebsiteAd extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
#[\Override]
|
||||
protected $fillable = [
|
||||
'image',
|
||||
];
|
||||
|
||||
public function getImageUrlAttribute(): string
|
||||
{
|
||||
$settingsService = app(SettingsService::class);
|
||||
|
||||
$adsPicturePath = Cache::remember('ads_picture_path', 3600, fn () => $settingsService->getOrDefault('ads_picture_path'));
|
||||
|
||||
if (! str_starts_with((string) $adsPicturePath, 'http')) {
|
||||
$adsPicturePath = rtrim((string) config('app.url'), '/') . '/' . ltrim((string) $adsPicturePath, '/');
|
||||
}
|
||||
|
||||
return rtrim((string) $adsPicturePath, '/') . '/' . $this->image;
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
protected static function booted()
|
||||
{
|
||||
static::deleting(function ($websiteAd) {
|
||||
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,
|
||||
]]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user