/** * {@inheritdoc} */ public function renderFile(CRUDEntity $entity, $entityName, $field) { $targetPath = $this->getPath($entityName, $entity, $field); $fileName = $entity->get($field); $file = $targetPath . '/' . $fileName; $response = new Response(''); $finfo = finfo_open(FILEINFO_MIME_TYPE); $mimeType = finfo_file($finfo, $file); finfo_close($finfo); $size = filesize($file); if ($fileName && file_exists($file)) { $response = new StreamedResponse(CRUDStreamedFileResponse::getStreamedFileFunction($file), 200, array('Content-Type' => $mimeType, 'Content-Disposition' => 'attachment; filename="' . $fileName . '"', 'Content-length' => $size)); $response->send(); } return $response; }
/** * The controller for serving static files. * * @param Application $app * the Silex application * * @return Response * redirects to the instance details page or 404 on invalid input */ public function staticFile(Application $app) { $fileParam = $app['request']->get('file'); if (!$fileParam) { return $this->getNotFoundPage($app, $app['translator']->trans('crudlex.resourceNotFound')); } $file = __DIR__ . '/../static/' . $fileParam; if (!file_exists($file)) { return $this->getNotFoundPage($app, $app['translator']->trans('crudlex.resourceNotFound')); } $extension = pathinfo($file, PATHINFO_EXTENSION); $mimeType = 'text/css'; if (strtolower($extension) !== 'css') { $finfo = finfo_open(FILEINFO_MIME_TYPE); $mimeType = finfo_file($finfo, $file); finfo_close($finfo); } $size = filesize($file); $response = new StreamedResponse(CRUDStreamedFileResponse::getStreamedFileFunction($file), 200, array('Content-Type' => $mimeType, 'Content-Disposition' => 'attachment; filename="' . basename($file) . '"', 'Content-length' => $size)); $response->send(); return $response; }