You are here

class FlysystemS3FileSystem in Flysystem - S3 8

Same name and namespace in other branches
  1. 2.0.x src/File/FlysystemS3FileSystem.php \Drupal\flysystem_s3\File\FlysystemS3FileSystem

Decorates the Drupal FileSystem service to handle chmod() for S3.

Hierarchy

Expanded class hierarchy of FlysystemS3FileSystem

1 string reference to 'FlysystemS3FileSystem'
flysystem_s3.services.yml in ./flysystem_s3.services.yml
flysystem_s3.services.yml
1 service uses FlysystemS3FileSystem
flysystem_s3.file_system in ./flysystem_s3.services.yml
\Drupal\flysystem_s3\File\FlysystemS3FileSystem

File

src/File/FlysystemS3FileSystem.php, line 13

Namespace

Drupal\flysystem_s3\File
View source
class FlysystemS3FileSystem extends FileSystem {

  /**
   * FlysystemS3FileSystem constructor.
   *
   * @param \Drupal\Core\File\FileSystemInterface $file_system
   *   The file system being decorated.
   * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
   *   The stream wrapper manager.
   * @param \Drupal\Core\Site\Settings $settings
   *   The site settings.
   * @param \Psr\Log\LoggerInterface $logger
   *   The file logger channel.
   */
  public function __construct(StreamWrapperManagerInterface $stream_wrapper_manager, Settings $settings, LoggerInterface $logger) {
    parent::__construct($stream_wrapper_manager, $settings, $logger);
  }

  /**
   * {@inheritdoc}
   *
   * Extend chmod(), respecting S3's ACL setting.
   *
   * With Drupal's private files, chmod() is called by file_save_upload() to
   * ensure the new file is readable by the file server, using the same file
   * system permissions as the public file system. However, since private files
   * are stored outside of the docroot, they are forced to go be accessed
   * through Drupal's file permissions handling.
   *
   * With S3, \Twistor\FlysystemStreamWrapper::stream_metadata() automatically
   * maps chmod() calls to basic S3 ACLs, which means that while a file can be
   * initially uploaded as 'private', Drupal will immediately chmod it to
   * public using the default file mask in settings.php.
   *
   * This method checks to see if we are using a private S3 scheme, and if so,
   * ensures that group / other permissions are always unset, ensuring the
   * stream wrapper preserves private permissions.
   *
   * @param string $uri
   *   A string containing a URI file, or directory path.
   * @param int $mode
   *   Integer value for the permissions. Consult PHP chmod() documentation for
   *   more information.
   *
   * @return bool
   *   TRUE for success, FALSE in the event of an error.
   *
   * @see \Twistor\FlysystemStreamWrapper::stream_metadata
   */
  public function chmod($uri, $mode = NULL) {
    $scheme = parent::uriScheme($uri);
    if ($this
      ->isPrivateS3Scheme($scheme)) {
      is_dir($uri) ? $mode = 0700 : ($mode = 0600);
    }
    return parent::chmod($uri, $mode);
  }

  /**
   * Return if a scheme is a private S3 scheme.
   *
   * @param string $scheme
   *   The scheme to check.
   *
   * @return bool
   *   TRUE if the scheme's S3 acl is set to 'private'.
   */
  protected function isPrivateS3Scheme($scheme) {
    $settings = $this->settings
      ->get('flysystem', []);
    return isset($settings[$scheme]) && $settings[$scheme]['driver'] == 's3' && isset($settings[$scheme]['config']['options']['ACL']) && $settings[$scheme]['config']['options']['ACL'] == 'private';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FileSystem::$logger protected property The file logger channel.
FileSystem::$settings protected property The site settings.
FileSystem::$streamWrapperManager protected property The stream wrapper manager.
FileSystem::basename public function Gets the filename from a given path. Overrides FileSystemInterface::basename
FileSystem::CHMOD_DIRECTORY constant Default mode for new directories. See self::chmod().
FileSystem::CHMOD_FILE constant Default mode for new files. See self::chmod().
FileSystem::copy public function Copies a file to a new location without invoking the file API. Overrides FileSystemInterface::copy
FileSystem::createFilename public function Creates a full file path from a directory and filename. Overrides FileSystemInterface::createFilename
FileSystem::delete public function Deletes a file without database changes or hook invocations. Overrides FileSystemInterface::delete
FileSystem::deleteRecursive public function Deletes all files and directories in the specified filepath recursively. Overrides FileSystemInterface::deleteRecursive
FileSystem::dirname public function Gets the name of the directory from a given path. Overrides FileSystemInterface::dirname
FileSystem::doScanDirectory protected function Internal function to handle directory scanning with recursion.
FileSystem::getDestinationFilename public function Determines the destination path for a file. Overrides FileSystemInterface::getDestinationFilename
FileSystem::getTempDirectory public function Gets the path of the configured temporary directory. Overrides FileSystemInterface::getTempDirectory
FileSystem::mkdir public function Creates a directory, optionally creating missing components in the path to the directory. Overrides FileSystemInterface::mkdir
FileSystem::mkdirCall protected function Helper function. Ensures we don't pass a NULL as a context resource to mkdir().
FileSystem::move public function Moves a file to a new location without database changes or hook invocation. Overrides FileSystemInterface::move
FileSystem::moveUploadedFile public function Moves an uploaded file to a new location. Overrides FileSystemInterface::moveUploadedFile
FileSystem::prepareDestination protected function Prepares the destination for a file copy or move operation.
FileSystem::prepareDirectory public function Checks that the directory exists and is writable. Overrides FileSystemInterface::prepareDirectory
FileSystem::realpath public function Resolves the absolute filepath of a local URI or filepath. Overrides FileSystemInterface::realpath
FileSystem::rmdir public function Removes a directory. Overrides FileSystemInterface::rmdir
FileSystem::saveData public function Saves a file to the specified destination without invoking file API. Overrides FileSystemInterface::saveData
FileSystem::scanDirectory public function Finds all files that match a given mask in a given directory. Overrides FileSystemInterface::scanDirectory
FileSystem::tempnam public function Creates a file with a unique filename in the specified directory. Overrides FileSystemInterface::tempnam
FileSystem::unlink public function Deletes a file. Overrides FileSystemInterface::unlink
FileSystem::uriScheme public function Returns the scheme of a URI (e.g. a stream). Overrides FileSystemInterface::uriScheme
FileSystem::validScheme public function Checks that the scheme of a stream URI is valid. Overrides FileSystemInterface::validScheme
FileSystemInterface::CREATE_DIRECTORY constant Flag used by ::prepareDirectory() -- create directory if not present.
FileSystemInterface::EXISTS_ERROR constant Flag for dealing with existing files: Do nothing and return FALSE.
FileSystemInterface::EXISTS_RENAME constant Flag for dealing with existing files: Appends number until name is unique.
FileSystemInterface::EXISTS_REPLACE constant Flag for dealing with existing files: Replace the existing file.
FileSystemInterface::MODIFY_PERMISSIONS constant Flag used by ::prepareDirectory() -- file permissions may be changed.
FlysystemS3FileSystem::chmod public function Extend chmod(), respecting S3's ACL setting. Overrides FileSystem::chmod
FlysystemS3FileSystem::isPrivateS3Scheme protected function Return if a scheme is a private S3 scheme.
FlysystemS3FileSystem::__construct public function FlysystemS3FileSystem constructor. Overrides FileSystem::__construct