You are here

class DefaultController in Filebrowser 8.2

Same name and namespace in other branches
  1. 3.x src/Controller/DefaultController.php \Drupal\filebrowser\Controller\DefaultController

Default controller for the filebrowser module.

Hierarchy

Expanded class hierarchy of DefaultController

File

src/Controller/DefaultController.php, line 29

Namespace

Drupal\filebrowser\Controller
View 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

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
DefaultController::$common protected property
DefaultController::$filebrowserManager protected property
DefaultController::$validator protected property
DefaultController::actionArchive public function @function zip file will be written to the temp directory on the local filesystem.
DefaultController::actionFormSubmitAction public function
DefaultController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
DefaultController::inlineDescriptionForm public function
DefaultController::noItemsError public function
DefaultController::pageDownload public function Callback for route: filebrowser.page_download path: filebrowser/download/{fid}
DefaultController::__construct public function DefaultController constructor.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.