1 | <?php |
||
2 | |||
3 | namespace UniSharp\Uploadable; |
||
4 | |||
5 | use UniSharp\Uploadable\File; |
||
6 | use UniSharp\Uploadable\Image; |
||
7 | use Illuminate\Support\Facades\Config; |
||
8 | use Illuminate\Support\Facades\Storage; |
||
9 | |||
10 | class Uploader |
||
0 ignored issues
–
show
Coding Style
Documentation
introduced
by
![]() |
|||
11 | { |
||
12 | const IMAGE_MIME_TYPES = [ |
||
13 | 'image/gif', |
||
14 | 'image/jpeg', |
||
15 | 'image/jpg', |
||
16 | 'image/png' |
||
17 | ]; |
||
18 | |||
19 | 8 | public static function upload($file): File |
|
20 | { |
||
21 | 8 | $name = $file->getClientOriginalName(); |
|
22 | 8 | $mime = $file->getMimeType(); |
|
23 | 8 | $size = $file->getSize(); |
|
24 | |||
25 | switch (true) { |
||
26 | 8 | case in_array($mime, static::IMAGE_MIME_TYPES): |
|
27 | 6 | $type = 'image'; |
|
28 | 6 | $class = Image::class; |
|
29 | 6 | break; |
|
30 | |||
31 | default: |
||
32 | 2 | $type = 'file'; |
|
33 | 2 | $class = File::class; |
|
34 | 2 | break; |
|
35 | } |
||
36 | |||
37 | 8 | $path = $file->store(str_plural($type)); |
|
38 | |||
39 | 8 | foreach (Config::get("uploadable.plugins.{$type}", []) as $plugin) { |
|
40 | 4 | $storagePath = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix(); |
|
41 | 4 | $fullPath = $storagePath . DIRECTORY_SEPARATOR . $path; |
|
42 | |||
43 | 4 | (new $plugin)->handle($fullPath); |
|
44 | } |
||
45 | |||
46 | 8 | return $class::create(compact('name', 'mime', 'size', 'path')); |
|
47 | } |
||
48 | } |
||
49 |