forked from UniSharp/laravel-filemanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLfmUploadValidator.php
108 lines (83 loc) · 2.9 KB
/
LfmUploadValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace UniSharp\LaravelFilemanager;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use UniSharp\LaravelFilemanager\Exceptions\DuplicateFileNameException;
use UniSharp\LaravelFilemanager\Exceptions\EmptyFileException;
use UniSharp\LaravelFilemanager\Exceptions\ExcutableFileException;
use UniSharp\LaravelFilemanager\Exceptions\FileFailedToUploadException;
use UniSharp\LaravelFilemanager\Exceptions\FileSizeExceedConfigurationMaximumException;
use UniSharp\LaravelFilemanager\Exceptions\FileSizeExceedIniMaximumException;
use UniSharp\LaravelFilemanager\Exceptions\InvalidMimeTypeException;
use UniSharp\LaravelFilemanager\LfmPath;
class LfmUploadValidator
{
private $file;
public function __construct(UploadedFile $file)
{
// if (! $file instanceof UploadedFile) {
// throw new \Exception(trans(self::PACKAGE_NAME . '::lfm.error-instance'));
// }
$this->file = $file;
}
// public function hasContent()
// {
// if (empty($this->file)) {
// throw new EmptyFileException();
// }
// return $this;
// }
public function sizeLowerThanIniMaximum()
{
if ($this->file->getError() == UPLOAD_ERR_INI_SIZE) {
throw new FileSizeExceedIniMaximumException();
}
return $this;
}
public function uploadWasSuccessful()
{
if ($this->file->getError() != UPLOAD_ERR_OK) {
throw new FileFailedToUploadException($this->file->getError());
}
return $this;
}
public function nameIsNotDuplicate($new_file_name, LfmPath $lfm_path)
{
if ($lfm_path->setName($new_file_name)->exists()) {
throw new DuplicateFileNameException();
}
return $this;
}
public function mimetypeIsNotExcutable($excutable_mimetypes)
{
$mimetype = $this->file->getMimeType();
if (in_array($mimetype, $excutable_mimetypes)) {
throw new ExcutableFileException();
}
return $this;
}
public function extensionIsNotExcutable($excutable_extensions)
{
$extension = $this->file->getClientOriginalExtension();
if (in_array($extension, $excutable_extensions)) {
throw new ExcutableFileException();
}
return $this;
}
public function mimeTypeIsValid($available_mime_types)
{
$mimetype = $this->file->getMimeType();
if (false === in_array($mimetype, $available_mime_types)) {
throw new InvalidMimeTypeException($mimetype);
}
return $this;
}
public function sizeIsLowerThanConfiguredMaximum($max_size_in_kb)
{
// size to kb unit is needed
$file_size_in_kb = $this->file->getSize() / 1000;
if ($file_size_in_kb > $max_size_in_kb) {
throw new FileSizeExceedConfigurationMaximumException($file_size_in_kb);
}
return $this;
}
}