Sistema de archivos multimedia: MediaManager, checkbox imágenes en mapa, modal visor, subida por feature/proyecto
This commit is contained in:
@@ -29,4 +29,14 @@ class Feature extends Model
|
||||
{
|
||||
return $this->hasMany(Inspection::class, 'feature_id');
|
||||
}
|
||||
|
||||
public function media()
|
||||
{
|
||||
return $this->morphMany(Media::class, 'mediable');
|
||||
}
|
||||
|
||||
public function images()
|
||||
{
|
||||
return $this->morphMany(Media::class, 'mediable')->where('category', 'image');
|
||||
}
|
||||
}
|
||||
@@ -33,4 +33,9 @@ class Layer extends Model
|
||||
{
|
||||
return $this->hasMany(Feature::class);
|
||||
}
|
||||
|
||||
public function media()
|
||||
{
|
||||
return $this->morphMany(Media::class, 'mediable');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?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',
|
||||
];
|
||||
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -38,4 +38,14 @@ class Phase extends Model
|
||||
{
|
||||
return $this->hasManyThrough(Feature::class, Layer::class);
|
||||
}
|
||||
|
||||
public function media()
|
||||
{
|
||||
return $this->morphMany(Media::class, 'mediable');
|
||||
}
|
||||
|
||||
public function images()
|
||||
{
|
||||
return $this->morphMany(Media::class, 'mediable')->where('category', 'image');
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,16 @@ class Project extends Model
|
||||
return $this->belongsTo(User::class, 'created_by');
|
||||
}
|
||||
|
||||
public function media()
|
||||
{
|
||||
return $this->morphMany(Media::class, 'mediable');
|
||||
}
|
||||
|
||||
public function images()
|
||||
{
|
||||
return $this->morphMany(Media::class, 'mediable')->where('category', 'image');
|
||||
}
|
||||
|
||||
// Scope to filter accessible projects for non-admin users
|
||||
public function scopeAccessibleBy($query, User $user)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user