1 | <?php namespace Arcanesoft\Media\Http\Controllers\Admin; |
||
16 | class ApiController |
||
17 | { |
||
18 | /* ----------------------------------------------------------------- |
||
19 | | Traits |
||
20 | | ----------------------------------------------------------------- |
||
21 | */ |
||
22 | |||
23 | use JsonResponses, |
||
24 | AuthorizesRequests; |
||
25 | |||
26 | /* ----------------------------------------------------------------- |
||
27 | | Properties |
||
28 | | ----------------------------------------------------------------- |
||
29 | */ |
||
30 | |||
31 | /** |
||
32 | * The media instance. |
||
33 | * |
||
34 | * @var \Arcanesoft\Media\Contracts\Media |
||
35 | */ |
||
36 | protected $media; |
||
37 | |||
38 | /* ----------------------------------------------------------------- |
||
39 | | Constructor |
||
40 | | ----------------------------------------------------------------- |
||
41 | */ |
||
42 | |||
43 | /** |
||
44 | * ApiController constructor. |
||
45 | * |
||
46 | * @param \Arcanesoft\Media\Contracts\Media $media |
||
47 | */ |
||
48 | 34 | public function __construct(Media $media) |
|
52 | |||
53 | /* ----------------------------------------------------------------- |
||
54 | | Main Methods |
||
55 | | ----------------------------------------------------------------- |
||
56 | */ |
||
57 | |||
58 | /** |
||
59 | * Get the the media files. |
||
60 | * |
||
61 | * @param \Illuminate\Http\Request $request |
||
62 | * |
||
63 | * @return \Illuminate\Http\JsonResponse |
||
64 | */ |
||
65 | 4 | public function getAll(Request $request) |
|
66 | { |
||
67 | 4 | $this->authorize(MediasPolicy::PERMISSION_LIST); |
|
68 | |||
69 | 4 | return $this->jsonResponseSuccess([ |
|
70 | 4 | 'medias' => $this->media->all( |
|
71 | 4 | $request->get('location', '/') |
|
72 | ), |
||
73 | ]); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Upload a media file. |
||
78 | * |
||
79 | * @param \Illuminate\Http\Request $request |
||
80 | * |
||
81 | * @return \Illuminate\Http\JsonResponse |
||
82 | */ |
||
83 | 4 | public function uploadMedia(Request $request) |
|
84 | { |
||
85 | 4 | $this->authorize(MediasPolicy::PERMISSION_CREATE); |
|
86 | |||
87 | // TODO: Refactor this with the Laravel 5.5 new Exception Handler & Form Request |
||
88 | 4 | $validator = validator($request->all(), [ |
|
89 | 4 | 'location' => ['required', 'string'], |
|
90 | 'medias' => ['required', 'array'], |
||
91 | 'medias.*' => ['required', 'file'] |
||
92 | ]); |
||
93 | |||
94 | 4 | if ($validator->fails()) { |
|
95 | 2 | return $this->jsonResponseError([ |
|
96 | 2 | 'messages' => $validator->messages(), |
|
97 | 2 | ], 422); |
|
98 | } |
||
99 | |||
100 | 2 | $uploaded = $this->media->storeMany( |
|
101 | 2 | $request->get('location'), $request->file('medias') |
|
|
|||
102 | ); |
||
103 | |||
104 | 2 | return $this->jsonResponseSuccess([ |
|
105 | 2 | 'data' => compact('uploaded') |
|
106 | ]); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Create a directory. |
||
111 | * |
||
112 | * @param \Illuminate\Http\Request $request |
||
113 | * |
||
114 | * @return \Illuminate\Http\JsonResponse |
||
115 | */ |
||
116 | 4 | public function createDirectory(Request $request) |
|
117 | { |
||
118 | 4 | $this->authorize(MediasPolicy::PERMISSION_CREATE); |
|
119 | |||
120 | // TODO: Refactor this with the Laravel 5.5 new Exception Handler & Form Request |
||
121 | 4 | $validator = validator($data = $request->all(), [ |
|
122 | 4 | 'name' => ['required', 'string'], // TODO: check if the folder does not exists |
|
123 | 'location' => ['required', 'string'], |
||
124 | ]); |
||
125 | |||
126 | 4 | if ($validator->fails()) { |
|
127 | 2 | return $this->jsonResponseError([ |
|
128 | 2 | 'messages' => $validator->messages(), |
|
129 | 2 | ], 422); |
|
130 | } |
||
131 | |||
132 | 2 | $this->media->makeDirectory( |
|
133 | 2 | $path = trim($data['location'], '/').'/'.Str::slug($data['name']) |
|
134 | ); |
||
135 | |||
136 | 2 | return $this->jsonResponseSuccess(['data' => compact('path')]); |
|
137 | } |
||
138 | |||
139 | 8 | public function renameMedia(Request $request) |
|
140 | { |
||
141 | 8 | $this->authorize(MediasPolicy::PERMISSION_UPDATE); |
|
142 | |||
143 | // TODO: Refactor this with the Laravel 5.5 new Exception Handler & Form Request |
||
144 | // TODO: Check if the folder does not exists |
||
145 | 8 | $validator = validator($data = $request->all(), [ |
|
146 | 8 | 'media' => ['required', 'array'], |
|
147 | 'media.type' => ['required', 'string'], |
||
148 | 'media.name' => ['required', 'string'], |
||
149 | 'newName' => ['required', 'string'], |
||
150 | 'location' => ['required', 'string'], |
||
151 | ]); |
||
152 | |||
153 | 8 | if ($validator->fails()) { |
|
154 | 2 | return $this->jsonResponseError([ |
|
155 | 2 | 'messages' => $validator->messages(), |
|
156 | 2 | ], 422); |
|
157 | } |
||
158 | |||
159 | 6 | $path = $this->performMoveMedia( |
|
160 | 6 | $data['media']['type'], |
|
161 | 6 | trim($data['location'], '/'), |
|
162 | 6 | $data['media']['name'], |
|
163 | 6 | $data['newName'] |
|
164 | ); |
||
165 | |||
166 | 6 | if ($path === false) { |
|
167 | 2 | return $this->jsonResponseError([ |
|
168 | 2 | 'message' => 'Something wrong was happened while renaming the media.', |
|
169 | 2 | ], 500); |
|
170 | } |
||
171 | 4 | return $this->jsonResponseSuccess([ |
|
172 | 4 | 'data' => compact('path'), |
|
173 | ]); |
||
174 | } |
||
175 | |||
176 | 6 | public function deleteMedia(Request $request) |
|
177 | { |
||
178 | 6 | $this->authorize(MediasPolicy::PERMISSION_DELETE); |
|
179 | |||
180 | // TODO: Refactor this with the Laravel 5.5 new Exception Handler & Form Request |
||
181 | 6 | $validator = validator($data = $request->all(), [ |
|
182 | 6 | 'media' => ['required', 'array'], |
|
183 | 'media.type' => ['required', 'string'], |
||
184 | 'media.path' => ['required', 'string'], |
||
185 | ]); |
||
186 | |||
187 | 6 | if ($validator->fails()) { |
|
188 | 2 | return $this->jsonResponseError([ |
|
189 | 2 | 'messages' => $validator->messages(), |
|
190 | 2 | ], 422); |
|
191 | } |
||
192 | |||
193 | 4 | return $this->performDeleteMedia($data['media']['type'], $data['media']['path']) |
|
194 | 4 | ? $this->jsonResponseSuccess() |
|
195 | 4 | : $this->jsonResponseError(); |
|
196 | } |
||
197 | |||
198 | 2 | public function moveLocations(Request $request) |
|
199 | { |
||
200 | 2 | $this->authorize(MediasPolicy::PERMISSION_UPDATE); |
|
201 | |||
202 | // TODO: Adding validation ? |
||
203 | 2 | $destinations = $this->getDestinations( |
|
204 | 2 | $request->get('name'), |
|
205 | 2 | $request->get('location', '/') |
|
206 | ); |
||
207 | |||
208 | 2 | return $this->jsonResponseSuccess(compact('destinations')); |
|
209 | } |
||
210 | |||
211 | 4 | public function moveMedia(Request $request) |
|
212 | { |
||
213 | 4 | $this->authorize(MediasPolicy::PERMISSION_UPDATE); |
|
214 | |||
215 | // TODO: Refactor this with the Laravel 5.5 new Exception Handler & Form Request |
||
216 | 4 | $validator = validator($data = $request->all(), [ |
|
217 | 4 | 'old_path' => ['required', 'string'], |
|
218 | 'new_path' => ['required', 'string'], |
||
219 | ]); |
||
220 | |||
221 | 4 | if ($validator->fails()) { |
|
222 | 2 | return $this->jsonResponseError([ |
|
223 | 2 | 'messages' => $validator->messages(), |
|
224 | 2 | ], 422); |
|
225 | } |
||
226 | |||
227 | 2 | return $this->media->move($data['old_path'], $data['new_path']) |
|
228 | 2 | ? $this->jsonResponseSuccess() |
|
229 | 2 | : $this->jsonResponseError(['message' => 'Something wrong happened !'], 500); |
|
230 | } |
||
231 | |||
232 | /* ----------------------------------------------------------------- |
||
233 | | Other Methods |
||
234 | | ----------------------------------------------------------------- |
||
235 | */ |
||
236 | |||
237 | /** |
||
238 | * Get the destinations paths. |
||
239 | * |
||
240 | * @param string $name |
||
241 | * @param string $location |
||
242 | * |
||
243 | * @return \Arcanesoft\Media\Entities\DirectoryCollection |
||
244 | */ |
||
245 | 2 | private function getDestinations($name, $location) |
|
260 | |||
261 | /** |
||
262 | * Perform the media movement. |
||
263 | * |
||
264 | * @param string $type |
||
265 | * @param string $location |
||
266 | * @param string $oldName |
||
267 | * @param string $newName |
||
268 | * |
||
269 | * @return bool|string |
||
270 | */ |
||
271 | 6 | private function performMoveMedia($type, $location, $oldName, $newName) |
|
272 | { |
||
273 | 6 | $from = "{$location}/{$oldName}"; |
|
274 | |||
275 | 6 | switch (Str::lower($type)) { |
|
276 | 6 | case Media::MEDIA_TYPE_FILE: |
|
277 | 2 | return $this->moveFile($from, $location, $newName); |
|
278 | |||
279 | 4 | case Media::MEDIA_TYPE_DIRECTORY: |
|
280 | 2 | return $this->moveDirectory($from, $location, $newName); |
|
281 | |||
282 | default: |
||
283 | 2 | return false; |
|
284 | } |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Move file. |
||
289 | * |
||
290 | * @param string $location |
||
291 | * @param string $location |
||
292 | * @param string $from |
||
293 | * @param string $newName |
||
294 | * |
||
295 | * @return string |
||
296 | */ |
||
297 | 2 | private function moveFile($from, $location, $newName) |
|
306 | |||
307 | /** |
||
308 | * Move directory. |
||
309 | * |
||
310 | * @param string $from |
||
311 | * @param string $location |
||
312 | * @param string $newName |
||
313 | * |
||
314 | * @return string |
||
315 | */ |
||
316 | 2 | private function moveDirectory($from, $location, $newName) |
|
324 | |||
325 | /** |
||
326 | * Perform the media deletion. |
||
327 | * |
||
328 | * @param string $type |
||
329 | * @param string $path |
||
330 | * |
||
331 | * @return bool |
||
332 | */ |
||
333 | 4 | private function performDeleteMedia($type, $path) |
|
346 | } |
||
347 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.