create() public static method

public static create ( SplFileInfo | string $file = null, integer $status = 200, array $headers = [], boolean $public = true, null | string $contentDisposition = null, boolean $autoEtag = false, boolean $autoLastModified = true ) : BinaryFileResponse
$file SplFileInfo | string The file to stream
$status integer The response status code
$headers array An array of response headers
$public boolean Files are public by default
$contentDisposition null | string The type of Content-Disposition to set automatically with the filename
$autoEtag boolean Whether the ETag header should be automatically set
$autoLastModified boolean Whether the Last-Modified header should be automatically set
return BinaryFileResponse The created response
Ejemplo n.º 1
0
 public function testXSendfile()
 {
     $request = Request::create('/');
     $request->headers->set('X-Sendfile-Type', 'X-Sendfile');
     BinaryFileResponse::trustXSendfileTypeHeader();
     $response = BinaryFileResponse::create('README.md');
     $response->prepare($request);
     $this->expectOutputString('');
     $response->sendContent();
     $this->assertContains('README.md', $response->headers->get('X-Sendfile'));
 }
 protected function createPrivateFileDownloadResponse(Request $request, $file)
 {
     $response = BinaryFileResponse::create($file['fullpath'], 200, array(), false);
     $response->trustXSendfileTypeHeader();
     $file['filename'] = urlencode($file['filename']);
     if (preg_match("/MSIE/i", $request->headers->get('User-Agent'))) {
         $response->headers->set('Content-Disposition', 'attachment; filename="' . $file['filename'] . '"');
     } else {
         $response->headers->set('Content-Disposition', "attachment; filename*=UTF-8''" . $file['filename']);
     }
     $mimeType = FileToolkit::getMimeTypeByExtension($file['ext']);
     if ($mimeType) {
         $response->headers->set('Content-Type', $mimeType);
     }
     return $response;
 }
Ejemplo n.º 3
0
 /**
  *
  */
 protected function renderFile($file, $status = 200, $headers = array(), $public = true, $contentDisposition = null, $autoEtag = false, $autoLastModified = true)
 {
     // Check if a output action has be already performed
     $this->_checkIfPerformed();
     // Create a file response
     $fileResponse = \Symfony\Component\HttpFoundation\BinaryFileResponse::create($file, $status, $headers, $public, $contentDisposition, $autoEtag, $autoLastModified);
     // Add request informationen
     $fileResponse->prepare($this->application->requestStack->getCurrentRequest());
     // Replace response object
     $this->application->response = $fileResponse;
 }
 public function testAcceptRangeNotOverriden()
 {
     $request = Request::create('/', 'POST');
     $response = BinaryFileResponse::create(__DIR__ . '/File/Fixtures/test.gif');
     $response->headers->set('Accept-Ranges', 'foo');
     $response->prepare($request);
     $this->assertEquals('foo', $response->headers->get('Accept-Ranges'));
 }
 public function downloadAction(Request $request, $fileId)
 {
     $response = new Response();
     $user = $this->getCurrentUser();
     if (!$user->isLogin()) {
         return $this->redirect($this->generateUrl('login'));
     }
     $goods = $this->getThreadService()->getGoods($fileId);
     $file = $this->getFileService()->getFile($goods['fileId']);
     if ($goods['coin'] > 0 && $user['id'] != $file['userId']) {
         $trade = $this->getThreadService()->getTradeByUserIdAndGoodsId($user['id'], $goods['id']);
         if (!$trade) {
             return $this->createMessageResponse('info', '您未购买该附件!');
         }
     }
     $file = $this->getFileService()->getFile($goods['fileId']);
     $this->getThreadService()->waveGoodsHitNum($goods['id']);
     if (empty($file)) {
         throw $this->createNotFoundException();
     }
     $filename = $this->get('topxia.twig.web_extension')->getFilePath($file['uri']);
     $filename = substr($filename, 1);
     $filename = explode("?", $filename);
     $filename = $filename[0];
     $response = BinaryFileResponse::create($filename, 200, array(), false);
     $goods['title'] = urlencode($goods['title']);
     $goods['title'] = str_replace('+', '%20', $goods['title']);
     if (preg_match("/MSIE/i", $request->headers->get('User-Agent'))) {
         $response->headers->set('Content-Disposition', 'attachment; filename="' . $goods['title'] . '"');
     } else {
         $response->headers->set('Content-Disposition', "attachment; filename*=UTF-8''" . $goods['title']);
     }
     $response->headers->set('Content-type', "application/octet-stream");
     return $response;
 }
Ejemplo n.º 6
0
 public function getLocalVideo()
 {
     $fileId = $this->getParam("targetId");
     $user = $this->controller->getuserByToken($this->request);
     if (!$user->isLogin()) {
         return $this->createErrorResponse('not_login', "您尚未登录!");
     }
     $file = $this->getUploadFileService()->getFile($fileId);
     if (empty($file)) {
         return $this->createErrorResponse('error', "视频文件不存在!");
     }
     $response = BinaryFileResponse::create($file['fullpath'], 200, array(), false);
     $response->trustXSendfileTypeHeader();
     $mimeType = FileToolkit::getMimeTypeByExtension($file['ext']);
     if ($mimeType) {
         $response->headers->set('Content-Type', $mimeType);
     }
     return $response;
 }
 /**
  * Serve the image from the templates-folder
  * @param  string                                               $filename
  * @param  string                                               $folderKey
  * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
  */
 public function serveImageAction($folderKey, $filename)
 {
     $folder = $this->getTemplateProviderService()->getFolderFrom($folderKey);
     if ($folder !== false) {
         $fullPath = $folder . $filename;
         $response = BinaryFileResponse::create($fullPath);
         $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_INLINE);
         $response->headers->set("Content-Type", "image");
         return $response;
     }
     throw new FileNotFoundException($filename);
 }
 /**
  * @param  Request           $request
  * @param  Document          $document
  * @param  int               $page
  * @param  string            $size
  * @throws \RuntimeException
  * @return Response
  */
 public function pageImageAction(Request $request, Document $document, $page, $size)
 {
     $sizes = array('small' => 180, 'normal' => 700, 'large' => 1000);
     $width = $sizes[$size];
     $destination = realpath($this->container->getParameter('kernel.root_dir') . '/../web') . $request->getPathInfo();
     /** @var \Bangpound\Bundle\DocumentCloudBundle\BinaryDriver\GraphicsmagickDriver $driver */
     $driver = $this->get('bangpound_documentcloud.binary_driver.graphicsmagick');
     $filesystem = $this->get('filesystem');
     $filesystem->mkdir(dirname($destination));
     $arguments = array('convert', '+adjoin', '-define', 'pdf:use-cropbox=true', '-limit', 'memory', '256MiB', '-limit', 'map', '512MiB', '-density', '150', '-resize', $width . 'x', '-quality', 100, $document->getAbsolutePath() . '[' . ($page - 1) . ']', $destination);
     $builder = $driver->getProcessBuilderFactory();
     $process = $builder->create($arguments);
     $process->setEnv(array('PATH' => getenv('PATH')));
     $process->run();
     if (!$process->isSuccessful()) {
         throw new \RuntimeException($process->getErrorOutput());
     }
     return BinaryFileResponse::create($destination);
 }
Ejemplo n.º 9
0
 public function localMediaAction(Request $request, $id, $token)
 {
     $file = $this->getUploadFileService()->getFile($id);
     if (empty($file)) {
         throw $this->createNotFoundException();
     }
     if (!in_array($file["type"], array("audio", "video"))) {
         throw $this->createAccessDeniedException();
     }
     $token = $this->getTokenService()->verifyToken('local.media', $token);
     if ($token['userId'] != $this->getCurrentUser()->getId()) {
         throw $this->createAccessDeniedException();
     }
     $response = BinaryFileResponse::create($file['fullpath'], 200, array(), false);
     $response->trustXSendfileTypeHeader();
     $mimeType = FileToolkit::getMimeTypeByExtension($file['ext']);
     if ($mimeType) {
         $response->headers->set('Content-Type', $mimeType);
     }
     return $response;
 }