58 lines
1.3 KiB
PHP
58 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class Upload extends Model
|
|
{
|
|
protected $table = 'uploads';
|
|
|
|
protected $fillable = [
|
|
'uploader_user_id',
|
|
'file_name',
|
|
'ext',
|
|
'mime_type',
|
|
'size_mb',
|
|
'storage_path'
|
|
];
|
|
|
|
public function uploader()
|
|
{
|
|
return $this->belongsTo(User::class, 'uploader_user_id');
|
|
}
|
|
|
|
public function normalizedStoragePath(): string
|
|
{
|
|
$path = ltrim((string) $this->storage_path, '/\\');
|
|
|
|
foreach (['storage/', 'public/'] as $prefix) {
|
|
if (str_starts_with($path, $prefix)) {
|
|
$path = substr($path, strlen($prefix));
|
|
}
|
|
}
|
|
|
|
return str_replace('\\', '/', $path);
|
|
}
|
|
|
|
public function existsOnPublicDisk(): bool
|
|
{
|
|
$path = $this->normalizedStoragePath();
|
|
|
|
return $path !== '' && Storage::disk('public')->exists($path);
|
|
}
|
|
|
|
public function publicDiskPath(): string
|
|
{
|
|
return Storage::disk('public')->path($this->normalizedStoragePath());
|
|
}
|
|
|
|
public function isStoredIn(string $directory): bool
|
|
{
|
|
$directory = trim(str_replace('\\', '/', $directory), '/');
|
|
|
|
return str_starts_with($this->normalizedStoragePath(), $directory . '/');
|
|
}
|
|
}
|