$uriVariables * @param array $context * @return Response|RedirectResponse * @throws FileNotFoundException */ public function provide(Operation $operation, array $uriVariables = [], array $context = []): Response | RedirectResponse { if($operation instanceof GetCollection) { throw new RuntimeException('not supported', HttpCodeEnum::METHOD_NOT_ALLOWED()->getValue()); } return $this->serveFile($uriVariables['fileId']); } /** * @param int $fileId * @return Response * @throws FileNotFoundException */ protected function serveFile(int $fileId): Response { $file = $this->fileRepository->find($fileId); if (empty($file)) { throw new RuntimeException("File " . $fileId . " does not exist; abort."); } if ($file->getStatus() !== FileStatusEnum::READY()->getValue()) { throw new RuntimeException("File " . $fileId . " has " . $file->getStatus() . " status; abort."); } $content = $this->fileManager->read($file); // Build the response and attach the file to it // @see https://symfony.com/doc/current/components/http_foundation.html#serving-files $response = new Response($content); $response->headers->set('Charset', 'UTF-8'); $response->headers->set('Access-Control-Expose-Headers', 'Content-Disposition'); if (!empty($file->getMimeType())) { $response->headers->set('Content-Type', $file->getMimeType()); } $response->headers->set( 'Content-Disposition', HeaderUtils::makeDisposition(HeaderUtils::DISPOSITION_ATTACHMENT, $file->getName()) ); return $response; } }