View source
<?php
namespace Drupal\filebrowser\Services;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManager;
use Drupal\Core\Url;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
class Common extends ControllerBase {
const ICON = 'icon';
const NAME = 'name';
const SIZE = 'size';
const CREATED = 'created';
const MIME_TYPE = 'mimetype';
const DESCRIPTION = 'description';
const CREATE_LISTING = 'create listings';
const DELETE_OWN_LISTINGS = 'delete listings';
const DELETE_ANY_LISTINGS = 'delete any listings';
const EDIT_OWN_LISTINGS = 'edit own listings';
const EDIT_ANY_LISTINGS = 'edit any listings';
const EDIT_DESCRIPTION = 'edit description';
const VIEW_LISTINGS = 'view listings';
const FILE_UPLOAD = 'upload files';
const CREATE_FOLDER = 'create folders';
const DOWNLOAD_ARCHIVE = 'download archive';
const DOWNLOAD = 'download files';
const DELETE_FILES = 'delete files';
const RENAME_FILES = 'rename files';
const FILEBROWSER_GRID_CONTAINER_CLASS = 'filebrowser-grid-container';
const FILEBROWSER_GRID_ROW_CLASS = 'filebrowser-grid-row';
const FILEBROWSER_GRID_ITEM_CLASS = 'filebrowser-grid-item';
const FILEBROWSER_GRID_CONTAINER_COLUMN_CLASS = 'filebrowser-grid-container-column';
protected $storage;
public function __construct(FilebrowserStorage $storage) {
$this->storage = $storage;
}
public function getFolderViewOptions() {
return [
'list-view' => [
'title' => $this
->t('Table - List of files in a table'),
'theme' => 'dir_listing_list_view',
],
'icon-view' => [
'title' => $this
->t('Grid - Thumbnail (or icon) of the files in a grid'),
'theme' => 'dir_listing_icon_view',
],
];
}
public function getDownloadManagerOptions() {
return [
'private' => [
'title' => $this
->t('Private - Files are served by PHP/Drupal'),
],
'public' => [
'title' => $this
->t('Public - Files are served by the web server and should be accessible by it'),
],
];
}
public function toCheckboxes($properties) {
$result = [];
if ($properties) {
foreach ($properties as $key => $arr) {
if ($arr) {
if (isset($arr['title'])) {
$result[$key] = $arr['title'];
}
else {
$result[$key] = $key;
}
}
}
}
return $result;
}
function canDownloadArchive(NodeInterface $node) {
$download_archive = $node->filebrowser->downloadArchive;
return $node
->access('view') && $download_archive && \Drupal::currentUser()
->hasPermission(Common::DOWNLOAD_ARCHIVE);
}
public function iconGenerate($file_type, $file_mimetype, $height, $width, $return_image = true) {
$ext = '.svg';
$mime_type = $this
->mimeIcon($file_mimetype);
$main_type = dirname($file_type);
if ($file_type == 'dir' && $mime_type != 'folder-parent') {
$mime_type = 'directory';
}
$theme_path = \Drupal::theme()
->getActiveTheme()
->getPath() . "/filebrowser/icons/";
$icons = [
$theme_path . $mime_type . $ext,
$theme_path . $main_type . $ext,
$this
->filebrowserPath() . '//icons/' . $mime_type . $ext,
$this
->filebrowserPath() . '//icons/' . $main_type . $ext,
];
$eligible = $this
->filebrowserPath() . '//icons/' . 'unknown' . $ext;
foreach ($icons as $icon) {
if (file_exists($icon)) {
$eligible = $icon;
break;
}
}
if ($return_image) {
$markup = file_get_contents($eligible, \FILE_TEXT);
return [
'#theme' => 'filebrowser_icon_svg',
'#html' => $markup,
'#data' => [
'height' => $height,
'width' => $width,
'class' => [
'filebrowser-svg',
$mime_type . '-icon',
],
],
'#test' => 'dir is een test',
];
}
else {
return $eligible;
}
}
function canExploreSubFolders(NodeInterface $node) {
return $node->filebrowser->exploreSubdirs;
}
function nodeContentLoad($fid) {
static $contents = [];
if (isset($contents[$fid])) {
return $contents[$fid];
}
$contents[$fid] = $this->storage
->loadRecord($fid);
if ($contents[$fid]) {
return $contents[$fid];
}
else {
return FALSE;
}
}
function nodeContentLoadMultiple(array $fids) {
$files = $this->storage
->nodeContentLoadMultiple($fids);
return $files;
}
public function nodeDelete($nid) {
$this->storage
->deleteContent($nid);
$this->storage
->deleteNode($nid);
}
function safeBasename($path) {
$path = rtrim($path, '/');
$path = explode('/', $path);
return end($path);
}
function safeDirname($path) {
$path = rtrim($path, '/');
$path = explode('/', $path);
array_pop($path);
$result = implode("/", $path);
if ($result == '') {
return '/';
}
return $result;
}
public function routeParam($nid, $query_fid = NULL) {
$p = empty($query_fid) ? [
'nid' => $nid,
'query_fid' => 0,
] : [
'nid' => $nid,
'query_fid' => $query_fid,
];
return $p;
}
public function redirectRoute($query_fid, $nid) {
$route['name'] = 'entity.node.canonical';
$route['node'] = [
'node' => $nid,
];
$route['query'] = !empty($query_fid) ? [
'query' => [
'fid' => $query_fid,
],
] : [];
return $route;
}
public function userAllowedActions($node) {
$actions = [];
$account = \Drupal::currentUser();
$filebrowser = $node->filebrowser;
if ($filebrowser->enabled && $account
->hasPermission(Common::FILE_UPLOAD)) {
$actions[] = [
'operation' => 'upload',
'title' => $this
->t('Upload'),
'type' => 'link',
'needs_item' => FALSE,
'route' => 'filebrowser.action',
];
}
if ($filebrowser->createFolders && $account
->hasPermission(Common::CREATE_FOLDER)) {
$actions[] = [
'operation' => 'folder',
'title' => $this
->t('Add folder'),
'needs_item' => FALSE,
'type' => 'link',
];
}
if ($account
->hasPermission(Common::DELETE_FILES)) {
$actions[] = [
'operation' => 'delete',
'title' => $this
->t('Delete'),
'needs_item' => TRUE,
'type' => 'button',
];
}
if ($filebrowser->enabled && $account
->hasPermission(Common::RENAME_FILES)) {
$actions[] = [
'operation' => 'rename',
'title' => $this
->t('Rename items'),
'needs_item' => TRUE,
'type' => 'button',
];
}
if ($filebrowser->enabled && $account
->hasPermission(Common::EDIT_DESCRIPTION)) {
$actions[] = [
'operation' => 'description',
'title' => $this
->t('Edit description'),
'needs_item' => TRUE,
'type' => 'button',
];
}
if ($this
->canDownloadArchive($node) && function_exists('zip_open')) {
$actions[] = [
'operation' => 'archive',
'title' => $this
->t('Download archive'),
'needs_item' => TRUE,
'type' => 'default',
];
}
return $actions;
}
public function closeButtonMarkup() {
return [
'#markup' => Link::fromTextAndUrl($this
->t('Close'), Url::fromUserInput('#', [
'attributes' => [
'class' => [
'filebrowser-close-window-link',
],
],
]))
->toString(),
];
}
public function isLocal($uri) {
$scheme = StreamWrapperManager::getScheme($uri);
return $scheme == 'public' || $scheme == 'private';
}
public function relativePath($fid) {
if (!$fid) {
return NULL;
}
return $this->storage
->loadRecord($fid)['path'];
}
private function mimeIcon($mime_type) {
if ($mime_type == 'folder/parent') {
return 'folder-parent';
}
$parts = explode('/', $mime_type);
switch ($parts[0]) {
case 'video':
case 'image':
case 'audio':
case 'text':
$mime = $parts[0];
break;
case 'application':
$mime = $this
->applicationMimeIcon($parts[1]);
break;
default:
$mime = 'unknown';
}
return $mime;
}
private function applicationMimeIcon($application_mime) {
$parts = explode('.', $application_mime);
switch ($parts[0]) {
case 'pdf':
case 'xml':
case 'msword':
case 'xhtml+xml':
$mime = $parts[0];
break;
case 'vnd':
$mime = $this
->vndMimeIcon($parts[1]);
break;
default:
$mime = 'unknown';
}
return $mime;
}
private function vndMimeIcon($vnd_mime) {
$parts = explode('.', $vnd_mime);
switch ($parts[0]) {
case 'ms-excel':
case 'ms-powerpoint':
case 'ms-word':
$mime = $parts[0];
break;
case 'vnd':
$mime = $this
->vndMimeIcon($parts[1]);
break;
default:
$mime = 'unknown';
}
return $mime;
}
public function getNodeFromPath($route_match = NULL) {
$route_match = $route_match ?: \Drupal::routeMatch();
if ($node = $route_match
->getParameter('node')) {
if (!is_object($node)) {
$node = Node::load($node);
}
return $node;
}
return NULL;
}
public function filebrowserPath() {
$module_handler = \Drupal::service('module_handler');
return $module_handler
->getModule('filebrowser')
->getPath();
}
}