trypost/app/Http/Requests/App/Media/StoreChunkedMediaRequest.php
Paulo Castellano c48c774e23 feat: publishing engine improvements — rate limit retry, inline token refresh, per-platform queues, proactive refresh
- Add HasSocialHttpClient trait with 429 rate limit retry (3 attempts, 5s delay)
- Integrate trait into all 10 publishers (YouTube uses Google SDK)
- Add inline token refresh retry in PublishToSocialPlatform job
- Add per-platform Horizon queues via Platform::queue() and Platform::allQueues()
- Add RefreshExpiringTokens hourly command for proactive token refresh
- Fix token leaks: redact response bodies in all Log::error calls
- Fix token leaks: remove $response->body() from exception messages
- Fix ConnectionVerifier: redact all refresh error logs
- Fix null checks on API response IDs (Instagram, Threads, Pinterest, Facebook)
- Fix PublishPost::failed() to mark post as failed
- Fix StoreChunkedMediaRequest: validate max 1GB total size
- Fix scheduled_at validation: string → date
- Fix StoreMediaRequest: images max 10MB, videos max 1GB, only MP4 video
2026-04-01 10:51:53 -03:00

106 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Requests\App\Media;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class StoreChunkedMediaRequest extends FormRequest
{
private const CONTENT_RANGE_PATTERN = '/bytes (\d+)-(\d+)\/(\d+)/';
protected array $allowedModels = [
'postPlatform',
'workspace',
'user',
];
public function authorize(): bool
{
return true;
}
public function prepareForValidation(): void
{
$this->merge([
'model' => $this->header('X-Model'),
'model_id' => $this->header('X-Model-Id'),
'collection' => $this->header('X-Collection', 'default'),
'file_name' => $this->header('X-File-Name', 'upload'),
'content_range' => $this->header('Content-Range'),
]);
}
public function rules(): array
{
return [
'content_range' => ['required', 'string', 'regex:'.self::CONTENT_RANGE_PATTERN],
'model' => ['required', 'string', Rule::in($this->allowedModels)],
'model_id' => ['required', 'string'],
'collection' => ['sometimes', 'string', 'max:255'],
'file_name' => ['required', 'string', 'max:255'],
];
}
public function after(): array
{
return [
function ($validator) {
if ($validator->errors()->has('content_range')) {
return;
}
if ($this->totalSize() > 1073741824) { // 1GB
$validator->errors()->add('content_range', 'File size exceeds the maximum allowed (1GB).');
}
},
];
}
public function rangeStart(): int
{
return (int) $this->parsedRange()[1];
}
public function rangeEnd(): int
{
return (int) $this->parsedRange()[2];
}
public function totalSize(): int
{
return (int) $this->parsedRange()[3];
}
public function isLastChunk(): bool
{
return ($this->rangeEnd() + 1) >= $this->totalSize();
}
public function isFirstChunk(): bool
{
return $this->rangeStart() === 0;
}
public function progress(): int
{
return (int) round(($this->rangeEnd() + 1) / $this->totalSize() * 100);
}
public function chunkIdentifier(): string
{
return md5($this->input('file_name').$this->totalSize());
}
/**
* @return array<int, string>
*/
private function parsedRange(): array
{
preg_match(self::CONTENT_RANGE_PATTERN, $this->input('content_range'), $matches);
return $matches;
}
}