You are here

protected function WebformManagedFileBase::getFileDestinationUri in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/WebformManagedFileBase.php \Drupal\webform\Plugin\WebformElement\WebformManagedFileBase::getFileDestinationUri()

Determine the destination URI where to save an uploaded file.

Parameters

array $element: Element whose destination URI is requested.

\Drupal\file\FileInterface $file: File whose destination URI is requested.

\Drupal\webform\WebformSubmissionInterface $webform_submission: Webform submission that contains the file whose destination URI is requested.

Return value

string Destination URI under which the file should be saved.

1 call to WebformManagedFileBase::getFileDestinationUri()
WebformManagedFileBase::addFiles in src/Plugin/WebformElement/WebformManagedFileBase.php
Add a webform submission file's usage and mark it as permanent.

File

src/Plugin/WebformElement/WebformManagedFileBase.php, line 1238

Class

WebformManagedFileBase
Provides a base class webform 'managed_file' elements.

Namespace

Drupal\webform\Plugin\WebformElement

Code

protected function getFileDestinationUri(array $element, FileInterface $file, WebformSubmissionInterface $webform_submission) {
  $destination_folder = $this->fileSystem
    ->dirname($file
    ->getFileUri());
  $destination_filename = $file
    ->getFilename();
  $destination_extension = pathinfo($destination_filename, PATHINFO_EXTENSION);
  $destination_basename = substr(pathinfo($destination_filename, PATHINFO_BASENAME), 0, -strlen(".{$destination_extension}"));

  // Replace /_sid_/ token with the submission id.
  if (strpos($destination_folder, '/_sid_')) {
    $destination_folder = str_replace('/_sid_', '/' . $webform_submission
      ->id(), $destination_folder);
    $this->fileSystem
      ->prepareDirectory($destination_folder, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
  }

  // Replace tokens in file name.
  if (isset($element['#file_name']) && $element['#file_name']) {
    $destination_basename = $this->tokenManager
      ->replace($element['#file_name'], $webform_submission);
  }

  // Sanitize filename.
  // @see http://stackoverflow.com/questions/2021624/string-sanitizer-for-filename
  // @see \Drupal\webform_attachment\Element\WebformAttachmentBase::getFileName
  if (!empty($element['#sanitize'])) {
    $destination_extension = mb_strtolower($destination_extension);
    $destination_basename = mb_strtolower($destination_basename);
    $destination_basename = $this->transliteration
      ->transliterate($destination_basename, $this->languageManager
      ->getCurrentLanguage()
      ->getId(), '-');
    $destination_basename = preg_replace('([^\\w\\s\\d\\-_~,;:\\[\\]\\(\\].]|[\\.]{2,})', '', $destination_basename);
    $destination_basename = preg_replace('/\\s+/', '-', $destination_basename);
    $destination_basename = trim($destination_basename, '-');

    // If the basename is empty use the element's key, composite key, or type.
    if (empty($destination_basename)) {
      if (isset($element['#webform_key'])) {
        $destination_basename = $element['#webform_key'];
      }
      elseif (isset($element['#webform_composite_key'])) {
        $destination_basename = $element['#webform_composite_key'];
      }
      else {
        $destination_basename = $element['#type'];
      }
    }
  }

  // Make sure $destination_uri does not exceed 250 + _01 character limit for
  // the 'file_managed' table uri column.
  // @see file_validate_name_length()
  // @see https://drupal.stackexchange.com/questions/36760/overcoming-255-character-uri-limit-for-files-managed
  $filename_maxlength = 250;

  // Subtract the destination's folder length.
  $filename_maxlength -= mb_strlen($destination_folder);

  // Subtract the destination's extension length.
  $filename_maxlength -= mb_strlen($destination_extension);

  // Subtract the directory's forward slash and the extension's period.
  $filename_maxlength -= 2;

  // Truncate the base name.
  $destination_basename = mb_strimwidth($destination_basename, 0, $filename_maxlength);
  return $destination_folder . '/' . $destination_basename . '.' . $destination_extension;
}