1 | <?php |
||||||
2 | |||||||
3 | namespace Owowagency\LaravelMedia\Managers; |
||||||
4 | |||||||
5 | use Mimey\MimeTypes; |
||||||
6 | use Illuminate\Support\Arr; |
||||||
7 | use Spatie\MediaLibrary\HasMedia; |
||||||
8 | use Owowagency\LaravelMedia\Rules\IsBase64; |
||||||
9 | use Spatie\MediaLibrary\MediaCollections\Models\Media; |
||||||
10 | use Owowagency\LaravelMedia\Exceptions\UploadException; |
||||||
11 | use Owowagency\LaravelMedia\Rules\Concerns\ValidatesBase64; |
||||||
12 | |||||||
13 | class MediaManager |
||||||
14 | { |
||||||
15 | use ValidatesBase64; |
||||||
16 | |||||||
17 | /** |
||||||
18 | * Upload media without specifying the type. Currently this method can |
||||||
19 | * upload a string or an array of strings (base64 string). |
||||||
20 | * |
||||||
21 | * @param \Spatie\MediaLibrary\HasMedia $model |
||||||
22 | * @param string|array $media |
||||||
23 | * @param string|null $name |
||||||
24 | * @param string $collection |
||||||
25 | * @return Media[] |
||||||
26 | * |
||||||
27 | * @throws \Owowagency\LaravelMedia\Exceptions\UploadException |
||||||
28 | */ |
||||||
29 | public static function upload( |
||||||
30 | HasMedia $model, |
||||||
31 | $media, |
||||||
32 | string $name = null, |
||||||
33 | string $collection = 'default' |
||||||
34 | ): array { |
||||||
35 | if (is_array($media)) { |
||||||
36 | $uploads = []; |
||||||
37 | |||||||
38 | if (Arr::isAssoc($media)) { |
||||||
39 | return static::upload( |
||||||
40 | $model, |
||||||
41 | ...static::getUploadParams($media), |
||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
42 | ); |
||||||
43 | } |
||||||
44 | |||||||
45 | foreach ($media as $value) { |
||||||
46 | $uploads[] = static::upload($model, $value, $name, $collection); |
||||||
47 | } |
||||||
48 | |||||||
49 | return Arr::flatten($uploads); |
||||||
50 | } else { |
||||||
51 | if (is_string($media)) { |
||||||
0 ignored issues
–
show
|
|||||||
52 | return [static::uploadFromString($model, $media, $name, $collection)]; |
||||||
53 | } |
||||||
54 | } |
||||||
55 | |||||||
56 | throw new UploadException(gettype($media)); |
||||||
57 | } |
||||||
58 | |||||||
59 | /** |
||||||
60 | * Check if the given string is base64. If so, upload |
||||||
61 | * the base64 string. |
||||||
62 | * |
||||||
63 | * @param \Spatie\MediaLibrary\HasMedia $model |
||||||
64 | * @param string $string |
||||||
65 | * @param string|null $name |
||||||
66 | * @param string $collection |
||||||
67 | * @return \Spatie\MediaLibrary\MediaCollections\Models\Media |
||||||
68 | * |
||||||
69 | * @throws \Owowagency\LaravelMedia\Exceptions\UploadException |
||||||
70 | */ |
||||||
71 | public static function uploadFromString( |
||||||
72 | HasMedia $model, |
||||||
73 | string $string, |
||||||
74 | $name = null, |
||||||
75 | string $collection = 'default' |
||||||
76 | ): Media { |
||||||
77 | $base64Rule = new IsBase64(); |
||||||
78 | |||||||
79 | if ($base64Rule->passes('', $string)) { |
||||||
80 | return static::uploadFromBase64($model, $string, $name, $collection); |
||||||
81 | } elseif (filter_var($string, FILTER_VALIDATE_URL) !== false) { |
||||||
82 | return static::uploadFromUrl($model, $string, $collection); |
||||||
83 | } |
||||||
84 | |||||||
85 | throw new UploadException(); |
||||||
86 | } |
||||||
87 | |||||||
88 | /** |
||||||
89 | * Saves base64 media to file and adds to collection. |
||||||
90 | * |
||||||
91 | * @param \Spatie\MediaLibrary\HasMedia $model |
||||||
92 | * @param string $string |
||||||
93 | * @param string|null $name |
||||||
94 | * @param string $collection |
||||||
95 | * @return \Spatie\MediaLibrary\MediaCollections\Models\Media |
||||||
96 | */ |
||||||
97 | public static function uploadFromBase64( |
||||||
98 | HasMedia $model, |
||||||
99 | string $string, |
||||||
100 | $name = null, |
||||||
101 | string $collection = 'default' |
||||||
102 | ): Media { |
||||||
103 | $fileAdder = $model->addMediaFromBase64($string); |
||||||
0 ignored issues
–
show
The method
addMediaFromBase64() does not exist on Spatie\MediaLibrary\HasMedia . Did you maybe mean addMedia() ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
104 | |||||||
105 | preg_match('/data\:\w+\/(.*?)\;/s', $string, $extension); |
||||||
106 | |||||||
107 | if (! is_null($name)) { |
||||||
108 | if (empty($extension)) { |
||||||
109 | $format = '%s.%s'; |
||||||
110 | |||||||
111 | $mimes = new MimeTypes(); |
||||||
112 | |||||||
113 | $extension[1] = $mimes->getExtension(static::getMimeType($string)); |
||||||
114 | } else { |
||||||
115 | $format = '%s.%s'; |
||||||
116 | } |
||||||
117 | |||||||
118 | $fileAdder->usingName($name) |
||||||
119 | ->usingFileName( |
||||||
120 | sprintf( |
||||||
121 | $format, |
||||||
122 | $name, |
||||||
123 | $extension[1], |
||||||
124 | ) |
||||||
125 | ); |
||||||
126 | } |
||||||
127 | |||||||
128 | $media = $fileAdder->toMediaCollection($collection); |
||||||
129 | |||||||
130 | return $media; |
||||||
131 | } |
||||||
132 | |||||||
133 | /** |
||||||
134 | * Saves url media to file and adds to collection. |
||||||
135 | * |
||||||
136 | * @param \Spatie\MediaLibrary\HasMedia $model |
||||||
137 | * @param string $string |
||||||
138 | * @param string $collection |
||||||
139 | * @return \Spatie\MediaLibrary\MediaCollections\Models\Media |
||||||
140 | */ |
||||||
141 | public static function uploadFromUrl( |
||||||
142 | HasMedia $model, |
||||||
143 | string $string, |
||||||
144 | string $collection = 'default' |
||||||
145 | ): Media { |
||||||
146 | return $model->addMediaFromUrl($string) |
||||||
0 ignored issues
–
show
The method
addMediaFromUrl() does not exist on Spatie\MediaLibrary\HasMedia . Did you maybe mean addMedia() ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
147 | ->toMediaCollection($collection); |
||||||
148 | } |
||||||
149 | |||||||
150 | /** |
||||||
151 | * Returns an array that matches the parameters needed to upload. |
||||||
152 | * |
||||||
153 | * @param array $data |
||||||
154 | * @return array |
||||||
155 | */ |
||||||
156 | public static function getUploadParams(array $data): array |
||||||
157 | { |
||||||
158 | return array_values(Arr::only( |
||||||
159 | $data, |
||||||
160 | ['file', 'name', 'collection'], |
||||||
161 | )); |
||||||
162 | } |
||||||
163 | } |
||||||
164 |