1
|
|
|
<?php namespace Arcanesoft\Media\Http\Controllers\Admin; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelApiHelper\Traits\JsonResponses; |
4
|
|
|
use Arcanesoft\Media\Contracts\Media; |
5
|
|
|
use Arcanesoft\Media\Policies\MediasPolicy; |
6
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ApiController |
12
|
|
|
* |
13
|
|
|
* @package Arcanesoft\Media\Http\Controllers\Admin |
14
|
|
|
* @author ARCANEDEV <[email protected]> |
15
|
|
|
*/ |
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) |
49
|
|
|
{ |
50
|
34 |
|
$this->media = $media; |
51
|
34 |
|
} |
52
|
|
|
|
53
|
|
|
/* ----------------------------------------------------------------- |
54
|
|
|
| Main Methods |
55
|
|
|
| ----------------------------------------------------------------- |
56
|
|
|
*/ |
57
|
|
|
/** |
58
|
|
|
* Get the the media files. |
59
|
|
|
* |
60
|
|
|
* @param \Illuminate\Http\Request $request |
61
|
|
|
* |
62
|
|
|
* @return \Illuminate\Http\JsonResponse |
63
|
|
|
*/ |
64
|
4 |
|
public function getAll(Request $request) |
65
|
|
|
{ |
66
|
4 |
|
$this->authorize(MediasPolicy::PERMISSION_LIST); |
67
|
|
|
|
68
|
4 |
|
$location = $request->get('location', '/'); |
69
|
|
|
|
70
|
4 |
|
return $this->jsonResponseSuccess([ |
71
|
4 |
|
'medias' => $this->media->all($location), |
72
|
|
|
]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Upload a media file. |
77
|
|
|
* |
78
|
|
|
* @param \Illuminate\Http\Request $request |
79
|
|
|
* |
80
|
|
|
* @return \Illuminate\Http\JsonResponse |
81
|
|
|
*/ |
82
|
4 |
|
public function uploadMedia(Request $request) |
83
|
|
|
{ |
84
|
4 |
|
$this->authorize(MediasPolicy::PERMISSION_CREATE); |
85
|
|
|
|
86
|
4 |
|
$validator = validator($request->all(), [ |
87
|
4 |
|
'location' => ['required', 'string'], |
88
|
|
|
'medias' => ['required', 'array'], |
89
|
|
|
'medias.*' => ['required', 'file'] |
90
|
|
|
]); |
91
|
|
|
|
92
|
4 |
|
if ($validator->fails()) { |
93
|
2 |
|
return $this->jsonResponseError([ |
94
|
2 |
|
'messages' => $validator->messages(), |
95
|
2 |
|
], 422); |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
$uploaded = $this->media->storeMany( |
99
|
2 |
|
$request->get('location'), $request->file('medias') |
100
|
|
|
); |
101
|
|
|
|
102
|
2 |
|
return $this->jsonResponseSuccess([ |
103
|
2 |
|
'data' => compact('uploaded') |
104
|
|
|
]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Create a directory. |
109
|
|
|
* |
110
|
|
|
* @param \Illuminate\Http\Request $request |
111
|
|
|
* |
112
|
|
|
* @return \Illuminate\Http\JsonResponse |
113
|
|
|
*/ |
114
|
4 |
|
public function createDirectory(Request $request) |
115
|
|
|
{ |
116
|
4 |
|
$this->authorize(MediasPolicy::PERMISSION_CREATE); |
117
|
|
|
|
118
|
4 |
|
$data = $request->all(); |
119
|
4 |
|
$validator = validator($data, [ |
120
|
4 |
|
'name' => ['required', 'string'], // TODO: check if the folder does not exists |
121
|
|
|
'location' => ['required', 'string'], |
122
|
|
|
]); |
123
|
|
|
|
124
|
4 |
|
if ($validator->fails()) { |
125
|
2 |
|
return $this->jsonResponseError([ |
126
|
2 |
|
'messages' => $validator->messages(), |
127
|
2 |
|
], 422); |
128
|
|
|
} |
129
|
|
|
|
130
|
2 |
|
$this->media->makeDirectory( |
131
|
2 |
|
$path = trim($data['location'], '/').'/'.Str::slug($data['name']) |
132
|
|
|
); |
133
|
|
|
|
134
|
2 |
|
return $this->jsonResponseSuccess(['data' => compact('path')]); |
135
|
|
|
} |
136
|
|
|
|
137
|
8 |
|
public function renameMedia(Request $request) |
138
|
|
|
{ |
139
|
8 |
|
$this->authorize(MediasPolicy::PERMISSION_UPDATE); |
140
|
|
|
|
141
|
|
|
// TODO: check if the folder does not exists |
142
|
8 |
|
$validator = validator($data = $request->all(), [ |
143
|
8 |
|
'media' => ['required', 'array'], |
144
|
|
|
'media.type' => ['required', 'string'], |
145
|
|
|
'media.name' => ['required', 'string'], |
146
|
|
|
'newName' => ['required', 'string'], |
147
|
|
|
'location' => ['required', 'string'], |
148
|
|
|
]); |
149
|
|
|
|
150
|
8 |
|
if ($validator->fails()) { |
151
|
2 |
|
return $this->jsonResponseError([ |
152
|
2 |
|
'messages' => $validator->messages(), |
153
|
2 |
|
], 422); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
// TODO Refactor this... |
157
|
6 |
|
$location = trim($data['location'], '/'); |
158
|
6 |
|
$from = $location.'/'.$data['media']['name']; |
159
|
|
|
|
160
|
6 |
|
switch (strtolower($data['media']['type'])) { |
161
|
6 |
|
case Media::MEDIA_TYPE_FILE: |
162
|
2 |
|
$path = $this->moveFile($location, $from, $data['newName']); |
163
|
2 |
|
break; |
164
|
|
|
|
165
|
4 |
|
case Media::MEDIA_TYPE_DIRECTORY: |
166
|
2 |
|
$path = $this->moveDirectory($location, $from, $data['newName']); |
167
|
2 |
|
break; |
168
|
|
|
|
169
|
|
|
default: |
170
|
2 |
|
return $this->jsonResponseError([ |
171
|
2 |
|
'message' => 'Something wrong was happened while renaming the media.', |
172
|
2 |
|
], 500); |
173
|
|
|
} |
174
|
|
|
|
175
|
4 |
|
return $this->jsonResponseSuccess([ |
176
|
4 |
|
'data' => compact('path'), |
177
|
|
|
]); |
178
|
|
|
} |
179
|
|
|
|
180
|
6 |
|
public function deleteMedia(Request $request) |
181
|
|
|
{ |
182
|
6 |
|
$this->authorize(MediasPolicy::PERMISSION_DELETE); |
183
|
|
|
|
184
|
|
|
// TODO: Add validation |
185
|
6 |
|
$validator = validator($data = $request->all(), [ |
186
|
6 |
|
'media' => ['required', 'array'], |
187
|
|
|
'media.type' => ['required', 'string'], |
188
|
|
|
'media.path' => ['required', 'string'], |
189
|
|
|
]); |
190
|
|
|
|
191
|
6 |
|
if ($validator->fails()) { |
192
|
2 |
|
return $this->jsonResponseError([ |
193
|
2 |
|
'messages' => $validator->messages(), |
194
|
2 |
|
], 422); |
195
|
|
|
} |
196
|
|
|
|
197
|
4 |
|
$type = $data['media']['type']; |
198
|
4 |
|
$path = $data['media']['path']; |
199
|
|
|
|
200
|
|
|
// TODO Refactor this... |
201
|
4 |
|
$deleted = false; |
202
|
|
|
|
203
|
4 |
|
if ($type == Media::MEDIA_TYPE_FILE) { |
204
|
2 |
|
$deleted = $this->media->deleteFile($path); |
205
|
|
|
} |
206
|
2 |
|
elseif ($type == Media::MEDIA_TYPE_DIRECTORY) { |
207
|
2 |
|
$deleted = $this->media->deleteDirectory(trim($path, '/')); |
208
|
|
|
} |
209
|
|
|
|
210
|
4 |
|
return $deleted ? $this->jsonResponseSuccess() : $this->jsonResponseError(); |
211
|
|
|
} |
212
|
|
|
|
213
|
2 |
|
public function moveLocations(Request $request) |
214
|
|
|
{ |
215
|
2 |
|
$this->authorize(MediasPolicy::PERMISSION_UPDATE); |
216
|
|
|
|
217
|
2 |
|
$location = $request->get('location', '/'); |
218
|
2 |
|
$name = $request->get('name'); |
219
|
2 |
|
$isHome = $location === '/'; |
220
|
2 |
|
$selected = $isHome ? $name : $location.'/'.$name; |
221
|
|
|
|
222
|
2 |
|
$destinations = $this->media->directories($location) |
223
|
2 |
|
->pluck('path') |
224
|
2 |
|
->reject(function ($path) use ($selected) { |
225
|
2 |
|
return $path === $selected; |
226
|
2 |
|
}) |
227
|
2 |
|
->values(); |
228
|
|
|
|
229
|
2 |
|
if ( ! $isHome) |
230
|
2 |
|
$destinations->prepend('..'); |
231
|
|
|
|
232
|
2 |
|
return $this->jsonResponseSuccess(compact('destinations')); |
233
|
|
|
} |
234
|
|
|
|
235
|
4 |
|
public function moveMedia(Request $request) |
236
|
|
|
{ |
237
|
4 |
|
$this->authorize(MediasPolicy::PERMISSION_UPDATE); |
238
|
|
|
|
239
|
4 |
|
$validator = validator($data = $request->all(), [ |
240
|
4 |
|
'old_path' => ['required', 'string'], |
241
|
|
|
'new_path' => ['required', 'string'], |
242
|
|
|
]); |
243
|
|
|
|
244
|
4 |
|
if ($validator->fails()) { |
245
|
2 |
|
return $this->jsonResponseError([ |
246
|
2 |
|
'messages' => $validator->messages(), |
247
|
2 |
|
], 422); |
248
|
|
|
} |
249
|
|
|
|
250
|
2 |
|
return $this->media->move($data['old_path'], $data['new_path']) |
251
|
2 |
|
? $this->jsonResponseSuccess() |
252
|
2 |
|
: $this->jsonResponseError(['message' => 'Something wrong happened !'], 500); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/* ----------------------------------------------------------------- |
256
|
|
|
| Other Methods |
257
|
|
|
| ----------------------------------------------------------------- |
258
|
|
|
*/ |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Move a file. |
262
|
|
|
* |
263
|
|
|
* @param string $location |
264
|
|
|
* @param string $from |
265
|
|
|
* @param string $newName |
266
|
|
|
* |
267
|
|
|
* @return string |
268
|
|
|
*/ |
269
|
2 |
|
private function moveFile($location, $from, $newName) |
270
|
|
|
{ |
271
|
2 |
|
$filename = Str::slug(pathinfo($newName, PATHINFO_FILENAME)).'.'.pathinfo($newName, PATHINFO_EXTENSION); |
272
|
|
|
|
273
|
2 |
|
$this->media->move($from, $to = $location.'/'.$filename); |
274
|
|
|
|
275
|
2 |
|
return $to; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Move a directory. |
280
|
|
|
* |
281
|
|
|
* @param string $location |
282
|
|
|
* @param string $from |
283
|
|
|
* @param string $newName |
284
|
|
|
* |
285
|
|
|
* @return string |
286
|
|
|
*/ |
287
|
2 |
|
private function moveDirectory($location, $from, $newName) |
288
|
|
|
{ |
289
|
2 |
|
$to = $location.'/'.Str::slug($newName); |
290
|
|
|
|
291
|
2 |
|
$this->media->move($from, $to); |
292
|
|
|
|
293
|
2 |
|
return $to; |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|