Skip to content

Commit

Permalink
Merge pull request #85 from barryvdh/feat-attributes
Browse files Browse the repository at this point in the history
Use attributes + caching
  • Loading branch information
barryvdh authored Mar 15, 2022
2 parents c9fea39 + 79aaa64 commit 15e15ef
Showing 1 changed file with 77 additions and 28 deletions.
105 changes: 77 additions & 28 deletions src/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
use Intervention\Image\ImageManager;
use League\Flysystem\Cached\CachedAdapter;
use League\Flysystem\Cached\CacheInterface;
use League\Flysystem\DirectoryAttributes;
use League\Flysystem\FileAttributes;
use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemOperator;
use League\Flysystem\StorageAttributes;
use League\Flysystem\UnableToCopyFile;
use League\Flysystem\UnableToCreateDirectory;
use League\Flysystem\UnableToDeleteDirectory;
Expand Down Expand Up @@ -43,6 +46,9 @@ class Driver extends elFinderVolumeDriver
/** @var ImageManager $imageManager */
protected $imageManager = null;

/** @var StorageAttributes $attributes */
protected $attributeCache = [];

/**
* Constructor
* Extend options with required fields
Expand All @@ -63,6 +69,14 @@ public function __construct()
$this->options = array_merge($this->options, $opts);
}

protected function clearcache()
{
parent::clearcache();

// clear cached attributes
$this->attributeCache = [];
}

public function mount(array $opts)
{
// If path is not set, use the root
Expand Down Expand Up @@ -153,8 +167,9 @@ protected function _dirExists($path)
$dir = $this->_dirname($path);
$basename = basename($path);

foreach ($this->fs->listContents($dir)->toArray() as $meta) {
if ($meta && $meta['type'] !== 'file' && $meta['basename'] == $basename) {
/** @var StorageAttributes $meta */
foreach ($this->listContents($dir) as $attribute) {
if ($attribute->isDir() && $this->_basename($meta->path()) == $basename) {
return true;
}
}
Expand Down Expand Up @@ -198,33 +213,51 @@ protected function _stat($path)
return $stat;
}

// If not exists, return empty
if (!$this->fs->has($path)) {

// Check if the parent doesn't have this path
if ($this->_dirExists($path)) {
return $stat;
}

// Neither a file or directory exist, return empty
return array();
}
if (isset($this->attributeCache[$path])) {
/** @var StorageAttributes $attributes */
$attributes = $this->attributeCache[$path];

try {
$meta = [
'mimetype' => null,
'mimetype' => $attributes->type(),
'extension' => null,
'size' => null,
'type' => $this->fs->fileExists($path) ? 'file' : 'dir',
'timestamp' => $attributes->lastModified(),
'type' => $attributes->isFile() ? 'file' : 'dir',
];

if ($meta['type'] === 'file') {
$meta['mimetype'] = $this->fs->mimeType($path);
$meta['timestamp'] = $this->fs->lastModified($path);
$meta['size'] = $this->fs->fileSize($path);
if ($attributes instanceof FileAttributes) {
$meta['mimetype'] = $attributes->mimeType();
$meta['size'] = $attributes->fileSize();
}
} else {
// If not exists, return empty
if (!$this->fs->has($path)) {

// Check if the parent doesn't have this path
if ($this->_dirExists($path)) {
return $stat;
}

// Neither a file or directory exist, return empty
return array();
}

try {
$meta = [
'mimetype' => null,
'extension' => null,
'size' => null,
'type' => $this->fs->fileExists($path) ? 'file' : 'dir',
];

if ($meta['type'] === 'file') {
$meta['mimetype'] = $this->fs->mimeType($path);
$meta['timestamp'] = $this->fs->lastModified($path);
$meta['size'] = $this->fs->fileSize($path);
}
} catch (\Exception $e) {
return array();
}
} catch (\Exception $e) {
return array();
}

if(false === $meta) {
Expand Down Expand Up @@ -252,7 +285,7 @@ protected function _stat($path)
if(isset($meta['mimetype'])) {
$stat['mime'] = $meta['mimetype'];
} else {
$stat['mime'] = $this->fs->mimeType($path);
$stat['mime'] = null;
}

$imgMimes = ['image/jpeg', 'image/png', 'image/gif'];
Expand All @@ -272,6 +305,23 @@ protected function _stat($path)
return $stat;
}

/**
* @param $path
* @return array|StorageAttributes[]
* @throws \League\Flysystem\FilesystemException
*/
protected function listContents($path): array
{
$contents = $this->fs->listContents($path)->toArray();

/** @var StorageAttributes $item */
foreach ($contents as $item) {
$this->attributeCache[$item['path']] = $item;
}

return $contents;
}

/***************** file stat ********************/

/**
Expand All @@ -282,11 +332,11 @@ protected function _stat($path)
**/
protected function _subdirs($path)
{
$contents = $this->fs->listContents($path)->filter(function ($item) {
return $item['type'] == 'dir';
$contents = array_filter($this->listContents($path), function (StorageAttributes $item) {
return $item->isDir();
});

return !empty($contents->toArray());
return !empty($contents);
}

/**
Expand Down Expand Up @@ -318,7 +368,7 @@ protected function _scandir($path)
{
$paths = array();

foreach ($this->fs->listContents($path, false)->toArray() as $object) {
foreach ($this->listContents($path, false) as $object) {
if ($object) {
$paths[] = $object['path'];
}
Expand Down Expand Up @@ -542,7 +592,6 @@ protected function _filePutContents($path, $content)
**/
protected function _basename($path)
{

return basename($path);
}

Expand Down

0 comments on commit 15e15ef

Please sign in to comment.