/** * @covers ::registerWithSymfonyGuesser * * @see Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser */ public function testSymfonyGuesserRegistration() { // Make the guessers property accessible on Symfony's MimeTypeGuesser. $symfony_guesser = SymfonyMimeTypeGuesser::getInstance(); // Test that the Drupal mime type guess is not being used before the // override method is called. It is possible that the test environment does // not support the default guessers. $guessers = $this->readAttribute($symfony_guesser, 'guessers'); if (count($guessers)) { $this->assertNotInstanceOf('Drupal\\Core\\File\\MimeType\\MimeTypeGuesser', $guessers[0]); } $container = new ContainerBuilder(); $container->set('file.mime_type.guesser', new MimeTypeGuesser(new StreamWrapperManager())); MimeTypeGuesser::registerWithSymfonyGuesser($container); $symfony_guesser = SymfonyMimeTypeGuesser::getInstance(); $guessers = $this->readAttribute($symfony_guesser, 'guessers'); $this->assertSame($container->get('file.mime_type.guesser'), $guessers[0]); $this->assertInstanceOf('Drupal\\Core\\File\\MimeType\\MimeTypeGuesser', $guessers[0]); $count = count($guessers); $container = new ContainerBuilder(); $container->set('file.mime_type.guesser', new MimeTypeGuesser(new StreamWrapperManager())); MimeTypeGuesser::registerWithSymfonyGuesser($container); $symfony_guesser = SymfonyMimeTypeGuesser::getInstance(); $guessers = $this->readAttribute($symfony_guesser, 'guessers'); $this->assertSame($container->get('file.mime_type.guesser'), $guessers[0]); $this->assertInstanceOf('Drupal\\Core\\File\\MimeType\\MimeTypeGuesser', $guessers[0]); $new_count = count($guessers); $this->assertEquals($count, $new_count, 'The count of mime type guessers remains the same after container re-init.'); }
/** * @dataProvider provideLoadCases */ public function testLoad($rootDir, $path) { $loader = new FileSystemLoader(MimeTypeGuesser::getInstance(), ExtensionGuesser::getInstance(), $rootDir); $binary = $loader->find($path); $this->assertInstanceOf('Liip\\ImagineBundle\\Model\\Binary', $binary); $this->assertStringStartsWith('text/', $binary->getMimeType()); }
public function testGuessExtensionIsBasedOnMimeType() { $file = new File(__DIR__ . '/Fixtures/test'); $guesser = $this->createMockGuesser($file->getPathname(), 'image/gif'); MimeTypeGuesser::getInstance()->register($guesser); $this->assertEquals('gif', $file->guessExtension()); }
/** * {@inheritDoc} */ public function load(ObjectManager $manager) { $user = $this->getReference("user/{$this->creator}"); $directory = $this->getReference("directory/{$this->directory}"); $resourceManager = $this->container->get('claroline.manager.resource_manager'); $filesDirectory = $this->container->getParameter('claroline.param.files_directory'); $ut = $this->container->get('claroline.utilities.misc'); $fileType = $manager->getRepository('ClarolineCoreBundle:Resource\\ResourceType')->findOneByName('file'); foreach ($this->files as $filePath) { $filePathParts = explode(DIRECTORY_SEPARATOR, $filePath); $fileName = array_pop($filePathParts); $extension = pathinfo($filePath, PATHINFO_EXTENSION); $hashName = "{$ut->generateGuid()}.{$extension}"; $targetFilePath = $filesDirectory . DIRECTORY_SEPARATOR . $hashName; $file = new File(); $file->setName($fileName); $file->setHashName($hashName); if (file_exists($filePath)) { copy($filePath, $targetFilePath); $file->setSize(filesize($filePath)); } else { touch($targetFilePath); $file->setSize(0); } $mimeType = MimeTypeGuesser::getInstance()->guess($targetFilePath); $file->setMimeType($mimeType); $resourceManager->create($file, $fileType, $user, $directory->getWorkspace(), $directory); $this->addReference("file/{$fileName}", $file); } $manager->flush(); }
/** * Get the uploaded file from a local path * like "/home/user/pictures/photo.png" * * @param string $path * @return UploadedFile */ protected function getUploadedFile($path) { $mimeTypeGuesser = MimeTypeGuesser::getInstance(); $copiedFile = $this->getFileCopy($path); $uploadedFile = new UploadedFile($copiedFile, $copiedFile, $mimeTypeGuesser->guess($copiedFile), filesize($copiedFile), null, true); return $uploadedFile; }
public function __construct(MimeTypeGuesserInterface $mimeTypeGuesser = null) { if (null === $mimeTypeGuesser && class_exists('Symfony\\Component\\HttpFoundation\\File\\MimeType\\MimeTypeGuesser')) { $mimeTypeGuesser = MimeTypeGuesser::getInstance(); } $this->mimeTypeGuesser = $mimeTypeGuesser; }
public function testConvertPdfAsPng() { $result = $this->pdfToPpm->convertPdf(dirname(__DIR__) . '/Resources/test_1_page.pdf', null, true); $mimeTypeGuesser = MimeTypeGuesser::getInstance(); $mimeType = $mimeTypeGuesser->guess($result->current()->getPathName()); $this->assertSame('image/png', $mimeType); }
/** * @return MimeTypeGuesser */ private function getMimeTypeGuesser() { if (!$this->mimeTypeGuesser) { $this->mimeTypeGuesser = MimeTypeGuesser::getInstance(); } return $this->mimeTypeGuesser; }
/** * {@inheritdoc} */ public function load(array $configs, ContainerBuilder $container) { $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); $loader->load('services.yml'); $mimeTypeGuesser = MimeTypeGuesser::getInstance(); $mimeTypeGuesser->register(new ImageTypeGuesser()); }
/** * @param AssetInterface $asset */ public function filterDump(AssetInterface $asset) { $sourceBase = $asset->getSourceRoot(); $sourcePath = $asset->getSourcePath(); $assetRoot = $this->assetRoot; if (null === $sourcePath) { return; } $content = $this->filterReferences($asset->getContent(), function ($matches) use($sourceBase, $sourcePath, $assetRoot) { // its not a relative path if (false !== strpos($matches['url'], '://') || 0 === strpos($matches['url'], '//') || 0 === strpos($matches['url'], 'data:') || isset($matches['url'][0]) && '/' == $matches['url'][0]) { return $matches[0]; } $url = $matches['url']; if (false !== ($pos = strpos($url, '?'))) { $url = substr($url, 0, $pos); } $sourceAsset = dirname($sourceBase . '/' . $sourcePath) . '/' . $url; if (!is_file($sourceAsset)) { return $matches[0]; } $mimeType = MimeTypeGuesser::getInstance()->guess($sourceAsset); $destRelativePath = substr($mimeType, 0, strpos($mimeType, '/')) . '/' . basename($url); $destAsset = $assetRoot . '/' . $destRelativePath; if (!is_dir(dirname($destAsset))) { mkdir(dirname($destAsset), 0777, true); } copy($sourceAsset, $destAsset); return str_replace($matches['url'], '../' . $destRelativePath, $matches[0]); }); $asset->setContent($content); }
public function __construct($path) { $mimeTypeGuesser = MimeTypeGuesser::getInstance(); $this->path = $path; $this->originalName = basename($path); $this->mimeType = $mimeTypeGuesser->guess($path); $this->size = filesize($path); }
public function testGuessWithNonReadablePath() { $path = __DIR__ . '/../Fixtures/to_delete'; touch($path); chmod($path, 0333); $this->setExpectedException('Symfony\\Component\\HttpFoundation\\File\\Exception\\AccessDeniedException'); MimeTypeGuesser::getInstance()->guess($path); }
private function guessFileMimeType($filePath) { $type = MimeTypeGuesser::getInstance()->guess($filePath); if (!isset($type)) { return DEFAULT_MIME_TYPE; } return $type; }
/** * Registers mime type guessers given the configuration */ public function register() { $guesser = MimeTypeGuesser::getInstance(); $guesser->register(new RawImageMimeTypeGuesser()); $guesser->register(new PostScriptMimeTypeGuesser()); $guesser->register(new AudioMimeTypeGuesser()); $guesser->register(new VideoMimeTypeGuesser()); $guesser->register(new CustomExtensionGuesser($this->conf->get(['border-manager', 'extension-mapping'], []))); }
/** * @inheritdoc */ public function bootstrap(Request $request) { LoggerRegistry::debug('FileModule::bootstrap()'); // Register the extension-based MIME type guesser which doesn't fail on CSS files. MimeTypeGuesser::getInstance()->register(new ExtensionMimeTypeGuesser($this->getEngine()->config('system.mime-types'))); // TODO files in preview mode $filename = $this->getEngine()->getSiteInfo()->getPublicPath(ResourceLocations::RESOURCE_LOCATION_SITE, $request->getPathInfo(), $this); return $this->getEngine()->createFileResponse($request, $filename); }
/** * @requires extension fileinfo */ public function testGuessExtensionWithReset() { $file = new File(__DIR__ . '/Fixtures/other-file.example'); $guesser = $this->createMockGuesser($file->getPathname(), 'image/gif'); MimeTypeGuesser::getInstance()->register($guesser); $this->assertEquals('gif', $file->guessExtension()); MimeTypeGuesser::reset(); $this->assertNull($file->guessExtension()); }
public function testGuessWithNonReadablePath() { if (strstr(PHP_OS, 'WIN')) { $this->markTestSkipped('Can not verify chmod operations on Windows'); } $path = __DIR__ . '/../Fixtures/to_delete'; touch($path); chmod($path, 0333); $this->setExpectedException('Symfony\\Component\\HttpFoundation\\File\\Exception\\AccessDeniedException'); MimeTypeGuesser::getInstance()->guess($path); }
/** * Constructs a new repository * * @param Filesystem $Filesystem * @param null $Config * @param null $Request * @param null $URL */ public function __construct(Filesystem $Filesystem, MediaPaths $MediaPaths, Images $Image, $Config = null, $Request = null, $URL = null) { $this->Filesystem = $Filesystem; $this->MediaPaths = $MediaPaths; $this->config = $Config ?: \Config::get('devise.media-manager'); $this->Request = $Request ?: \Request::getFacadeRoot(); $this->URL = $URL ?: \URL::getFacadeRoot(); $this->guesser = MimeTypeGuesser::getInstance(); $this->basepath = public_path() . '/' . $this->config['root-dir'] . '/'; $this->Image = $Image; }
/** * @param $url * @return UploadedFile */ public function get($url) { $this->prepareDirectory(); $fileAttributes = pathinfo($url); $filename = $this->tempDir . $fileAttributes['basename']; file_put_contents($filename, fopen($url, 'r')); $this->registerDeleteFileEvent($filename); $mimetypeGuesses = MimeTypeGuesser::getInstance(); $mimetype = $mimetypeGuesses->guess($filename); return new UploadedFile($filename, $fileAttributes['basename'], $mimetype); }
public function setUp() { if (!static::$registered) { $guesser = MimeTypeGuesser::getInstance(); $guesser->register(new FileBinaryMimeTypeGuesser()); $guesser->register(new RawImageMimeTypeGuesser()); $guesser->register(new PostScriptMimeTypeGuesser()); $guesser->register(new AudioMimeTypeGuesser()); $guesser->register(new VideoMimeTypeGuesser()); static::$registered = true; } }
/** * Guesses the mimetype by the files extension ... or default * @param string $filename * @param string $default * @return string */ public static function guessMimeType($filename, $default = 'application/octed-stream') { $guesser = new FileExtMimeTypeGuesser(); $mime = $guesser->guess($filename); if (null === $mime) { try { $mime = MimeTypeGuesser::getInstance()->guess($filename); } catch (FileNotFoundException $ex) { // dont care } } return null === $mime ? $default : $mime; }
public function getMediaVorus() { static $initialized; if (null === $initialized) { $guesser = MimeTypeGuesser::getInstance(); $guesser->register(new AudioMimeTypeGuesser()); $guesser->register(new PostScriptMimeTypeGuesser()); $guesser->register(new RawImageMimeTypeGuesser()); $guesser->register(new VideoMimeTypeGuesser()); $initialized = true; } return new MediaVorus($this->getReader(), $this->getWriter(), $this->getProbe()); }
public function getImage($imageId) { $productImage = ProductImageQuery::create()->findPk($imageId); if ($productImage === null) { throw new FileNotFoundException(Translator::getInstance()->trans("The image id %id doesn't exist", ["%id" => $imageId], ShoppingFlux::MESSAGE_DOMAIN)); } $path = THELIA_LOCAL_DIR . "/media/images/product/" . $productImage->getFile(); if (!is_file($path) || !is_readable($path)) { throw new \ErrorException(Translator::getInstance()->trans("The file %file is not readable", ["%file" => $productImage->getFile()], ShoppingFlux::MESSAGE_DOMAIN)); } $data = file_get_contents($path); $mime = MimeTypeGuesser::getInstance()->guess($path); return new Response($data, 200, ["Content-Type" => $mime]); }
public function testGuessWithNonReadablePath() { if (strstr(PHP_OS, 'WIN')) { $this->markTestSkipped('Can not verify chmod operations on Windows'); } $path = __DIR__ . '/../Fixtures/to_delete'; touch($path); chmod($path, 0333); if (get_current_user() != 'root' && substr(sprintf('%o', fileperms($path)), -4) == '0333') { $this->setExpectedException('Symfony\\Component\\HttpFoundation\\File\\Exception\\AccessDeniedException'); MimeTypeGuesser::getInstance()->guess($path); } else { $this->markTestSkipped('Can not verify chmod operations, change of file permissions failed'); } }
/** * @param FileInterface $storageFile * @param string $filePath * @param string $fileName */ public function createFromFile(FileInterface $storageFile, $filePath, $fileName = null) { $fileSystem = new Filesystem(); $mimeTypeGuesser = MimeTypeGuesser::getInstance(); $storageFile->setFileName($fileName ?: basename($filePath)); $storageFile->setMimeType($mimeTypeGuesser->guess($filePath)); $storageFile->setSize(filesize($filePath)); if (!$storageFile->getId()) { $this->save($storageFile); } else { $fileSystem->remove($this->getFilePath($storageFile)); } $storageFile->setStorageId(uniqid($storageFile->getId() . '_', false)); $fileSystem->copy($filePath, $this->getFilePath($storageFile)); $this->save($storageFile); }
public function guess(\SplFileInfo $file) { // Get the mimetype $guesser = MimeTypeGuesser::getInstance(); $mimetype = $guesser->guess($file->getPathname()); if ($this->_isBannedMimetype($mimetype) || $this->_isBannedFiletype($file)) { throw new Exception\BannedType(sprintf('`%s` is not an allowed file type', $file->getBasename()), $file); } $mappings = $this->_getMappings(); foreach ($mappings as $typeID => $mimetypes) { if (in_array($mimetype, $mimetypes)) { return $typeID; } } return self::OTHER; }
public function testGuessWithNonReadablePath() { if (defined('PHP_WINDOWS_VERSION_BUILD')) { $this->markTestSkipped('Can not verify chmod operations on Windows'); } if (in_array(get_current_user(), array('root'))) { $this->markTestSkipped('This test will fail if run under superuser'); } $path = __DIR__ . '/../Fixtures/to_delete'; touch($path); @chmod($path, 0333); if (get_current_user() != 'root' && substr(sprintf('%o', fileperms($path)), -4) == '0333') { $this->setExpectedException('Symfony\\Component\\HttpFoundation\\File\\Exception\\AccessDeniedException'); MimeTypeGuesser::getInstance()->guess($path); } else { $this->markTestSkipped('Can not verify chmod operations, change of file permissions failed'); } }
public function testGuessWithNonReadablePath() { if ('\\' === DIRECTORY_SEPARATOR) { $this->markTestSkipped('Can not verify chmod operations on Windows'); } if (!getenv('USER') || 'root' === getenv('USER')) { $this->markTestSkipped('This test will fail if run under superuser'); } $path = __DIR__ . '/../Fixtures/to_delete'; touch($path); @chmod($path, 0333); if (substr(sprintf('%o', fileperms($path)), -4) == '0333') { $this->setExpectedException('Symfony\\Component\\HttpFoundation\\File\\Exception\\AccessDeniedException'); MimeTypeGuesser::getInstance()->guess($path); } else { $this->markTestSkipped('Can not verify chmod operations, change of file permissions failed'); } }
function __construct(Request $request, $filename, $force_download = false) { if (!is_file($filename) || !is_readable($filename)) { throw new \Hydra\Exception\NotFoundHttpException("File not found: {$filename}"); } $this->isPhp = preg_match('/\\.php$/', $filename); $this->filename = $filename; if ($force_download) { $this->headers['Content-Type'] = 'application/octet-stream'; } else { // Try to guess Content-Type if (!$this->isPhp) { $this->headers['Content-Type'] = MimeTypeGuesser::getInstance()->guess($filename); } elseif ($request->app->config->response['guessPhpContentType']) { $ext = Utils::fileExt(substr($filename, 0, -4)); if (isset($request->app->mimetypes[$ext])) { $this->headers['Content-Type'] = $request->app->mimetypes[$ext]; } } } if (!$this->isPhp) { $this->headers['Content-Length'] = filesize($filename); } $response = $this; parent::__construct($request, function () use($response) { // We don't want any left-over output when sending a file. ob_end_clean(); if ($response->isPhp) { $request = $response->request; $app = $response->app; require $response->filename; } else { if ($response->app->config->response['XSendfile']) { $filename = realpath($response->filename); header("X-Sendfile: {$filename}"); } else { set_time_limit(0); //Set the execution time to infinite. readfile($response->filename); } } }); }
/** * {@inheritdoc} */ public function createFromRawFile(\SplFileInfo $rawFile, $destFsAlias) { $pathInfo = $this->pathGenerator->generate($rawFile); $sha1 = sha1_file($rawFile->getPathname()); if ($rawFile instanceof UploadedFile) { $originalFilename = $rawFile->getClientOriginalName(); $extension = $rawFile->getClientOriginalExtension(); } else { $originalFilename = $rawFile->getFilename(); $extension = $rawFile->getExtension(); } $size = filesize($rawFile->getPathname()); $mimeType = MimeTypeGuesser::getInstance()->guess($rawFile->getPathname()); $file = new $this->fileClass(); $file->setKey($pathInfo['path'] . $pathInfo['file_name']); $file->setMimeType($mimeType); $file->setOriginalFilename($originalFilename); $file->setSize($size); $file->setExtension($extension); $file->setHash($sha1); $file->setStorage($destFsAlias); return $file; }