View source
<?php
namespace Drupal\dropzonejs;
use Drupal\Component\Render\PlainTextOutput;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Utility\Token;
use Drupal\file\FileInterface;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface;
use Drupal\Core\File\FileSystemInterface;
class DropzoneJsUploadSave implements DropzoneJsUploadSaveInterface {
use StringTranslationTrait;
protected $entityTypeManager;
protected $mimeTypeGuesser;
protected $fileSystem;
protected $logger;
protected $renderer;
protected $configFactory;
protected $token;
public function __construct(EntityTypeManagerInterface $entity_type_manager, MimeTypeGuesserInterface $mimetype_guesser, FileSystemInterface $file_system, LoggerChannelFactoryInterface $logger_factory, RendererInterface $renderer, ConfigFactoryInterface $config_factory, Token $token) {
$this->entityTypeManager = $entity_type_manager;
$this->mimeTypeGuesser = $mimetype_guesser;
$this->fileSystem = $file_system;
$this->logger = $logger_factory
->get('dropzonejs');
$this->renderer = $renderer;
$this->configFactory = $config_factory;
$this->token = $token;
}
public function createFile($uri, $destination, $extensions, AccountProxyInterface $user, array $validators = []) {
$uri = file_stream_wrapper_uri_normalize($uri);
$file_info = new \SplFileInfo($uri);
$file = $this->entityTypeManager
->getStorage('file')
->create([
'uid' => $user
->id(),
'status' => 0,
'filename' => $file_info
->getFilename(),
'uri' => $uri,
'filesize' => $file_info
->getSize(),
'filemime' => $this->mimeTypeGuesser
->guess($uri),
]);
$destination = PlainTextOutput::renderFromHtml($this->token
->replace($destination));
$renamed = $this
->renameExecutableExtensions($file);
if ($renamed && !empty($extensions)) {
$extensions .= ' txt';
drupal_set_message($this
->t('For security reasons, your upload has been renamed to %filename.', [
'%filename' => $file
->getFilename(),
]));
}
$errors = $this
->validateFile($file, $extensions, $validators);
if (!empty($errors)) {
$message = [
'error' => [
'#markup' => $this
->t('The specified file %name could not be uploaded.', [
'%name' => $file
->getFilename(),
]),
],
'item_list' => [
'#theme' => 'item_list',
'#items' => $errors,
],
];
drupal_set_message($this->renderer
->renderPlain($message), 'error');
return FALSE;
}
if (!$this
->prepareDestination($file, $destination)) {
drupal_set_message($this
->t('The file could not be uploaded because the destination %destination is invalid.', [
'%destination' => $destination,
]), 'error');
return FALSE;
}
$move_result = file_unmanaged_move($uri, $file
->getFileUri());
if (!$move_result) {
drupal_set_message($this
->t('File upload error. Could not move uploaded file.'), 'error');
$this->logger
->notice('Upload error. Could not move uploaded file %file to destination %destination.', [
'%file' => $file
->getFilename(),
'%destination' => $file
->getFileUri(),
]);
return FALSE;
}
$this->fileSystem
->chmod($file
->getFileUri());
return $file;
}
public function validateFile(FileInterface $file, $extensions, array $additional_validators = []) {
$validators = $additional_validators;
if (!empty($extensions)) {
$validators['file_validate_extensions'] = [
$extensions,
];
}
$validators['file_validate_name_length'] = [];
return file_validate($file, $validators);
}
protected function renameExecutableExtensions(FileInterface $file) {
if (!$this->configFactory
->get('system.file')
->get('allow_insecure_uploads') && preg_match('/\\.(php|pl|py|cgi|asp|js)(\\.|$)/i', $file
->getFilename()) && substr($file
->getFilename(), -4) != '.txt') {
$file
->setMimeType('text/plain');
$file
->setFilename($file
->getFilename() . '.txt');
return TRUE;
}
return FALSE;
}
protected function prepareDestination(FileInterface $file, $destination) {
$destination_scheme = $this->fileSystem
->uriScheme($destination);
if (!$this->fileSystem
->validScheme($destination_scheme)) {
return FALSE;
}
if (!file_exists($destination)) {
$this->fileSystem
->mkdir($destination, NULL, TRUE);
}
if (substr($destination, -1) != '/') {
$destination .= '/';
}
$destination = file_destination($destination . $file
->getFilename(), FILE_EXISTS_RENAME);
$file
->setFileUri($destination);
return TRUE;
}
}