class DefaultController in Filebrowser 3.x
Same name and namespace in other branches
- 8.2 src/Controller/DefaultController.php \Drupal\filebrowser\Controller\DefaultController
Default controller for the filebrowser module.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\filebrowser\Controller\DefaultController
Expanded class hierarchy of DefaultController
File
- src/
Controller/ DefaultController.php, line 29
Namespace
Drupal\filebrowser\ControllerView source
class DefaultController extends ControllerBase {
/**
* @var \Drupal\filebrowser\FilebrowserManager $filebrowserManager
*/
protected $filebrowserManager;
/**
* @var \Drupal\filebrowser\Services\FilebrowserValidator
*/
protected $validator;
/**
* @var \Drupal\filebrowser\Services\Common
*/
protected $common;
/**
* DefaultController constructor.
* @param FilebrowserManager $filebrowserManager
* @param FilebrowserValidator $validator
* @param Common $common
*
*/
public function __construct(FilebrowserManager $filebrowserManager, FilebrowserValidator $validator, Common $common) {
$this->filebrowserManager = $filebrowserManager;
$this->validator = $validator;
$this->common = $common;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('filebrowser.manager'), $container
->get('filebrowser.validator'), $container
->get('filebrowser.common'));
}
/**
* Callback for
* route: filebrowser.page_download
* path: filebrowser/download/{fid}
* @param int $fid Id of the file selected in the download link
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function pageDownload($fid) {
/* @var NodeInterface $node **/
$node_content = $this->common
->nodeContentLoad($fid);
// If $fid doesn't point to a valid file, $node_content is FALSE.
if (!$node_content) {
throw new NotFoundHttpException();
}
$file_data = unserialize($node_content['file_data']);
$filebrowser = new Filebrowser($node_content['nid']);
// Download method is 'public' and the uri is public://
// we will send the browser to the file location.
// todo:
// RedirectResponse needs a relative path so we will convert the full url into a relative path
// This is done here, but should be moved to a better place in Common
$file_path = file_url_transform_relative($file_data->url);
if ($filebrowser->downloadManager == 'public' && StreamWrapperManager::getScheme($file_data->uri) == 'public') {
$response = new RedirectResponse($file_path);
return $response;
}
else {
// load the node containing the file so we can check
// for the access rights
// User needs "view" permission on the node to download the file
$node = Node::load($node_content['nid']);
if (isset($node) && $node
->access('view')) {
// Stream the file
$file = $file_data->uri;
// in case you need the container
//$container = $this->container;
$response = new StreamedResponse(function () use ($file) {
$handle = fopen($file, 'r') or exit("Cannot open file {$file}");
while (!feof($handle)) {
$buffer = fread($handle, 1024);
echo $buffer;
flush();
}
fclose($handle);
});
$response->headers
->set('Content-Type', $file_data->mimetype);
$content_disposition = $filebrowser->forceDownload ? 'attachment' : 'inline';
$response->headers
->set('Content-Disposition', $content_disposition . '; filename="' . $file_data->filename . '";');
return $response;
}
elseif (isset($node)) {
throw new AccessDeniedHttpException();
}
else {
throw new NotFoundHttpException();
}
}
}
/**
* @param int $nid
* @param int $query_fid In case of a sub folder, the fid of the sub folder
* @param string $op - The operation called by the submit button ('upload', 'delete')
* @param string $method - Defines if Ajax should be used
* @param string|null $fids A string containing the field id's of the files
* to be processed.
*
* @return \Drupal\Core\Ajax\AjaxResponse|\Drupal\Core\Render\HtmlResponse
*/
public function actionFormSubmitAction($nid, $query_fid, $op, $method, $fids = NULL) {
// $op == archive does not use a form
if ($op == 'archive') {
return $this
->actionArchive($fids);
}
// continue for buttons needing a form
// Determine the requested form name
$op = ucfirst($op);
$form_name = 'Drupal\\filebrowser\\Form\\' . $op . 'Form';
//debug($form_name);
$form = \Drupal::formBuilder()
->getForm($form_name, $nid, $query_fid, $fids, $method == 'ajax');
// If JS enabled
if ($method == 'ajax' && $op != 'Archive') {
// Create an AjaxResponse.
$response = new AjaxResponse();
// Remove old error in case they exist.
$response
->addCommand(new RemoveCommand('#filebrowser-form-action-error'));
// Remove slide-downs if they exist.
$response
->addCommand(new RemoveCommand('.form-in-slide-down'));
// Insert event details after event.
$response
->addCommand(new AfterCommand('#form-action-actions-wrapper', $form));
return $response;
}
else {
return $form;
}
}
public function inlineDescriptionForm($nid, $query_fid, $fids) {
return \Drupal::formBuilder()
->getForm('Drupal\\filebrowser\\Form\\InlineDescriptionForm', $nid, $query_fid, $fids);
}
/**
* @function
* zip file will be written to the temp directory on the local filesystem.
* @param $fids
* @return BinaryFileResponse|bool The binary response object or false if method cannot create archive
*/
public function actionArchive($fids) {
$fid_array = explode(',', $fids);
$itemsToArchive = null;
$itemsToArchive = $this->common
->nodeContentLoadMultiple($fid_array);
$file_name = \Drupal::service('file_system')
->realPath('public://' . uniqid('archive') . '.zip');
$archive = new \ZipArchive();
$created = $archive
->open($file_name, \ZipArchive::CREATE);
if ($created === TRUE) {
foreach ($itemsToArchive as $item) {
$file_data = unserialize($item['file_data']);
if ($file_data->type == 'file') {
$archive
->addFile(\Drupal::service('file_system')
->realpath($file_data->uri), $file_data->filename);
}
if ($file_data->type == 'dir') {
$dirPath = \Drupal::service('file_system')
->realpath($file_data->uri);
// Iterate through the directory, adding each file within
$iterator = new \RecursiveDirectoryIterator($dirPath);
// Skip files that begin with a dot
$iterator
->setFlags(\RecursiveDirectoryIterator::SKIP_DOTS);
$dirFiles = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST);
foreach ($dirFiles as $dirFile) {
if (is_dir($dirFile)) {
$archive
->addEmptyDir(str_replace(dirname($dirPath) . '/', '', $dirFile . ''));
}
else {
if (is_file($dirFile)) {
$archive
->addFromString(str_replace(dirname($dirPath) . '/', '', $dirFile), file_get_contents($dirFile));
}
}
}
}
}
$name = $archive->filename;
$archive
->close();
// serve the file
$response = new BinaryFileResponse($name);
$response
->deleteFileAfterSend(true);
$response
->trustXSendfileTypeHeader();
$response
->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT);
$response
->prepare(Request::createFromGlobals());
return $response;
}
else {
\Drupal::logger('filebrowser')
->error($this
->t('Can not create archive: @error', [
'@error' => $created,
]));
\Drupal::messenger()
->addError($this
->t('Can not create archive'));
return false;
}
}
public function noItemsError() {
$error = $this
->t('You didn\'t select any item');
// Create an AjaxResponse.
$response = new AjaxResponse();
// Remove old events
$response
->addCommand(new RemoveCommand('#filebrowser-form-action-error'));
$response
->addCommand(new RemoveCommand('.form-in-slide-down'));
// Insert event details after event.
// $response->addCommand(new AfterCommand('#form-action-actions-wrapper', $html));
$response
->addCommand(new AlertCommand($error));
return $response;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function | Returns a redirect response object for the specified route. | |
ControllerBase:: |
protected | function | Returns the state storage service. | |
DefaultController:: |
protected | property | ||
DefaultController:: |
protected | property | ||
DefaultController:: |
protected | property | ||
DefaultController:: |
public | function | @function zip file will be written to the temp directory on the local filesystem. | |
DefaultController:: |
public | function | ||
DefaultController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
DefaultController:: |
public | function | ||
DefaultController:: |
public | function | ||
DefaultController:: |
public | function | Callback for route: filebrowser.page_download path: filebrowser/download/{fid} | |
DefaultController:: |
public | function | DefaultController constructor. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |