trypost/app/Http/Requests/App/Asset/StoreChunkedAssetRequest.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

68 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Requests\App\Asset;
use App\Enums\Media\Type as MediaType;
use Illuminate\Foundation\Http\FormRequest;
/**
* Validates a chunked upload request. The chunk metadata (offset / total
* size / filename) is encoded in the `Content-Range` and `X-File-Name`
* headers, not in the body, so we lift it into the request bag via
* `prepareForValidation` and then run standard rules against it.
*/
class StoreChunkedAssetRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
protected function prepareForValidation(): void
{
$parsed = sscanf((string) $this->header('Content-Range'), 'bytes %d-%d/%d') ?: [];
$this->merge([
'range_start' => $parsed[0] ?? null,
'range_end' => $parsed[1] ?? null,
'total_size' => $parsed[2] ?? null,
// Lowercase the name so `ends_with` validation is effectively
// case-insensitive (IMG_1234.JPG vs img_1234.jpg).
'file_name' => strtolower((string) $this->header('X-File-Name', 'upload')),
]);
}
/**
* @return array<string, array<int, string>>
*/
public function rules(): array
{
$allowedSuffixes = collect([MediaType::Image, MediaType::Video, MediaType::Document])
->flatMap(fn (MediaType $type) => $type->extensions())
->map(fn (string $ext) => '.'.$ext)
->all();
return [
'range_start' => ['required', 'integer', 'min:0'],
'range_end' => ['required', 'integer', 'gte:range_start'],
'total_size' => ['required', 'integer', 'min:1', 'max:'.MediaType::Video->maxSizeInBytes()],
'file_name' => ['required', 'string', 'ends_with:'.implode(',', $allowedSuffixes)],
];
}
/**
* @return array<string, string>
*/
public function messages(): array
{
return [
'range_start.required' => 'Invalid Content-Range header',
'range_end.required' => 'Invalid Content-Range header',
'total_size.required' => 'Invalid Content-Range header',
'total_size.max' => 'File size exceeds the maximum allowed ('.MediaType::Video->maxSizeInMb().' MB).',
'file_name.ends_with' => 'File type not supported.',
];
}
}