Files
construprogress/app/Models/Media.php
T
javier 14758136b6 feat(api): mobile API Milestones 5+6 — media upload, sync_logs idempotency, OpenAPI
Milestone 5 (media):
- POST /api/v1/media — multipart upload, attaches to feature/issue/project/
  phase/layer, idempotent by uuid, authz member + 'upload media'. Added
  uuid+client_updated_at to media.
- Bundle now includes a 'media' array (URLs) for the project's project/feature/
  issue attachments (delta-aware).

Milestone 6 (hardening + docs):
- sync_logs table/model: every applied op is logged; /sync short-circuits on a
  repeated op uuid -> 'duplicate' (true idempotency for updates too, not just
  creates).
- Rate limiting on login (10/min), sync (60/min), media (120/min).
- docs/openapi.yaml: OpenAPI 3 contract for the mobile team.

Tests: 18 passing (added media upload idempotency + sync_logs idempotency).
The mobile API (Milestones 1-6) is now feature-complete on the webapp side.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-18 10:23:50 +02:00

75 lines
1.8 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);
});
}
}