faultManagerService = $faultManagerService; $this->attachmentService = $attachmentService; } /** * Post a new fault report * * @param ServerRequestInterface $request * @param DelegateInterface $delegate * @return \Zend\Diactoros\Response */ public function create(ServerRequestInterface $request, DelegateInterface $delegate): Response { $faultId = $request->getAttribute('id'); $type = $request->getAttribute('type', 'file'); $token = $request->getAttribute('token'); try { /** @var UploadedFileInterface[] $files */ $files = $request->getUploadedFiles()['file']; return new JsonCorsResponse( $this->attachmentService->createAttachments($faultId, $files, $token->uid, $type), 201 ); } catch (\Exception $e) { return new JsonCorsResponse($e->getMessage(), 500); } } /** * @param ServerRequestInterface $request * @param DelegateInterface $delegate * @return Response */ public function get(ServerRequestInterface $request, DelegateInterface $delegate): Response { $id = $request->getAttribute('id'); $attachment = $this->attachmentService->get($id); $stream = new Stream($attachment->getIoStream()); switch ($attachment->getType()) { case 'image': $contentType = 'image/jpg'; break; case 'expense': $contentType = 'application/pdf'; break; default: $contentType = 'application/octet-stream'; } $response = new Response($stream); return $response->withStatus(200) ->withHeader('Content-type', $contentType) // ->withHeader('Content-disposition', 'attachment;filename='.$attachment->getFileName()) ; } /** * Configure CORS preflight * * @param ServerRequestInterface $request * @param DelegateInterface $delegate * @return \Zend\Diactoros\Response */ public function options(ServerRequestInterface $request, DelegateInterface $delegate): Response { return $this->withCorsHeaders(new EmptyResponse(), self::CORS_ALLOW_METHODS); } }