View source
<?php
namespace Drupal\textimage\Controller;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Image\ImageFactory;
use Drupal\image\ImageStyleInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\system\FileDownloadController;
use Drupal\textimage\TextimageFactory;
use Drupal\textimage\TextimageException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Psr\Log\LoggerInterface;
class TextimageDownloadController extends FileDownloadController implements ContainerInjectionInterface {
protected $textimageFactory;
protected $imageFactory;
protected $configFactory;
protected $logger;
protected $fileSystem;
public function __construct(TextimageFactory $textimage_factory, ImageFactory $image_factory, ConfigFactoryInterface $config_factory, LoggerInterface $logger, FileSystemInterface $file_system, StreamWrapperManagerInterface $stream_wrapper_manager) {
parent::__construct($stream_wrapper_manager);
$this->textimageFactory = $textimage_factory;
$this->imageFactory = $image_factory;
$this->configFactory = $config_factory;
$this->logger = $logger;
$this->fileSystem = $file_system;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('textimage.factory'), $container
->get('image.factory'), $container
->get('config.factory'), $container
->get('logger.channel.textimage'), $container
->get('file_system'), $container
->get('stream_wrapper_manager'));
}
public function urlDeliver(Request $request, ImageStyleInterface $image_style) {
if (!$this->configFactory
->get('textimage.settings')
->get('url_generation.enabled')) {
throw new AccessDeniedHttpException('Textimage URL generation is not enabled on this site');
}
if (!$this->textimageFactory
->isTextimage($image_style)) {
$this->logger
->error("URL generation - The image style '%style_name' is not relevant for Textimage.", [
'%style_name' => $image_style
->getName(),
]);
throw new NotFoundHttpException("The image style requested is not relevant for Textimage");
}
if ($image_style
->getThirdPartySetting('textimage', 'uri_scheme', $this->configFactory
->get('system.file')
->get('default_scheme')) !== 'public') {
$this->logger
->error("URL generation - The image style '%style_name' is not set to produce image files for the 'public' file scheme -> disabled.", [
'%style_name' => $image_style
->getName(),
]);
throw new AccessDeniedHttpException("The image style requested is not set to produce image files for the 'public' file scheme");
}
$text_string = $request->query
->get('text');
$text = explode($this->configFactory
->get('textimage.settings')
->get('url_generation.text_separator'), $text_string);
$last_text = array_pop($text);
$extension = pathinfo($last_text, PATHINFO_EXTENSION);
if ($extension) {
$text[] = str_replace('.' . $extension, '', pathinfo($last_text, PATHINFO_BASENAME));
}
else {
$this->logger
->error("URL generation - No file extension specified.");
throw new NotFoundHttpException('No file extension specified');
}
$file_uri = 'public://textimage/' . $image_style
->id() . '/' . $text_string;
try {
$image_uri = $this->textimageFactory
->get()
->setStyle($image_style)
->setTargetUri($file_uri)
->process($text)
->buildImage()
->getUri();
return $this
->returnBinary($request, $image_uri);
} catch (TextimageException $e) {
$this->logger
->error("URL generation - Failed to build an image at '%file_uri'.", [
'%file_uri' => $file_uri,
]);
throw new NotFoundHttpException('Image not found');
}
}
public function deferredDelivery(Request $request) {
$file = $request->query
->get('file');
$tiid = str_replace('.' . pathinfo($file, PATHINFO_EXTENSION), '', pathinfo($file, PATHINFO_BASENAME));
try {
$image_uri = $this->textimageFactory
->load($tiid)
->buildImage()
->getUri();
return $this
->returnBinary($request, $image_uri);
} catch (TextimageException $e) {
$this->logger
->error("Failed to build an image at '%file_uri'.", [
'%file_uri' => $file,
]);
throw new NotFoundHttpException('Image not found');
}
}
protected function returnBinary(Request $request, $uri) {
if (!file_exists($uri)) {
$this->logger
->notice("Textimage image at '%source_image_path' not found.", [
'%source_image_path' => $uri,
]);
return new Response($this
->t('Error downloading a textimage.'), 404);
}
if (($scheme = $this->streamWrapperManager
->getScheme($uri)) == 'private') {
$request->query
->set('file', $this->streamWrapperManager
->getTarget($uri));
return parent::download($request, $scheme);
}
else {
$image = $this->imageFactory
->get($uri);
$uri = $image
->getSource();
$headers = [
'Content-Type' => $image
->getMimeType(),
'Content-Length' => $image
->getFileSize(),
];
return new BinaryFileResponse($uri, 200, $headers);
}
}
}