You are here

class S3fsPathProcessorImageStyles in S3 File System 8.3

Same name and namespace in other branches
  1. 4.0.x src/PathProcessor/S3fsPathProcessorImageStyles.php \Drupal\s3fs\PathProcessor\S3fsPathProcessorImageStyles

Defines a path processor to rewrite image styles URLs.

As the route system does not allow arbitrary amount of parameters convert the file path to a query parameter on the request.

This processor handles Amazon S3 public image style callback:

  • In order to allow the webserver to serve these files with dynamic args the route is registered under /s3/files/styles prefix and change internally to pass validation and move the file to query parameter. This file will be processed in S3fsImageStyleDownloadController::deliver().

Private files use the normal private file workflow.

Hierarchy

Expanded class hierarchy of S3fsPathProcessorImageStyles

See also

\Drupal\s3fs\Controller\S3fsImageStyleDownloadController::deliver()

\Drupal\image\Controller\ImageStyleDownloadController::deliver()

\Drupal\image\PathProcessor\PathProcessorImageStyles::processInbound()

1 string reference to 'S3fsPathProcessorImageStyles'
s3fs.services.yml in ./s3fs.services.yml
s3fs.services.yml
1 service uses S3fsPathProcessorImageStyles
s3fs.path_processor.image_styles in ./s3fs.services.yml
Drupal\s3fs\PathProcessor\S3fsPathProcessorImageStyles

File

src/PathProcessor/S3fsPathProcessorImageStyles.php, line 26

Namespace

Drupal\s3fs\PathProcessor
View source
class S3fsPathProcessorImageStyles implements InboundPathProcessorInterface {
  const IMAGE_STYLE_PATH_PREFIX = '/s3/files/styles/';

  /**
   * {@inheritdoc}
   */
  public function processInbound($path, Request $request) {
    if ($this
      ->isImageStylePath($path)) {

      // Strip out path prefix.
      $rest = preg_replace('|^' . preg_quote(static::IMAGE_STYLE_PATH_PREFIX, '|') . '|', '', $path);

      // Get the image style, scheme and path.
      if (substr_count($rest, '/') >= 2) {
        list($image_style, $scheme, $file) = explode('/', $rest, 3);
        if ($this
          ->isValidScheme($scheme)) {

          // Set the file as query parameter.
          $request->query
            ->set('file', $file);
          $path = static::IMAGE_STYLE_PATH_PREFIX . $image_style . '/' . $scheme;
        }
      }
    }
    return $path;
  }

  /**
   * Check if the path is a s3 image style path.
   *
   * @param string $path
   *   Path to be checked.
   *
   * @return bool
   *   TRUE if path starts with s3fs image style prefix, FALSE otherwise.
   */
  private function isImageStylePath($path) {
    return strpos($path, static::IMAGE_STYLE_PATH_PREFIX) === 0;
  }

  /**
   * Check if scheme is Amazon S3 image style supported.
   *
   * @param string $scheme
   *   Scheme (ex 's3' for s3://) to test.
   *
   * @return bool
   *   TRUE if s3fs will generate image styles, FALSE otherwise.
   */
  private function isValidScheme($scheme) {
    return in_array($scheme, [
      'public',
      's3',
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
S3fsPathProcessorImageStyles::IMAGE_STYLE_PATH_PREFIX constant
S3fsPathProcessorImageStyles::isImageStylePath private function Check if the path is a s3 image style path.
S3fsPathProcessorImageStyles::isValidScheme private function Check if scheme is Amazon S3 image style supported.
S3fsPathProcessorImageStyles::processInbound public function Processes the inbound path. Overrides InboundPathProcessorInterface::processInbound