View source
<?php
namespace Drupal\filebrowser\Controller;
use Drupal\Core\Ajax\AfterCommand;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\AlertCommand;
use Drupal\Core\Ajax\RemoveCommand;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\StreamWrapper\StreamWrapperManager;
use Drupal\filebrowser\Filebrowser;
use Drupal\filebrowser\FilebrowserManager;
use Drupal\filebrowser\Services\FilebrowserValidator;
use Drupal\filebrowser\Services\Common;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class DefaultController extends ControllerBase {
protected $filebrowserManager;
protected $validator;
protected $common;
public function __construct(FilebrowserManager $filebrowserManager, FilebrowserValidator $validator, Common $common) {
$this->filebrowserManager = $filebrowserManager;
$this->validator = $validator;
$this->common = $common;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('filebrowser.manager'), $container
->get('filebrowser.validator'), $container
->get('filebrowser.common'));
}
public function pageDownload($fid) {
$node_content = $this->common
->nodeContentLoad($fid);
if (!$node_content) {
throw new NotFoundHttpException();
}
$file_data = unserialize($node_content['file_data']);
$filebrowser = new Filebrowser($node_content['nid']);
$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 {
$node = Node::load($node_content['nid']);
if (isset($node) && $node
->access('view')) {
$file = $file_data->uri;
$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();
}
}
}
public function actionFormSubmitAction($nid, $query_fid, $op, $method, $fids = NULL) {
if ($op == 'archive') {
return $this
->actionArchive($fids);
}
$op = ucfirst($op);
$form_name = 'Drupal\\filebrowser\\Form\\' . $op . 'Form';
$form = \Drupal::formBuilder()
->getForm($form_name, $nid, $query_fid, $fids, $method == 'ajax');
if ($method == 'ajax' && $op != 'Archive') {
$response = new AjaxResponse();
$response
->addCommand(new RemoveCommand('#filebrowser-form-action-error'));
$response
->addCommand(new RemoveCommand('.form-in-slide-down'));
$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);
}
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);
$iterator = new \RecursiveDirectoryIterator($dirPath);
$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();
$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');
$response = new AjaxResponse();
$response
->addCommand(new RemoveCommand('#filebrowser-form-action-error'));
$response
->addCommand(new RemoveCommand('.form-in-slide-down'));
$response
->addCommand(new AlertCommand($error));
return $response;
}
}