You are here

ServerFileList.php in Filebrowser 3.x

Same filename and directory in other branches
  1. 8.2 src/ServerFileList.php

Namespace

Drupal\filebrowser

File

src/ServerFileList.php
View source
<?php

namespace Drupal\filebrowser;

use Drupal\filebrowser\Services\Common;
use Drupal\node\NodeInterface;
class ServerFileList {

  /**
   * @var array
   */
  protected $serverFileList;

  /**
   * @var NodeInterface
   */
  protected $node;

  /**
   * @var $string
   */
  protected $relativePath;

  /**
   * @var Common
   */
  protected $common;

  /**
   * @var Filebrowser
   */
  protected $filebrowser;

  /**
   * ServerFileList constructor.
   * @param \Drupal\node\NodeInterface $node
   * @param string $relative_path
   */
  public function __construct(NodeInterface $node, $relative_path) {
    $this->serverFileList = $this
      ->createServerFileList($node, $relative_path);
    $this->common = \Drupal::service('filebrowser.common');
    $this->filebrowser = $node->filebrowser;
  }
  public function getList() {
    return $this->serverFileList;
  }

  /**
   * Retrieves files from the file system
   * @param NodeInterface $node
   * @param string $relative_path
   * @return array list of files filtered as per node settings and restrictions
   * returns an array of objects keyed to the uri:
   *   [public://directory/ic_autorenew_white_18px.svg] => stdClass Object(
   *     [uri] => public://directory/ic_autorenew_white_18px.svg // file_scan_directory
   *     [filename] => ic_autorenew_white_18px.svg               // file_scan_directory
   *     [name] => ic_autorenew_white_18px                       // file_scan_directory
   *     [url] => http://drupal8.dev/sites/default/files/NFTR/file.svg
   *     [mimetype] => application/octet-stream
   *     [size] => 394
   *     [type] => file
   *     [timestamp] => 1460968364)
   */
  protected function createServerFileList($node, $relative_path) {

    /** @var Filebrowser $folder_path */
    $folder_path = $node->filebrowser->folderPath;
    $directory = $folder_path . $relative_path;
    $files = \Drupal::service('file_system')
      ->scanDirectory($directory, '/.*/', [
      'recurse' => false,
    ]);
    $validator = \Drupal::service('filebrowser.validator');
    $guessor = \Drupal::service('file.mime_type.guesser');
    foreach ($files as $key => $file) {
      $file->url = file_create_url($file->uri);

      // Complete the required file data
      $file->mimetype = $guessor
        ->guess($file->filename);
      $file->size = filesize($file->uri);
      $file->type = filetype($file->uri);
      $file->timestamp = filectime($file->uri);
      if ($file->type != 'dir' && !$validator
        ->whiteListed($file->filename, $node->filebrowser->whitelist) || $validator
        ->blackListed($file->filename, $node->filebrowser->forbiddenFiles) || !$node->filebrowser->exploreSubdirs && $file->type == 'dir') {
        unset($files[$key]);
      }
    }
    return $files;
  }

}

Classes

Namesort descending Description
ServerFileList