Skip to content

Bugfix: In Create/Rename file with allowed extension #361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 80 additions & 40 deletions src/FileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
);
}
}
Expand All @@ -76,7 +77,7 @@ public function initialize(): array

return [
'result' => [
'status' => 'success',
'status' => 'success',
'message' => null,
],
'config' => $config,
Expand All @@ -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'],
];
}

Expand All @@ -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,
Expand Down Expand Up @@ -149,15 +150,17 @@ 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;
continue;
}

// check file type
if ($this->configRepository->getAllowFileTypes()
if (
$this->configRepository->getAllowFileTypes()
&& !in_array(
$file->getClientOriginalExtension(),
$this->configRepository->getAllowFileTypes()
Expand All @@ -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(
Expand All @@ -188,15 +191,15 @@ public function upload($disk, $path, $files, $overwrite): array
if ($fileNotUploaded) {
return [
'result' => [
'status' => 'warning',
'status' => 'warning',
'message' => 'notAllUploaded',
],
];
}

return [
'result' => [
'status' => 'success',
'status' => 'success',
'message' => 'uploaded',
],
];
Expand Down Expand Up @@ -232,7 +235,7 @@ public function delete($disk, $items): array

return [
'result' => [
'status' => 'success',
'status' => 'success',
'message' => 'deleted',
],
];
Expand Down Expand Up @@ -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',
],
];
Expand Down Expand Up @@ -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),
];
}

Expand All @@ -382,7 +394,7 @@ public function createDirectory($disk, $path, $name)
if (Storage::disk($disk)->exists($directoryName)) {
return [
'result' => [
'status' => 'warning',
'status' => 'warning',
'message' => 'dirExist',
],
];
Expand All @@ -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],
];
}

Expand All @@ -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',
],
];
Expand All @@ -435,10 +457,10 @@ public function createFile($disk, $path, $name): array

return [
'result' => [
'status' => 'success',
'status' => 'success',
'message' => 'fileCreated',
],
'file' => $fileProperties,
'file' => $fileProperties,
];
}

Expand All @@ -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,
];
}

Expand All @@ -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;
}
}
}