diff --git a/src/FileManager.php b/src/FileManager.php index da1642c..aa17d50 100644 --- a/src/FileManager.php +++ b/src/FileManager.php @@ -46,27 +46,28 @@ public function initialize(): array if (!config()->has('file-manager')) { return [ 'result' => [ - 'status' => 'danger', + 'status' => 'danger', 'message' => 'noConfig', ], ]; } $config = [ - 'acl' => $this->configRepository->getAcl(), - 'leftDisk' => $this->configRepository->getLeftDisk(), - 'rightDisk' => $this->configRepository->getRightDisk(), - 'leftPath' => $this->configRepository->getLeftPath(), - 'rightPath' => $this->configRepository->getRightPath(), + 'acl' => $this->configRepository->getAcl(), + 'leftDisk' => $this->configRepository->getLeftDisk(), + 'rightDisk' => $this->configRepository->getRightDisk(), + 'leftPath' => $this->configRepository->getLeftPath(), + 'rightPath' => $this->configRepository->getRightPath(), 'windowsConfig' => $this->configRepository->getWindowsConfig(), - 'hiddenFiles' => $this->configRepository->getHiddenFiles(), + 'hiddenFiles' => $this->configRepository->getHiddenFiles(), ]; // disk list foreach ($this->configRepository->getDiskList() as $disk) { if (array_key_exists($disk, config('filesystems.disks'))) { $config['disks'][$disk] = Arr::only( - config('filesystems.disks')[$disk], ['driver'] + config('filesystems.disks')[$disk], + ['driver'] ); } } @@ -76,7 +77,7 @@ public function initialize(): array return [ 'result' => [ - 'status' => 'success', + 'status' => 'success', 'message' => null, ], 'config' => $config, @@ -97,12 +98,12 @@ public function content($disk, $path): array $content = $this->getContent($disk, $path); return [ - 'result' => [ - 'status' => 'success', + 'result' => [ + 'status' => 'success', 'message' => null, ], 'directories' => $content['directories'], - 'files' => $content['files'], + 'files' => $content['files'], ]; } @@ -120,8 +121,8 @@ public function tree($disk, $path): array $directories = $this->getDirectoriesTree($disk, $path); return [ - 'result' => [ - 'status' => 'success', + 'result' => [ + 'status' => 'success', 'message' => null, ], 'directories' => $directories, @@ -149,7 +150,8 @@ public function upload($disk, $path, $files, $overwrite): array } // check file size - if ($this->configRepository->getMaxUploadFileSize() + if ( + $this->configRepository->getMaxUploadFileSize() && $file->getSize() / 1024 > $this->configRepository->getMaxUploadFileSize() ) { $fileNotUploaded = true; @@ -157,7 +159,8 @@ public function upload($disk, $path, $files, $overwrite): array } // check file type - if ($this->configRepository->getAllowFileTypes() + if ( + $this->configRepository->getAllowFileTypes() && !in_array( $file->getClientOriginalExtension(), $this->configRepository->getAllowFileTypes() @@ -170,12 +173,12 @@ public function upload($disk, $path, $files, $overwrite): array $name = $file->getClientOriginalName(); if ($this->configRepository->getSlugifyNames()) { $name = Str::slug( - Str::replace( - '.' . $file->getClientOriginalExtension(), - '', - $name - ) - ) . '.' . $file->getClientOriginalExtension(); + Str::replace( + '.' . $file->getClientOriginalExtension(), + '', + $name + ) + ) . '.' . $file->getClientOriginalExtension(); } // overwrite or save file Storage::disk($disk)->putFileAs( @@ -188,7 +191,7 @@ public function upload($disk, $path, $files, $overwrite): array if ($fileNotUploaded) { return [ 'result' => [ - 'status' => 'warning', + 'status' => 'warning', 'message' => 'notAllUploaded', ], ]; @@ -196,7 +199,7 @@ public function upload($disk, $path, $files, $overwrite): array return [ 'result' => [ - 'status' => 'success', + 'status' => 'success', 'message' => 'uploaded', ], ]; @@ -232,7 +235,7 @@ public function delete($disk, $items): array return [ 'result' => [ - 'status' => 'success', + 'status' => 'success', 'message' => 'deleted', ], ]; @@ -273,11 +276,20 @@ public function paste($disk, $path, $clipboard): array */ public function rename($disk, $newName, $oldName): array { + if (!$this->AllowTypes($newName)) { + return [ + 'result' => [ + 'status' => 'error', + 'message' => "Failed to rename the file because extension is not allowed", + ], + ]; + } + Storage::disk($disk)->move($oldName, $newName); return [ 'result' => [ - 'status' => 'success', + 'status' => 'success', 'message' => 'renamed', ], ]; @@ -359,10 +371,10 @@ public function url($disk, $path): array { return [ 'result' => [ - 'status' => 'success', + 'status' => 'success', 'message' => null, ], - 'url' => Storage::disk($disk)->url($path), + 'url' => Storage::disk($disk)->url($path), ]; } @@ -382,7 +394,7 @@ public function createDirectory($disk, $path, $name) if (Storage::disk($disk)->exists($directoryName)) { return [ 'result' => [ - 'status' => 'warning', + 'status' => 'warning', 'message' => 'dirExist', ], ]; @@ -395,16 +407,16 @@ public function createDirectory($disk, $path, $name) ); // add directory properties for the tree module - $tree = $directoryProperties; + $tree = $directoryProperties; $tree['props'] = ['hasSubdirectories' => false]; return [ - 'result' => [ - 'status' => 'success', + 'result' => [ + 'status' => 'success', 'message' => 'dirCreated', ], 'directory' => $directoryProperties, - 'tree' => [$tree], + 'tree' => [$tree], ]; } @@ -419,12 +431,22 @@ public function createDirectory($disk, $path, $name) */ public function createFile($disk, $path, $name): array { + if (!$this->AllowTypes($name)) { + return [ + 'result' => [ + 'status' => 'error', + 'message' => "Failed to create file because extension is not allowed", + ], + ]; + } + + $path = $this->newPath($path, $name); if (Storage::disk($disk)->exists($path)) { return [ 'result' => [ - 'status' => 'warning', + 'status' => 'warning', 'message' => 'fileExist', ], ]; @@ -435,10 +457,10 @@ public function createFile($disk, $path, $name): array return [ 'result' => [ - 'status' => 'success', + 'status' => 'success', 'message' => 'fileCreated', ], - 'file' => $fileProperties, + 'file' => $fileProperties, ]; } @@ -459,15 +481,15 @@ public function updateFile($disk, $path, $file): array $file->getClientOriginalName() ); - $filePath = $this->newPath($path, $file->getClientOriginalName()); + $filePath = $this->newPath($path, $file->getClientOriginalName()); $fileProperties = $this->fileProperties($disk, $filePath); return [ 'result' => [ - 'status' => 'success', + 'status' => 'success', 'message' => 'fileUpdated', ], - 'file' => $fileProperties, + 'file' => $fileProperties, ]; } @@ -490,4 +512,22 @@ public function streamFile($disk, $path): StreamedResponse return Storage::disk($disk)->response($path, $filename, ['Accept-Ranges' => 'bytes']); } -} + + private function AllowTypes($name) + { + $ext = explode('.', $name); + $ext = end($ext); + + if ( + $this->configRepository->getAllowFileTypes() + && !in_array( + $ext, + $this->configRepository->getAllowFileTypes() + ) + ) { + return false; + } else { + return true; + } + } +} \ No newline at end of file