getMimeType() public method

Function to get the mimetype of a file.
public getMimeType ( string $file ) : string
$file string the file to get the mimetype from
return string the mimetype
示例#1
0
 public function testGetMimeType()
 {
     $mimeTypes = new MimeTypes();
     $read = $mimeTypes->getMimeType('test.css');
     $expected = 'text/css';
     $this->assertSame($expected, $read);
     $read = $mimeTypes->getMimeType('test.jpg');
     $expected = 'image/jpeg';
     $this->assertSame($expected, $read);
     $read = $mimeTypes->getMimeType(__DIR__ . '/../../src/static/css/vendor/bootstrap/bootstrap.css');
     $expected = 'text/css';
     $this->assertSame($expected, $read);
     $read = $mimeTypes->getMimeType(__DIR__ . '/../../src/static/images/flags/ad.png');
     $expected = 'image/png';
     $this->assertSame($expected, $read);
     $read = $mimeTypes->getMimeType(__DIR__ . '/../../src/static/js/vendor/moment.min.js');
     $expected = 'application/x-javascript';
     $this->assertSame($expected, $read);
     $read = $mimeTypes->getMimeType(null);
     $expected = 'application/octet-stream';
     $this->assertSame($expected, $read);
     $read = $mimeTypes->getMimeType('foo');
     $expected = 'application/octet-stream';
     $this->assertSame($expected, $read);
     $read = $mimeTypes->getMimeType('');
     $expected = 'application/octet-stream';
     $this->assertSame($expected, $read);
 }
示例#2
0
 /**
  * The controller for serving static files.
  *
  * @param Request $request
  * the current request
  * @param Application $app
  * the Silex application
  *
  * @return Response
  * redirects to the instance details page or 404 on invalid input
  */
 public function staticFile(Request $request, Application $app)
 {
     $fileParam = str_replace('..', '', $request->get('file'));
     $file = __DIR__ . '/../static/' . $fileParam;
     if (!$fileParam || !file_exists($file)) {
         return $this->getNotFoundPage($app, $app['translator']->trans('crudlex.resourceNotFound'));
     }
     $mimeTypes = new MimeTypes();
     $mimeType = $mimeTypes->getMimeType($file);
     $size = filesize($file);
     $streamedFileResponse = new StreamedFileResponse();
     $response = new StreamedResponse($streamedFileResponse->getStreamedFileFunction($file), 200, ['Content-Type' => $mimeType, 'Content-Disposition' => 'attachment; filename="' . basename($file) . '"', 'Content-length' => $size]);
     $response->setETag(filemtime($file))->setPublic()->isNotModified($request);
     $response->send();
     return $response;
 }
 /**
  * {@inheritdoc}
  */
 public function renderFile(Entity $entity, $entityName, $field)
 {
     $targetPath = $this->getPath($entityName, $entity, $field);
     $fileName = $entity->get($field);
     $file = $targetPath . '/' . $fileName;
     $response = new Response('');
     $mimeTypes = new MimeTypes();
     $mimeType = $mimeTypes->getMimeType($file);
     $size = filesize($file);
     if ($fileName && file_exists($file)) {
         $streamedFileResponse = new StreamedFileResponse();
         $response = new StreamedResponse($streamedFileResponse->getStreamedFileFunction($file), 200, ['Content-Type' => $mimeType, 'Content-Disposition' => 'attachment; filename="' . $fileName . '"', 'Content-length' => $size]);
         $response->send();
     }
     return $response;
 }