Files
construprogress/app/Models/Media.php
T
Javier Braña 90630379fb chore: cleanup dead code + format with Pint
- Remove unused FeaturesController (empty stubs, no routes)
- Remove ConvertSpatialFile CLI command (unused; service used in LayerManager)
- Remove MigrateGeojsonToFeatures CLI command (one-shot migration, not referenced)
- Remove .claude/worktrees/ (11 old agent worktrees from June)
- Apply Laravel Pint formatting across 219 files (style only, no functional changes)

Tests: 101 passing (319 assertions)
API routes: unchanged (8 routes intact)
2026-08-28 13:04:28 +02:00

83 lines
1.9 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
class Media extends Model
{
protected $table = 'media';
protected $fillable = [
'mediable_type', 'mediable_id',
'name', 'file_path', 'file_type', 'file_extension', 'file_size',
'category', 'description', 'metadata', 'uploaded_by',
'uuid', 'client_updated_at',
];
protected $casts = [
'metadata' => 'array',
'file_size' => 'integer',
];
// Relación polimórfica: pertenece a Project, Phase, Layer o Feature
public function mediable()
{
return $this->morphTo();
}
public function uploader()
{
return $this->belongsTo(User::class, 'uploaded_by');
}
// Helper: URL pública del archivo
public function getUrlAttribute()
{
return Storage::url($this->file_path);
}
// Helper: si es imagen
public function getIsImageAttribute()
{
return str_starts_with($this->file_type, 'image/');
}
// Helper: tamaño formateado
public function getFormattedSizeAttribute()
{
$bytes = $this->file_size;
if ($bytes >= 1073741824) {
return round($bytes / 1073741824, 2).' GB';
}
if ($bytes >= 1048576) {
return round($bytes / 1048576, 1).' MB';
}
if ($bytes >= 1024) {
return round($bytes / 1024).' KB';
}
return $bytes.' B';
}
// Scopes
public function scopeImages($query)
{
return $query->where('category', 'image');
}
public function scopeDocuments($query)
{
return $query->where('category', 'document');
}
// Boot: borrar archivo físico al eliminar registro
protected static function booted()
{
static::deleting(function ($media) {
Storage::delete($media->file_path);
});
}
}