/**
  * Get resource
  *
  * @param Request $request
  * @param string  $id
  *
  * @return BinaryFileResponse
  */
 public function getAction(Request $request, $id)
 {
     $realId = $request->getSession()->get('_resource/' . $id);
     $class = $this->getParameter('rafrsr_resource.config')['class'];
     $resource = null;
     if ($class && $realId) {
         $resource = $this->getDoctrine()->getRepository($class)->find($realId);
     }
     if (!$resource) {
         throw new NotFoundHttpException();
     }
     // Generate response
     $response = new BinaryFileResponse($resource->getFile()->getRealPath());
     $response->setLastModified($resource->getUpdated());
     return $response;
 }
示例#2
0
 protected function serveFile($filename, $displayName, Request $request, $asAttachment = false)
 {
     $lastModified = new \DateTime();
     $lastModified->setTimestamp(filemtime($filename));
     $ifModifiedSince = $request->headers->has('If-Modified-Since') ? new \DateTime($request->headers->get('If-Modified-Since')) : false;
     $finfo = finfo_open(FILEINFO_MIME_TYPE);
     $mimeType = finfo_file($finfo, $filename);
     $response = new BinaryFileResponse($filename);
     $response->setMaxAge(10);
     $response->headers->addCacheControlDirective('must-revalidate');
     $response->headers->set('Content-Type', $mimeType);
     $response->setLastModified($lastModified);
     $response->setContentDisposition($asAttachment ? ResponseHeaderBag::DISPOSITION_ATTACHMENT : ResponseHeaderBag::DISPOSITION_INLINE, $displayName, mb_convert_encoding($displayName, "ASCII", "UTF-8"));
     $response->prepare($request);
     if ($ifModifiedSince && $ifModifiedSince <= $lastModified) {
         $response->setStatusCode(304);
     }
     return $response;
 }