X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/8d7febe482f92a34093127c60c6e2dda342b4223..refs/pull/4193/head:/app/Http/Controllers/Api/AttachmentApiController.php diff --git a/app/Http/Controllers/Api/AttachmentApiController.php b/app/Http/Controllers/Api/AttachmentApiController.php index fc5008e3c..9fc7f3bde 100644 --- a/app/Http/Controllers/Api/AttachmentApiController.php +++ b/app/Http/Controllers/Api/AttachmentApiController.php @@ -13,11 +13,9 @@ use Illuminate\Validation\ValidationException; class AttachmentApiController extends ApiController { - protected $attachmentService; - - public function __construct(AttachmentService $attachmentService) - { - $this->attachmentService = $attachmentService; + public function __construct( + protected AttachmentService $attachmentService + ) { } /** @@ -87,14 +85,33 @@ class AttachmentApiController extends ApiController 'markdown' => $attachment->markdownLink(), ]); - if (!$attachment->external) { - $attachmentContents = $this->attachmentService->getAttachmentFromStorage($attachment); - $attachment->setAttribute('content', base64_encode($attachmentContents)); - } else { + // Simply return a JSON response of the attachment for link-based attachments + if ($attachment->external) { $attachment->setAttribute('content', $attachment->path); + + return response()->json($attachment); } - return response()->json($attachment); + // Build and split our core JSON, at point of content. + $splitter = 'CONTENT_SPLIT_LOCATION_' . time() . '_' . rand(1, 40000); + $attachment->setAttribute('content', $splitter); + $json = $attachment->toJson(); + $jsonParts = explode($splitter, $json); + // Get a stream for the file data from storage + $stream = $this->attachmentService->streamAttachmentFromStorage($attachment); + + return response()->stream(function () use ($jsonParts, $stream) { + // Output the pre-content JSON data + echo $jsonParts[0]; + + // Stream out our attachment data as base64 content + stream_filter_append($stream, 'convert.base64-encode', STREAM_FILTER_READ); + fpassthru($stream); + fclose($stream); + + // Output our post-content JSON data + echo $jsonParts[1]; + }, 200, ['Content-Type' => 'application/json']); } /** @@ -155,13 +172,13 @@ class AttachmentApiController extends ApiController 'name' => ['required', 'min:1', 'max:255', 'string'], 'uploaded_to' => ['required', 'integer', 'exists:pages,id'], 'file' => array_merge(['required_without:link'], $this->attachmentService->getFileValidationRules()), - 'link' => ['required_without:file', 'min:1', 'max:255', 'safe_url'], + 'link' => ['required_without:file', 'min:1', 'max:2000', 'safe_url'], ], 'update' => [ 'name' => ['min:1', 'max:255', 'string'], 'uploaded_to' => ['integer', 'exists:pages,id'], 'file' => $this->attachmentService->getFileValidationRules(), - 'link' => ['min:1', 'max:255', 'safe_url'], + 'link' => ['min:1', 'max:2000', 'safe_url'], ], ]; }