Skip to content

Commit 8e81855

Browse files
committed
remove restricted characters from filenames
1 parent 135e8ea commit 8e81855

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed

src/Controller/Component/UploadComponent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class UploadComponent extends Component
2525
];
2626

2727
/**
28-
* Allowed storage types
28+
* Allowed stoage types
2929
*
3030
* @var string[]
3131
*/

src/File/PathUtils.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace FileUpload\File;
5+
6+
use Cake\Utility\Text;
7+
8+
class PathUtils
9+
{
10+
public static function fileNameSanitize(string $filename): string
11+
{
12+
// get extension even if filename has multiple dots
13+
$ext = pathinfo($filename, PATHINFO_EXTENSION);
14+
$filename = pathinfo($filename, PATHINFO_FILENAME);
15+
$filename = Text::slug($filename);
16+
17+
return $filename . '.' . $ext;
18+
}
19+
}

src/File/UploadedFileDecorator.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,13 @@ public function get(string $key): string
2828
{
2929
return $this->options[$key] ?? "";
3030
}
31+
32+
public function getFileName(): string
33+
{
34+
if (isset($this->options['fileName'])) {
35+
return $this->options['fileName'];
36+
}
37+
38+
return $this->originalData->getClientFilename();
39+
}
3140
}

src/Storage/BunnyStorageManager.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace FileUpload\Storage;
55

6+
use FileUpload\File\PathUtils;
67
use FileUpload\File\StoredFile;
78
use FileUpload\File\StoredFileInterface;
89
use FileUpload\File\UploadedFileDecorator;
@@ -22,7 +23,10 @@ public function put(UploadedFileInterface $fileObject): UploadedFileDecorator
2223
{
2324
$accessKey = $this->getConfig('accessKey');
2425
$hostname = (!empty($this->getConfig('region'))) ? $this->getConfig('region') . '.' . $this->getConfig('baseHostName') : $this->getConfig('baseHostName');
25-
$url = $this->composeUrl($hostname, $this->getConfig('storageZone'), $this->getConfig('storageZonePath'), $fileObject->getClientFilename());
26+
27+
$fileName = PathUtils::fileNameSanitize($fileObject->getClientFilename());
28+
29+
$url = $this->composeUrl($hostname, $this->getConfig('storageZone'), $this->getConfig('storageZonePath'), $fileName);
2630

2731
$ch = curl_init();
2832

@@ -49,7 +53,8 @@ public function put(UploadedFileInterface $fileObject): UploadedFileDecorator
4953
}
5054

5155
$file = new UploadedFileDecorator($fileObject, self::STORAGE_TYPE, [
52-
'storagePath' => $this->composeUrl($this->getConfig('cdnDomain'))
56+
'storagePath' => $this->composeUrl($this->getConfig('cdnDomain'), $this->getConfig('storageZonePath')),
57+
'fileName' => $fileName,
5358
]);
5459

5560
return $file;

src/Storage/LocalStorageManager.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace FileUpload\Storage;
55

6+
use FileUpload\File\PathUtils;
67
use FileUpload\File\StoredFile;
78
use FileUpload\File\StoredFileInterface;
89
use FileUpload\File\UploadedFileDecorator;
@@ -19,10 +20,12 @@ class LocalStorageManager extends StorageManager
1920
*/
2021
public function put(UploadedFileInterface $fileObject): UploadedFileDecorator
2122
{
22-
$fileObject->moveTo($this->getConfig('storagePath') . $fileObject->getClientFilename());
23+
$fileName = PathUtils::fileNameSanitize($fileObject->getClientFilename());
24+
$fileObject->moveTo($this->getConfig('storagePath') . $fileName);
2325

2426
$uploadedFile = new UploadedFileDecorator($fileObject, self::STORAGE_TYPE, options: [
2527
'storagePath' => $this->getConfig('storagePath'),
28+
'fileName' => $fileName,
2629
]);
2730

2831
return $uploadedFile;

0 commit comments

Comments
 (0)