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\InvalidMimeTypeException; |
13
|
|
|
use UniSharp\LaravelFilemanager\LfmPath; |
14
|
|
|
|
15
|
|
|
class LfmUploadValidator |
16
|
|
|
{ |
17
|
|
|
private $file; |
18
|
|
|
|
19
|
|
|
public function __construct(UploadedFile $file) |
20
|
|
|
{ |
21
|
|
|
// if (! $file instanceof UploadedFile) { |
22
|
|
|
// throw new \Exception(trans(self::PACKAGE_NAME . '::lfm.error-instance')); |
23
|
|
|
// } |
24
|
|
|
|
25
|
|
|
$this->file = $file; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
// public function hasContent() |
29
|
|
|
// { |
30
|
|
|
// if (empty($this->file)) { |
31
|
|
|
// throw new EmptyFileException(); |
32
|
|
|
// } |
33
|
|
|
|
34
|
|
|
// return $this; |
35
|
|
|
// } |
36
|
|
|
|
37
|
|
|
public function sizeLowerThanIniMaximum() |
38
|
|
|
{ |
39
|
|
|
if ($this->file->getError() == UPLOAD_ERR_INI_SIZE) { |
40
|
|
|
throw new FileSizeExceedIniMaximumException(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function uploadWasSuccessful() |
47
|
|
|
{ |
48
|
|
|
if ($this->file->getError() != UPLOAD_ERR_OK) { |
49
|
|
|
throw new FileFailedToUploadException($this->file->getError()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function nameIsNotDuplicate($new_file_name, LfmPath $lfm_path) |
56
|
|
|
{ |
57
|
|
|
if ($lfm_path->setName($new_file_name)->exists()) { |
|
|
|
|
58
|
|
|
throw new DuplicateFileNameException(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function mimetypeIsNotExcutable($excutable_mimetypes) |
65
|
|
|
{ |
66
|
|
|
$mimetype = $this->file->getMimeType(); |
67
|
|
|
|
68
|
|
|
if (in_array($mimetype, $excutable_mimetypes)) { |
69
|
|
|
throw new ExcutableFileException(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function extensionIsNotExcutable($excutable_extensions) |
76
|
|
|
{ |
77
|
|
|
$extension = $this->file->getClientOriginalExtension(); |
78
|
|
|
|
79
|
|
|
if (in_array($extension, $excutable_extensions)) { |
80
|
|
|
throw new ExcutableFileException(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function mimeTypeIsValid($available_mime_types) |
87
|
|
|
{ |
88
|
|
|
$mimetype = $this->file->getMimeType(); |
89
|
|
|
|
90
|
|
|
if (false === in_array($mimetype, $available_mime_types)) { |
91
|
|
|
throw new InvalidMimeTypeException($mimetype); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function sizeIsLowerThanConfiguredMaximum($max_size_in_kb) |
98
|
|
|
{ |
99
|
|
|
// size to kb unit is needed |
100
|
|
|
$file_size_in_kb = $this->file->getSize() / 1000; |
101
|
|
|
|
102
|
|
|
if ($file_size_in_kb > $max_size_in_kb) { |
103
|
|
|
throw new FileSizeExceedConfigurationMaximumException($file_size_in_kb); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|