From f10e67850335d9c1832eab3b45102c7285e8295b Mon Sep 17 00:00:00 2001 From: Bogdan Mucenica Date: Fri, 8 Apr 2022 14:10:19 +0300 Subject: [PATCH] change filesystem calls to work with Flysystem v2 used by laravel 9 --- src/Http/Services/GetFiles.php | 422 ++++++++++++++-------------- src/Http/Services/NormalizeFile.php | 6 +- 2 files changed, 221 insertions(+), 207 deletions(-) diff --git a/src/Http/Services/GetFiles.php b/src/Http/Services/GetFiles.php index a23d567..304572b 100644 --- a/src/Http/Services/GetFiles.php +++ b/src/Http/Services/GetFiles.php @@ -4,8 +4,9 @@ use Carbon\Carbon; use GuzzleHttp\Client; -use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; +use League\Flysystem\FileAttributes; +use League\Flysystem\StorageAttributes; trait GetFiles { @@ -27,7 +28,7 @@ trait GetFiles */ public function getFiles($folder, $order, $filter = false) { - $filesData = $this->storage->listContents($folder); + $filesData = $this->listContents($folder); $filesData = $this->normalizeFiles($filesData); $files = []; @@ -55,6 +56,53 @@ public function getFiles($folder, $order, $filter = false) return $this->orderData($files, $order, config('filemanager.direction', 'asc')); } + protected function listContents(string $folder) + { + return $this->storage->listContents($folder)->map(fn(StorageAttributes $attributes) => [ + 'type' => $attributes->type(), + 'basename' => basename($attributes->path()), + 'path' => $attributes->path(), + 'size' => $attributes instanceof FileAttributes ? $attributes->fileSize() : 0, + 'visibility' => $attributes->visibility(), + 'lastModified' => $attributes->lastModified(), + 'mimeType' => $attributes instanceof FileAttributes ? $this->storage->mimeType($attributes->path()) : null, + ])->toArray(); + } + + /** + * @param $files + */ + public function normalizeFiles($files) + { + foreach ($files as $key => $file) { + if (! isset($file['extension'])) { + $files[$key]['extension'] = null; + } + if (! isset($file['size'])) { + // $size = $this->storage->getSize($file['path']); + $files[$key]['size'] = null; + } + } + + return $files; + } + + /** + * Generates an id based on file. + * + * @param array $file + * + * @return string + */ + public function generateId($file) + { + if (isset($file['timestamp'])) { + return md5($this->disk . '_' . trim($file['basename']) . $file['timestamp']); + } + + return md5($this->disk . '_' . trim($file['basename'])); + } + /** * @param $file * @param $id @@ -63,18 +111,18 @@ public function getFileData($file, $id) { if (! $this->isDot($file) && ! $this->exceptExtensions->contains($file['extension']) && ! $this->exceptFolders->contains($file['basename']) && ! $this->exceptFiles->contains($file['basename']) && $this->accept($file)) { $fileInfo = [ - 'id' => $id, - 'name' => trim($file['basename']), - 'path' => $this->cleanSlashes($file['path']), - 'type' => $file['type'], - 'mime' => $this->getFileType($file), - 'ext' => (isset($file['extension'])) ? $file['extension'] : false, - 'size' => ($file['size'] != 0) ? $file['size'] : 0, + 'id' => $id, + 'name' => trim($file['basename']), + 'path' => $this->cleanSlashes($file['path']), + 'type' => $file['type'], + 'mime' => $this->getFileType($file), + 'ext' => (isset($file['extension'])) ? $file['extension'] : false, + 'size' => ($file['size'] != 0) ? $file['size'] : 0, 'size_human' => ($file['size'] != 0) ? $this->formatBytes($file['size'], 0) : 0, - 'thumb' => $this->getThumbFile($file), - 'asset' => $this->cleanSlashes($this->storage->url($file['basename'])), - 'can' => true, - 'loading' => false, + 'thumb' => $this->getThumbFile($file), + 'asset' => $this->cleanSlashes($this->storage->url($file['basename'])), + 'can' => true, + 'loading' => false, ]; if (isset($file['timestamp'])) { @@ -85,7 +133,7 @@ public function getFileData($file, $id) if ($fileInfo['mime'] == 'image') { [$width, $height] = $this->getImageDimesions($file); if (! $width == false) { - $fileInfo['dimensions'] = $width.'x'.$height; + $fileInfo['dimensions'] = $width . 'x' . $height; } } @@ -95,130 +143,34 @@ public function getFileData($file, $id) } } - return (object) $fileInfo; + return (object)$fileInfo; } } /** - * Filter data by custom type. - * - * @param $files - * @param $filter - * - * @return mixed - */ - public function filterData($files, $filter) - { - $folders = $files->where('type', 'dir'); - $items = $files->where('type', 'file'); - - $filters = config('filemanager.filters', []); - if (count($filters) > 0) { - $filters = array_change_key_case($filters); - - if (isset($filters[$filter])) { - $filteredExtensions = $filters[$filter]; - - $filtered = $items->filter(function ($item) use ($filteredExtensions) { - if (in_array($item->ext, $filteredExtensions)) { - return $item; - } - }); - } - } - - return $folders->merge($filtered); - } - - /** - * Order files and folders. - * - * @param $files - * @param $order - * - * @return mixed - */ - public function orderData($files, $order, $direction = 'asc') - { - $folders = $files->where('type', 'dir'); - $items = $files->where('type', 'file'); - - if ($order == 'size') { - $folders = $folders->sortByDesc($order); - $items = $items->sortByDesc($order); - } else { - if ($direction == 'asc') { - // mb_strtolower to fix order by alpha - $folders = $folders->sortBy('name')->sortBy(function ($item) use ($order) { - return mb_strtolower($item->{$order}); - })->values(); - - $items = $items->sortBy('name')->sortBy(function ($item) use ($order) { - return mb_strtolower($item->{$order}); - })->values(); - } else { - // mb_strtolower to fix order by alpha - $folders = $folders->sortByDesc(function ($item) use ($order) { - return mb_strtolower($item->{$order}); - })->values(); - - $items = $items->sortByDesc(function ($item) use ($order) { - return mb_strtolower($item->{$order}); - })->values(); - } - } - - $result = $folders->merge($items); - - return $result; - } - - /** - * Generates an id based on file. + * Check if file is Dot. * - * @param array $file + * @param string $file * - * @return string + * @return bool */ - public function generateId($file) + public function isDot($file) { - if (isset($file['timestamp'])) { - return md5($this->disk.'_'.trim($file['basename']).$file['timestamp']); + if (Str::startsWith($file['basename'], '.')) { + return true; } - return md5($this->disk.'_'.trim($file['basename'])); - } - - /** - * Set Relative Path. - * - * @param $folder - */ - public function setRelativePath($folder) - { - $defaultPath = $this->storage->getDriver()->getAdapter()->getPathPrefix(); - - $publicPath = str_replace($defaultPath, '', $folder); - - if ($folder != '/') { - $this->currentPath = $this->getAppend().'/'.$publicPath; - } else { - $this->currentPath = $this->getAppend().$publicPath; - } + return false; } /** - * Get Append to url. + * @param $file * - * @return mixed|string + * @return bool */ - public function getAppend() + public function accept($file) { - if (in_array(config('filemanager.disk'), $this->cloudDisks)) { - return ''; - } - - return '/storage'; + return '.' !== substr($file['basename'], 0, 1); } /** @@ -232,7 +184,7 @@ public function getFileType($file) return 'dir'; } - $mime = $this->storage->getMimetype($file['path']); + $mime = $file['mimeType']; $extension = $file['extension']; if (Str::contains($mime, 'directory')) { @@ -298,6 +250,15 @@ public function getFileType($file) return false; } + /** + * @param $file + * @return mixed + */ + public function getThumbFile($file) + { + return $this->cleanSlashes($this->getThumb($file, $this->currentPath)); + } + /** * Return the Type of file. * @@ -311,7 +272,7 @@ public function getThumb($file, $folder = false) return false; } - $mime = $this->storage->getMimetype($file['path']); + $mime = $file['mimeType']; $extension = $file['extension']; if (Str::contains($mime, 'directory')) { @@ -323,7 +284,7 @@ public function getThumb($file, $folder = false) return $this->storage->url($file['path']); } - return $folder.'/'.$file['basename']; + return $folder . '/' . $file['basename']; } $fileType = new FileTypesImages(); @@ -331,6 +292,18 @@ public function getThumb($file, $folder = false) return $fileType->getImage($mime); } + /** + * @param $timestamp + */ + public function modificationDate($time) + { + try { + return Carbon::createFromTimestamp($time)->format('Y-m-d H:i:s'); + } catch (\Exception $e) { + return false; + } + } + /** * Get image dimensions for files. * @@ -350,76 +323,146 @@ public function getImageDimesions($file) } /** - * Get image dimensions from cloud. - * - * @param $file + * Hide folders with .hide file. + * @param $oath */ - public function getImageDimesionsFromCloud($file) + private function checkShouldHideFolder($path) { - try { - $client = new Client(); + $filesData = $this->listContents($path); - $response = $client->get($this->storage->url($file['path']), ['stream' => true]); - $image = imagecreatefromstring($response->getBody()->getContents()); - $dims = [imagesx($image), imagesy($image)]; - imagedestroy($image); + $key = array_search('.hide', array_column($filesData, 'basename')); - return $dims; - } catch (\Exception $e) { - return false; + if ($key === false) { + return true; } return false; } /** - * @param $file + * Filter data by custom type. + * + * @param $files + * @param $filter + * * @return mixed */ - public function getThumbFile($file) + public function filterData($files, $filter) { - return $this->cleanSlashes($this->getThumb($file, $this->currentPath)); + $folders = $files->where('type', 'dir'); + $items = $files->where('type', 'file'); + + $filters = config('filemanager.filters', []); + if (count($filters) > 0) { + $filters = array_change_key_case($filters); + + if (isset($filters[$filter])) { + $filteredExtensions = $filters[$filter]; + + $filtered = $items->filter(function ($item) use ($filteredExtensions) { + if (in_array($item->ext, $filteredExtensions)) { + return $item; + } + }); + } + } + + return $folders->merge($filtered); } /** + * Order files and folders. + * * @param $files + * @param $order + * + * @return mixed */ - public function normalizeFiles($files) + public function orderData($files, $order, $direction = 'asc') { - foreach ($files as $key => $file) { - if (! isset($file['extension'])) { - $files[$key]['extension'] = null; - } - if (! isset($file['size'])) { - // $size = $this->storage->getSize($file['path']); - $files[$key]['size'] = null; + $folders = $files->where('type', 'dir'); + $items = $files->where('type', 'file'); + + if ($order == 'size') { + $folders = $folders->sortByDesc($order); + $items = $items->sortByDesc($order); + } else { + if ($direction == 'asc') { + // mb_strtolower to fix order by alpha + $folders = $folders->sortBy('name')->sortBy(function ($item) use ($order) { + return mb_strtolower($item->{$order}); + })->values(); + + $items = $items->sortBy('name')->sortBy(function ($item) use ($order) { + return mb_strtolower($item->{$order}); + })->values(); + } else { + // mb_strtolower to fix order by alpha + $folders = $folders->sortByDesc(function ($item) use ($order) { + return mb_strtolower($item->{$order}); + })->values(); + + $items = $items->sortByDesc(function ($item) use ($order) { + return mb_strtolower($item->{$order}); + })->values(); } } - return $files; + $result = $folders->merge($items); + + return $result; } /** - * @param $file + * Set Relative Path. * - * @return bool + * @param $folder */ - public function accept($file) + public function setRelativePath($folder) { - return '.' !== substr($file['basename'], 0, 1); + $defaultPath = $this->storage->path(''); + + $publicPath = str_replace($defaultPath, '', $folder); + + if ($folder != '/') { + $this->currentPath = $this->getAppend() . '/' . $publicPath; + } else { + $this->currentPath = $this->getAppend() . $publicPath; + } } /** - * Check if file is Dot. + * Get Append to url. * - * @param string $file + * @return mixed|string + */ + public function getAppend() + { + if (in_array(config('filemanager.disk'), $this->cloudDisks)) { + return ''; + } + + return '/storage'; + } + + /** + * Get image dimensions from cloud. * - * @return bool + * @param $file */ - public function isDot($file) + public function getImageDimesionsFromCloud($file) { - if (Str::startsWith($file['basename'], '.')) { - return true; + try { + $client = new Client(); + + $response = $client->get($this->storage->url($file['path']), ['stream' => true]); + $image = imagecreatefromstring($response->getBody()->getContents()); + $dims = [imagesx($image), imagesy($image)]; + imagedestroy($image); + + return $dims; + } catch (\Exception $e) { + return false; } return false; @@ -441,20 +484,20 @@ public function generateParent($folder) } return [ - 'id' => 'folder_back', - 'name' => __('Go up'), - 'path' => $this->cleanSlashes($folderPath), - 'type' => 'dir', - 'mime' => 'dir', - 'ext' => false, - 'size' => 0, - 'size_human' => 0, - 'thumb' => '', - 'asset' => $this->cleanSlashes($this->storage->url($folderPath)), - 'can' => true, - 'loading' => false, + 'id' => 'folder_back', + 'name' => __('Go up'), + 'path' => $this->cleanSlashes($folderPath), + 'type' => 'dir', + 'mime' => 'dir', + 'ext' => false, + 'size' => 0, + 'size_human' => 0, + 'thumb' => '', + 'asset' => $this->cleanSlashes($this->storage->url($folderPath)), + 'can' => true, + 'loading' => false, 'last_modification' => false, - 'date' => false, + 'date' => false, ]; } } @@ -464,7 +507,7 @@ public function generateParent($folder) */ public function getPaths($currentFolder) { - $defaultPath = $this->cleanSlashes($this->storage->getDriver()->getAdapter()->getPathPrefix()); + $defaultPath = $this->cleanSlashes($this->storage->path('')); $currentPath = $this->cleanSlashes($this->storage->path($currentFolder)); $paths = $currentPath; @@ -489,35 +532,6 @@ public function getPaths($currentFolder) */ public function recursivePaths($name, $pathCollection) { - return Str::before($pathCollection->implode('/'), $name).$name; - } - - /** - * @param $timestamp - */ - public function modificationDate($time) - { - try { - return Carbon::createFromTimestamp($time)->format('Y-m-d H:i:s'); - } catch (\Exception $e) { - return false; - } - } - - /** - * Hide folders with .hide file. - * @param $oath - */ - private function checkShouldHideFolder($path) - { - $filesData = $this->storage->listContents($path); - - $key = array_search('.hide', array_column($filesData, 'basename')); - - if ($key === false) { - return true; - } - - return false; + return Str::before($pathCollection->implode('/'), $name) . $name; } } diff --git a/src/Http/Services/NormalizeFile.php b/src/Http/Services/NormalizeFile.php index ee12c9d..7bb23fc 100644 --- a/src/Http/Services/NormalizeFile.php +++ b/src/Http/Services/NormalizeFile.php @@ -66,12 +66,12 @@ public function toArray() */ private function setExtras(Collection $data) { - $mime = $this->storage->getMimetype($this->storagePath); + $mime = $this->storage->mimeType($this->storagePath); // Image if (Str::contains($mime, 'image') || $data['ext'] == 'svg') { $data->put('type', 'image'); - $data->put('dimensions', $this->getDimensions($this->storage->getMimetype($this->storagePath))); + $data->put('dimensions', $this->getDimensions($this->storage->mimeType($this->storagePath))); } // Video @@ -199,7 +199,7 @@ private function getCorrectMimeFileType() //If no type - return $this->storage->getMimetype($this->storagePath); + return $this->storage->mimeType($this->storagePath); } private function availablesTextExtensions()