public function UploadHandler::getFilename in DropzoneJS 8
Same name and namespace in other branches
- 8.2 src/UploadHandler.php \Drupal\dropzonejs\UploadHandler::getFilename()
Reads, checks and return filename of a file being uploaded.
Parameters
\Symfony\Component\HttpFoundation\File\UploadedFile $file: An instance of UploadedFile.
Return value
string The sanitized filename.
Throws
\Drupal\dropzonejs\UploadException
Overrides UploadHandlerInterface::getFilename
1 call to UploadHandler::getFilename()
- UploadHandler::handleUpload in src/
UploadHandler.php - Handles an uploaded file.
File
- src/
UploadHandler.php, line 76
Class
- UploadHandler
- Handles files uploaded by Dropzone.
Namespace
Drupal\dropzonejsCode
public function getFilename(UploadedFile $file) {
$original_name = $file
->getClientOriginalName();
// There should be a filename and it should not contain a semicolon,
// which we use to separate filenames.
if (!isset($original_name)) {
throw new UploadException(UploadException::FILENAME_ERROR);
}
// @todo The following filename sanitization steps replicate the behaviour
// of the 2492171-28 patch for https://www.drupal.org/node/2492171.
// Try to reuse that code instead, once that issue is committed.
// Transliterate.
$langcode = $this->languageManager
->getCurrentLanguage()
->getId();
$filename = $this->transliteration
->transliterate($original_name, $langcode, '');
// Replace whitespace.
$filename = str_replace(' ', '_', $filename);
// Remove remaining unsafe characters.
$filename = preg_replace('![^0-9A-Za-z_.-]!', '', $filename);
// Remove multiple consecutive non-alphabetical characters.
$filename = preg_replace('/(_)_+|(\\.)\\.+|(-)-+/', '\\1\\2\\3', $filename);
// Force lowercase to prevent issues on case-insensitive file systems.
$filename = Unicode::strtolower($filename);
// For security reasons append the txt extension. It will be removed in
// Drupal\dropzonejs\Element::valueCallback when we will know the valid
// extension and we will be able to properly sanitize the filename.
$processed_filename = $filename . '.txt';
return $processed_filename;
}