You are here

public function AjaxController::getKey in S3 File System CORS Upload 8

Return the file key (i.e. the path and name).

The values $file_size and $file_index are just values to be passed through and returned to the javaScript function.

1 string reference to 'AjaxController::getKey'
s3fs_cors.routing.yml in ./s3fs_cors.routing.yml
s3fs_cors.routing.yml

File

src/Controller/AjaxController.php, line 90

Class

AjaxController
Default controller for the s3fs_cors module.

Namespace

Drupal\s3fs_cors\Controller

Code

public function getKey($directory, $file_name, $file_size, $file_index, $replace = FileSystemInterface::EXISTS_RENAME) {

  // Strip control characters (ASCII value < 32). Though these are allowed in
  // some filesystems, not many applications handle them well.
  $file_name = preg_replace('/[\\x00-\\x1F]/u', '_', $file_name);

  // Also replace forbidden chars if this is a Windows envrionment.
  if (substr(PHP_OS, 0, 3) == 'WIN') {

    // These characters are not allowed in Windows filenames.
    $file_name = str_replace([
      ':',
      '*',
      '?',
      '"',
      '<',
      '>',
      '|',
    ], '_', $file_name);
  }

  // Decode the "/" chars in the directory and build an initial file key.
  // Note: this will include the s3fs root folder, if specified.
  $directory = str_replace('::', '/', $directory);
  $file_key = $directory . '/' . $file_name;

  // Check if a file with this key already exists on S3.
  $file_exists = $this
    ->s3FileExists($file_key);
  if ($file_exists) {
    switch ($replace) {
      case FileSystemInterface::EXISTS_REPLACE:

        // Do nothing here, we want to overwrite the existing file.
        break;
      case FileSystemInterface::EXISTS_RENAME:
        $file_key = $this
          ->createFileKey($directory, $file_name);
        break;
      case FileSystemInterface::EXISTS_ERROR:

        // Error reporting handled by calling function.
        return FALSE;
    }
  }

  // Core file_destination is not able to check remoe file existience.
  return new JsonResponse([
    'file_key' => $file_key,
    'file_name' => $file_name,
    'file_size' => $file_size,
    'file_index' => $file_index,
  ]);
}