UniSharp /
laravel-filemanager
| 1 | <?php |
||
| 2 | |||
| 3 | namespace UniSharp\LaravelFilemanager; |
||
| 4 | |||
| 5 | use Symfony\Component\HttpFoundation\File\UploadedFile; |
||
| 6 | use UniSharp\LaravelFilemanager\Exceptions\DuplicateFileNameException; |
||
| 7 | use UniSharp\LaravelFilemanager\Exceptions\EmptyFileException; |
||
| 8 | use UniSharp\LaravelFilemanager\Exceptions\ExcutableFileException; |
||
| 9 | use UniSharp\LaravelFilemanager\Exceptions\FileFailedToUploadException; |
||
| 10 | use UniSharp\LaravelFilemanager\Exceptions\FileSizeExceedConfigurationMaximumException; |
||
| 11 | use UniSharp\LaravelFilemanager\Exceptions\FileSizeExceedIniMaximumException; |
||
| 12 | use UniSharp\LaravelFilemanager\Exceptions\InvalidExtensionException; |
||
| 13 | use UniSharp\LaravelFilemanager\Exceptions\InvalidMimeTypeException; |
||
| 14 | use UniSharp\LaravelFilemanager\LfmPath; |
||
| 15 | |||
| 16 | class LfmUploadValidator |
||
| 17 | { |
||
| 18 | private $file; |
||
| 19 | |||
| 20 | public function __construct(UploadedFile $file) |
||
| 21 | { |
||
| 22 | // if (! $file instanceof UploadedFile) { |
||
| 23 | // throw new \Exception(trans(self::PACKAGE_NAME . '::lfm.error-instance')); |
||
| 24 | // } |
||
| 25 | |||
| 26 | $this->file = $file; |
||
| 27 | } |
||
| 28 | |||
| 29 | // public function hasContent() |
||
| 30 | // { |
||
| 31 | // if (empty($this->file)) { |
||
| 32 | // throw new EmptyFileException(); |
||
| 33 | // } |
||
| 34 | |||
| 35 | // return $this; |
||
| 36 | // } |
||
| 37 | |||
| 38 | public function sizeLowerThanIniMaximum() |
||
| 39 | { |
||
| 40 | if ($this->file->getError() == UPLOAD_ERR_INI_SIZE) { |
||
| 41 | throw new FileSizeExceedIniMaximumException(); |
||
| 42 | } |
||
| 43 | |||
| 44 | return $this; |
||
| 45 | } |
||
| 46 | |||
| 47 | public function uploadWasSuccessful() |
||
| 48 | { |
||
| 49 | if ($this->file->getError() != UPLOAD_ERR_OK) { |
||
| 50 | throw new FileFailedToUploadException($this->file->getError()); |
||
| 51 | } |
||
| 52 | |||
| 53 | return $this; |
||
| 54 | } |
||
| 55 | |||
| 56 | public function nameIsNotDuplicate($new_file_name, LfmPath $lfm_path) |
||
| 57 | { |
||
| 58 | if ($lfm_path->setName($new_file_name)->exists()) { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 59 | throw new DuplicateFileNameException(); |
||
| 60 | } |
||
| 61 | |||
| 62 | return $this; |
||
| 63 | } |
||
| 64 | |||
| 65 | public function mimetypeIsNotExcutable($excutable_mimetypes) |
||
| 66 | { |
||
| 67 | $mimetype = $this->file->getMimeType(); |
||
| 68 | |||
| 69 | if (in_array($mimetype, $excutable_mimetypes)) { |
||
| 70 | throw new ExcutableFileException(); |
||
| 71 | } |
||
| 72 | |||
| 73 | return $this; |
||
| 74 | } |
||
| 75 | |||
| 76 | public function extensionIsNotExcutable() |
||
| 77 | { |
||
| 78 | $extension = strtolower($this->file->getClientOriginalExtension()); |
||
| 79 | |||
| 80 | $excutable_extensions = ['php', 'html']; |
||
| 81 | |||
| 82 | if (in_array($extension, $excutable_extensions)) { |
||
| 83 | throw new ExcutableFileException(); |
||
| 84 | } |
||
| 85 | |||
| 86 | if (strpos($extension, 'php') === 0) { |
||
| 87 | throw new ExcutableFileException(); |
||
| 88 | } |
||
| 89 | |||
| 90 | if (preg_match('/[a-z]html/', $extension) > 0) { |
||
| 91 | throw new ExcutableFileException(); |
||
| 92 | } |
||
| 93 | |||
| 94 | return $this; |
||
| 95 | } |
||
| 96 | |||
| 97 | public function mimeTypeIsValid($available_mime_types) |
||
| 98 | { |
||
| 99 | $mimetype = $this->file->getMimeType(); |
||
| 100 | |||
| 101 | if (false === in_array($mimetype, $available_mime_types)) { |
||
| 102 | throw new InvalidMimeTypeException($mimetype); |
||
| 103 | } |
||
| 104 | |||
| 105 | return $this; |
||
| 106 | } |
||
| 107 | |||
| 108 | public function extensionIsValid($disallowed_extensions) |
||
| 109 | { |
||
| 110 | $extension = strtolower($this->file->getClientOriginalExtension()); |
||
| 111 | |||
| 112 | if (preg_match('/[^a-zA-Z0-9]/', $extension) > 0) { |
||
| 113 | throw new InvalidExtensionException(); |
||
| 114 | } |
||
| 115 | |||
| 116 | if (in_array($extension, $disallowed_extensions)) { |
||
| 117 | throw new InvalidExtensionException(); |
||
| 118 | } |
||
| 119 | |||
| 120 | return $this; |
||
| 121 | } |
||
| 122 | |||
| 123 | public function sizeIsLowerThanConfiguredMaximum($max_size_in_kb) |
||
| 124 | { |
||
| 125 | // size to kb unit is needed |
||
| 126 | $file_size_in_kb = $this->file->getSize() / 1000; |
||
| 127 | |||
| 128 | if ($file_size_in_kb > $max_size_in_kb) { |
||
| 129 | throw new FileSizeExceedConfigurationMaximumException($file_size_in_kb); |
||
| 130 | } |
||
| 131 | |||
| 132 | return $this; |
||
| 133 | } |
||
| 134 | } |
||
| 135 |