From 79aaa64908fad03a712230275d34f3b9ca5943b7 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Tue, 15 Mar 2022 19:17:27 +0100 Subject: [PATCH] Use attributes + caching --- src/Driver.php | 105 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 77 insertions(+), 28 deletions(-) diff --git a/src/Driver.php b/src/Driver.php index 2c20d3b..777ecb4 100644 --- a/src/Driver.php +++ b/src/Driver.php @@ -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; @@ -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 @@ -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 @@ -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; } } @@ -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) { @@ -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']; @@ -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 ********************/ /** @@ -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); } /** @@ -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']; } @@ -542,7 +592,6 @@ protected function _filePutContents($path, $content) **/ protected function _basename($path) { - return basename($path); }