/** * @param AttachmentDto $attachmentDto * @return BinaryFileResponse */ protected function getFileDownloadResponse(AttachmentDto $attachmentDto) { $response = new BinaryFileResponse($attachmentDto->getFilePath()); $response->trustXSendfileTypeHeader(); $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $attachmentDto->getFileName(), iconv('UTF-8', 'ASCII//TRANSLIT', $attachmentDto->getFileName())); return $response; }
/** * @Route( * "/download/thumbnail/{hash}", * name="diamante_attachment_thumbnail_download", * requirements={"hash"="\w+"} * ) * * @param string $hash * @return BinaryFileResponse */ public function thumbnailAttachmentAction($hash) { $attachmentService = $this->get('diamante.attachment.service'); try { $file = $attachmentService->getThumbnail($hash); $attachmentDto = new AttachmentDto(); $attachmentDto->setFileName($file->getFilename()); $attachmentDto->setFilePath($file->getPathname()); $response = $this->getFileDownloadResponse($attachmentDto); return $response; } catch (\Exception $e) { $this->handleException($e); throw $this->createNotFoundException('Attachment not found'); } }
private function getFileDownloadResponse(AttachmentDto $attachmentDto) { $response = new \Symfony\Component\HttpFoundation\BinaryFileResponse($attachmentDto->getFilePath()); $response->trustXSendfileTypeHeader(); $response->setContentDisposition(\Symfony\Component\HttpFoundation\ResponseHeaderBag::DISPOSITION_ATTACHMENT, $attachmentDto->getFileName(), iconv('UTF-8', 'ASCII//TRANSLIT', $attachmentDto->getFileName())); return $response; }
/** * @Route( * "/attachment/download/ticket/{ticketId}/attachment/{attachId}", * name="diamante_ticket_attachment_download", * requirements={"ticketId"="\d+", "attachId"="\d+"} * ) * * @return BinaryFileResponse * @todo refactor download logic */ public function downloadAttachmentAction($ticketId, $attachId) { $retrieveTicketAttachmentCommand = new RetrieveTicketAttachmentCommand(); $retrieveTicketAttachmentCommand->ticketId = $ticketId; $retrieveTicketAttachmentCommand->attachmentId = $attachId; try { $attachment = $this->get('diamante.ticket.service')->getTicketAttachment($retrieveTicketAttachmentCommand); $attachmentDto = AttachmentDto::createFromAttachment($attachment); $response = $this->getFileDownloadResponse($attachmentDto); return $response; } catch (\Exception $e) { $this->handleException($e); throw $this->createNotFoundException('Attachment not found'); } }