/** * @param Request $request * @param string $filename * * @throws NotFoundHttpException If media is not found * * @return Response */ public function showAction(Request $request, $filename) { if (!$this->filesystem->has($filename)) { throw new NotFoundHttpException(sprintf('Media "%s" not found', $filename)); } $response = new Response($content = $this->filesystem->read($filename)); $mime = $this->filesystem->mimeType($filename); if (($filter = $request->query->get('filter')) && null !== $mime && 0 === strpos($mime, 'image')) { try { $cachePath = $this->cacheManager->resolve($request, $filename, $filter); if ($cachePath instanceof Response) { $response = $cachePath; } else { $image = $this->imagine->load($content); $response = $this->filterManager->get($request, $filter, $image, $filename); $response = $this->cacheManager->store($response, $cachePath, $filter); } } catch (\RuntimeException $e) { if (0 === strpos($e->getMessage(), 'Filter not defined')) { throw new HttpException(404, sprintf('The filter "%s" cannot be found', $filter), $e); } throw $e; } } if ($mime) { $response->headers->set('Content-Type', $mime); } return $response; }
/** * {@inheritDoc} */ public function load($path) { try { $contents = $this->filesystem->read($path); } catch (FileNotFoundException $e) { throw new Exception\ImageNotFoundException(sprintf('Source image not found in "%s"', $path)); } try { $mimeType = $this->filesystem->mimeType($path); } catch (\LogicException $e) { // Mime Type could not be detected return $contents; } if (!$mimeType) { // Mime Type could not be detected return $contents; } return new Binary($contents, $mimeType); }
/** * Create a media and load file information * * @param string $filename * @param bool $isUploaded * * @throws \InvalidArgumentException When file does not exist * * @return ProductMediaInterface */ public function createFromFilename($filename, $isUploaded = true) { if ($isUploaded) { $filePath = $this->uploadDirectory . DIRECTORY_SEPARATOR . $filename; } else { $filePath = $filename; } if ($isUploaded && !$this->filesystem->has($filename)) { throw new \InvalidArgumentException(sprintf('File "%s" does not exist', $filePath)); } $media = $this->factory->createMedia(); $media->setOriginalFilename(basename($filename)); $media->setFilename($filename); if (!$isUploaded) { $media->setFile(new File($filename)); } else { $media->setMimeType($this->filesystem->mimeType($filename)); } return $media; }