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()) { |
|
|
|
|
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($excutable_extensions) |
77
|
|
|
{ |
78
|
|
|
$extension = strtolower($this->file->getClientOriginalExtension()); |
79
|
|
|
|
80
|
|
|
if (in_array($extension, $excutable_extensions)) { |
81
|
|
|
throw new ExcutableFileException(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function mimeTypeIsValid($available_mime_types) |
88
|
|
|
{ |
89
|
|
|
$mimetype = $this->file->getMimeType(); |
90
|
|
|
|
91
|
|
|
if (false === in_array($mimetype, $available_mime_types)) { |
92
|
|
|
throw new InvalidMimeTypeException($mimetype); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function extensionIsValid() |
99
|
|
|
{ |
100
|
|
|
$extension = strtolower($this->file->getClientOriginalExtension()); |
101
|
|
|
|
102
|
|
|
if (preg_match('/[^a-zA-Z0-9]/', $extension) > 0) { |
103
|
|
|
throw new InvalidExtensionException(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function sizeIsLowerThanConfiguredMaximum($max_size_in_kb) |
110
|
|
|
{ |
111
|
|
|
// size to kb unit is needed |
112
|
|
|
$file_size_in_kb = $this->file->getSize() / 1000; |
113
|
|
|
|
114
|
|
|
if ($file_size_in_kb > $max_size_in_kb) { |
115
|
|
|
throw new FileSizeExceedConfigurationMaximumException($file_size_in_kb); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|