trypost/app/Models/Media.php
Paulo Castellano 9f3b8e547a fix: overhaul social publishing — validation, uploads, token refresh
- Fix UpdatePostRequest missing content_type, synced, meta fields
  (content_type was silently dropped, causing Instagram Reels to post as Feed)
- Create API FormRequests (StorePostRequest, UpdatePostRequest) replacing inline validation
- Fix syntax errors in all publishers ($media->isVideo() missing variable)
- Fix Instagram Feed with single video calling publishSingleImage instead of publishReel
- Fix TikTok hardcoded SELF_ONLY privacy — now queries creator_info API
- Refactor YouTubePublisher to use google/apiclient SDK with chunked resumable upload
- Fix all publishers using file_get_contents for large videos (memory overflow)
  — X, LinkedIn, LinkedInPage, Pinterest, Bluesky, Mastodon now use temp file + stream
- Fix Media::isVideo/isImage to use mime_type instead of extension
- Fix Threads not saving refresh_token (was null, now saves access_token)
- Add Instagram token refresh to publisher and ConnectionVerifier
- Fix PublishToSocialPlatform job: tries 3→1 (prevents duplicate uploads),
  timeout 60→600s, added failed() method for cleanup
- Increase Horizon worker timeout 60→630s, Redis retry_after 90→660s
- Increase upload limit 500MB→1GB
- Add mastodon to getDefaultContentType in Edit.vue
2026-03-31 19:25:19 -03:00

104 lines
2.5 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',
];
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 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();
}
}