Upload.php in IMCE 8.2
File
src/Plugin/ImcePlugin/Upload.php
View source
<?php
namespace Drupal\imce\Plugin\ImcePlugin;
use Drupal\Core\File\FileSystemInterface;
use Drupal\file\FileInterface;
use Drupal\imce\ImcePluginBase;
use Drupal\imce\ImceFM;
class Upload extends ImcePluginBase {
public function permissionInfo() {
return [
'upload_files' => $this
->t('Upload files'),
];
}
public function buildPage(array &$page, ImceFM $fm) {
if ($fm
->hasPermission('upload_files')) {
$page['#attached']['library'][] = 'imce/drupal.imce.upload';
}
}
public function opUpload(ImceFM $fm) {
$folder = $fm->activeFolder;
if (!$folder || !$folder
->getPermission('upload_files')) {
return;
}
$destination = $folder
->getUri();
$replace = $fm
->getConf('replace', FileSystemInterface::EXISTS_RENAME);
$validators = [];
$exts = $fm
->getConf('extensions', '');
$validators['file_validate_extensions'] = [
$exts === '*' ? NULL : $exts,
];
$validators['file_validate_size'] = [
$fm
->getConf('maxsize'),
$fm
->getConf('quota'),
];
$width = $fm
->getConf('maxwidth');
$height = $fm
->getConf('maxheight');
if ($width || $height) {
$validators['file_validate_image_resolution'] = [
($width ? $width : 10000) . 'x' . ($height ? $height : 10000),
];
}
$validators[get_class($this) . '::validateFileName'] = [
$fm,
];
if ($files = file_save_upload('imce', $validators, $destination, NULL, $replace)) {
$fs = \Drupal::service('file_system');
foreach (array_filter($files) as $file) {
$file
->setPermanent();
$file
->save();
$name = $fs
->basename($file
->getFileUri());
$item = $folder
->addFile($name);
$item->uuid = $file
->uuid();
$item
->addToJs();
}
}
}
public static function validateFileName(FileInterface $file, ImceFM $fm) {
$errors = [];
$filename = $file
->getFileName();
if (!$fm
->validateFileName($filename, TRUE)) {
$errors[] = t('%filename contains invalid characters.', [
'%filename' => $filename,
]);
}
return $errors;
}
}
Classes
Name |
Description |
Upload |
Defines Imce Upload plugin. |