trypost/app/Models/Media.php
Paulo Castellano 7388313f5c feat(linkedin): support PDF document (carousel) posts
Add LinkedIn document posts — the swipeable PDF carousel — for both personal profiles and company pages. This is the format every major competitor exposes via native PDF upload, and the reason a trial user churned.

- New 'document' media type (application/pdf) across the upload pipeline (Type enum, HasMedia, FormRequests incl. chunked, Platform media types)
- New LinkedInDocument / LinkedInPageDocument content types: PDF-only, single-file, with a supportsDocument() flag
- Publisher flow: documents initializeUpload -> PUT -> poll AVAILABLE -> post with content.media.{id,title}; optional document_title meta (falls back to file name)
- PDF is mutually exclusive with image/video, enforced in ContentTypeCompatibleWithMedia
- Frontend: 'Document (PDF)' variant, media rules (100MB cap), composer/gallery/detail PDF cards, real PDF embed in the LinkedIn editor preview, i18n in en/es/pt-BR
- Tests: publishers (personal + page, incl. processing-failure path), enums, compatibility rule, chunked PDF upload, API + MCP document_title round-trip

LinkedIn caps documents at 100MB / 300 pages (Documents API). The page limit is enforced by LinkedIn at publish, not validated client-side.
2026-06-24 16:32:27 -03:00

114 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models;
use App\Enums\Media\Type as MediaType;
use Database\Factories\MediaFactory;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Facades\Storage;
class Media extends Model
{
/** @use HasFactory<MediaFactory> */
use HasFactory, HasUuids;
protected $table = 'medias';
protected $appends = ['url'];
protected $fillable = [
'mediable_id',
'mediable_type',
'group_id',
'collection',
'type',
'path',
'original_filename',
'mime_type',
'size',
'order',
'meta',
'upload_token',
];
protected function casts(): array
{
return [
'type' => MediaType::class,
'size' => 'integer',
'order' => 'integer',
'meta' => 'array',
];
}
public function mediable(): MorphTo
{
return $this->morphTo();
}
protected function url(): Attribute
{
return Attribute::make(
get: fn () => Storage::url($this->path),
);
}
public function isVideo(): bool
{
if ($this->mime_type) {
return str_starts_with($this->mime_type, 'video/');
}
$extension = strtolower(pathinfo($this->path, PATHINFO_EXTENSION));
return in_array($extension, ['mp4', 'mov', 'avi', 'wmv', 'webm', 'mkv', 'm4v']);
}
public function isImage(): bool
{
if ($this->mime_type) {
return str_starts_with($this->mime_type, 'image/');
}
$extension = strtolower(pathinfo($this->path, PATHINFO_EXTENSION));
return in_array($extension, ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'svg', 'heic', 'heif']);
}
public function isDocument(): bool
{
if ($this->mime_type) {
return $this->mime_type === 'application/pdf';
}
return strtolower(pathinfo($this->path, PATHINFO_EXTENSION)) === 'pdf';
}
public function getTemporaryUrl(int $expirationMinutes = 60): string
{
return Storage::temporaryUrl(
$this->path,
now()->addMinutes($expirationMinutes)
);
}
public function delete(): bool
{
// Only delete the file if no other media records use the same path
$otherMediaWithSamePath = static::where('path', $this->path)
->where('id', '!=', $this->id)
->exists();
if (! $otherMediaWithSamePath) {
Storage::delete($this->path);
}
return parent::delete();
}
}