|
4 | 4 |
|
5 | 5 | namespace Tamedevelopers\File\Traits; |
6 | 6 |
|
| 7 | +use Tamedevelopers\Support\Capsule\Forge; |
7 | 8 | use Tamedevelopers\Support\Tame; |
8 | 9 |
|
9 | 10 | /** |
@@ -42,107 +43,101 @@ private function setDirectory($path = null) |
42 | 43 | * |
43 | 44 | * @param array $message |
44 | 45 | * @param array $config |
45 | | - * @param array $filterError |
46 | 46 | * @return void |
47 | 47 | */ |
48 | | - public function globalConfig($message = [], $config = [], $filterError = []) |
| 48 | + public function globalConfig($message = [], $config = []) |
49 | 49 | { |
50 | | - // create message |
51 | | - $message = $message + [ |
52 | | - '401' => 'Select file to upload', |
53 | | - '402' => 'File upload is greater than allowed size of:', |
54 | | - '403' => 'Maximum file upload exceeded. Limit is:', |
55 | | - '404' => 'Uploaded file format not allowed. Allowed formats:', |
56 | | - '405' => 'Image dimension allowed is:', |
57 | | - '405x' => 'Image dimension should be greater than or equal to:', |
58 | | - '200' => 'File uploaded successfully:', |
59 | | - 'kb' => 'kb', |
60 | | - 'mb' => 'mb', |
61 | | - 'gb' => 'gb', |
62 | | - 'and' => 'and', |
63 | | - 'width' => 'width', |
64 | | - 'height'=> 'height', |
65 | | - 'files' => 'files', |
66 | | - 'file' => 'file', |
67 | | - ]; |
68 | | - |
69 | | - // create config |
70 | | - $config = array_merge([ |
71 | | - 'limit' => 1, |
72 | | - 'size' => 2097152, // 2mb |
73 | | - 'mime' => 'images', // video|audio|files|images|general_image|general_media|general_file |
74 | | - 'baseUrl' => domain(), |
75 | | - 'baseDir' => base_path(), |
76 | | - 'driver' => 'local', // local|s3 |
77 | | - 'structure' => 'default', // default|year|month|day |
78 | | - 'generate' => true, // will always generate a unique() name for each uploaded file |
79 | | - ], $config); |
80 | | - |
81 | | - |
82 | | - // Convert size to Bytes |
83 | | - $config['size'] = Tame::sizeToBytes( |
84 | | - !empty($config['size']) && (int) $config['size'] >= 1024 |
85 | | - ? Tame::byteToUnit($config['size']) |
86 | | - : $config['size'] ?? '2mb' |
87 | | - ); |
88 | | - |
89 | | - // create filterError |
90 | | - $filterError = array_merge([], $filterError); |
91 | | - |
92 | | - // trim any leading '\/' and manually add by ourselves |
93 | | - // this enable to make sure, paths are with a leading '/' |
94 | | - $config['baseDir'] = trim((string) $config['baseDir'], '\/') . '/'; |
95 | | - $config['baseUrl'] = trim((string) $config['baseUrl'], '\/') . '/'; |
96 | | - |
97 | | - // check for valid driver type |
98 | | - // only change the default driver if found |
99 | | - if(in_array($config['driver'], array_keys($this->driverTypes))){ |
100 | | - $config['driver'] = $config['driver']; |
101 | | - } |
102 | | - |
103 | | - // create default data |
104 | | - $default = [ |
105 | | - 'message' => $message, |
106 | | - 'config' => $config, |
107 | | - 'filterError' => $filterError, |
108 | | - ]; |
| 50 | + // define constant to hold global error handler |
| 51 | + if(!defined('TAME_FILE_ERROR')){ |
109 | 52 |
|
110 | | - // if filter error is defined |
111 | | - if(!empty($default['filterError'])){ |
| 53 | + // create message |
| 54 | + $message = $message + [ |
| 55 | + '401' => 'Select file to upload', |
| 56 | + '402' => 'File upload is greater than allowed size of:', |
| 57 | + '403' => 'Maximum file upload exceeded. Limit is:', |
| 58 | + '404' => 'Uploaded file format not allowed. Allowed formats:', |
| 59 | + '405' => 'Image dimension allowed is:', |
| 60 | + '405x' => 'Image dimension should be greater than or equal to:', |
| 61 | + '200' => 'File uploaded successfully:', |
| 62 | + 'kb' => 'kb', |
| 63 | + 'mb' => 'mb', |
| 64 | + 'gb' => 'gb', |
| 65 | + 'and' => 'and', |
| 66 | + 'width' => 'width', |
| 67 | + 'height'=> 'height', |
| 68 | + 'files' => 'files', |
| 69 | + 'file' => 'file', |
| 70 | + ]; |
| 71 | + |
| 72 | + // create config |
| 73 | + $config = array_merge([ |
| 74 | + 'limit' => 1, |
| 75 | + 'size' => 2097152, // 2mb |
| 76 | + 'mime' => 'images', // video|audio|files|images|general_image|general_media|general_file |
| 77 | + 'baseUrl' => domain(), |
| 78 | + 'baseDir' => base_path(), |
| 79 | + 'driver' => 'local', // local|s3 |
| 80 | + 'structure' => 'default', // default|year|month|day |
| 81 | + 'generate' => true, // will always generate a unique() name for each uploaded file |
| 82 | + ], $config); |
| 83 | + |
| 84 | + |
| 85 | + // Convert size to Bytes |
| 86 | + $config['size'] = Tame::sizeToBytes( |
| 87 | + !empty($config['size']) && (int) $config['size'] >= 1024 |
| 88 | + ? Tame::byteToUnit($config['size']) |
| 89 | + : $config['size'] ?? '2mb' |
| 90 | + ); |
| 91 | + |
| 92 | + // trim any leading '\/' and manually add by ourselves |
| 93 | + // this enable to make sure, paths are with a leading '/' |
| 94 | + $config['baseDir'] = trim((string) $config['baseDir'], '\/') . '/'; |
| 95 | + $config['baseUrl'] = trim((string) $config['baseUrl'], '\/') . '/'; |
| 96 | + |
| 97 | + // check for valid driver type |
| 98 | + // only change the default driver if found |
| 99 | + if(in_array($config['driver'], array_keys($this->driverTypes))){ |
| 100 | + $config['driver'] = $config['driver']; |
| 101 | + } |
112 | 102 |
|
113 | | - // filter error to be remove from parent $this->error |
114 | | - $this->filterError($default['filterError']); |
115 | | - } |
| 103 | + // create default data |
| 104 | + $default = [ |
| 105 | + 'message' => $message, |
| 106 | + 'config' => $config, |
| 107 | + ]; |
116 | 108 |
|
117 | | - // define constant to hold global error handler |
118 | | - if(!defined('TAME_FILE_ERROR')){ |
119 | 109 | define('TAME_FILE_ERROR', $default); |
120 | 110 | } |
121 | 111 | } |
122 | 112 |
|
123 | 113 | /** |
124 | | - * Filter error to remove unwanted error response |
| 114 | + * Remove unwanted Filter response status code |
125 | 115 | * |
126 | | - * @param array $errorDisallowed |
127 | | - * 401 => ERROR_401 - Select file to upload", |
128 | | - * 402 => ERROR_402 - File upload is greater than allowed size of", |
129 | | - * 403 => ERROR_403 - Maximum file upload exceeded. Limit is:", |
130 | | - * 404 => ERROR_404 - Uploaded file format not allowed. Allowed format is:", |
131 | | - * 405 => ERROR_405 - Image size allowed error" |
132 | | - * 200 => ERROR_200 - File uploaded successfully" |
| 116 | + * @param array $filter |
| 117 | + * Possible status code: [401, 402, 403, 404, 405, 200] |
| 118 | + * |
| 119 | + * 401 => Select file to upload", |
| 120 | + * 402 => File upload is greater than allowed size of", |
| 121 | + * 403 => Maximum file upload exceeded. Limit is:", |
| 122 | + * 404 => Uploaded file format not allowed. Allowed format is:", |
| 123 | + * 405 => Image size allowed error" |
| 124 | + * 200 => File uploaded successfully" |
133 | 125 | * |
134 | 126 | * @return $this |
135 | 127 | */ |
136 | | - public function filterError($errorDisallowed = []) |
| 128 | + public function filter(...$filter) |
137 | 129 | { |
138 | | - if(is_array($errorDisallowed) && count($errorDisallowed) > 0){ |
| 130 | + // flattern all into one array element |
| 131 | + $filter = Forge::flattenValue($filter); |
| 132 | + |
| 133 | + if(is_array($filter) && count($filter) > 0){ |
139 | 134 |
|
140 | | - foreach($errorDisallowed as $value){ |
| 135 | + foreach($filter as $value){ |
141 | 136 | // convert to int values |
142 | 137 | $value = (int) $value; |
143 | 138 |
|
144 | 139 | // if in error array keys |
145 | | - if(in_array($value, array_keys($this->error) )){ |
| 140 | + if( in_array($value, array_keys($this->error)) && $value !== 200 ){ |
146 | 141 | unset($this->error[$value]); |
147 | 142 | } |
148 | 143 | } |
|
0 commit comments