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
|
15 |
|
public function __construct(Media $media) |
49
|
|
|
{ |
50
|
15 |
|
$this->media = $media; |
51
|
15 |
|
} |
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
|
6 |
|
public function getAll(Request $request) |
65
|
|
|
{ |
66
|
6 |
|
$this->authorize(MediasPolicy::PERMISSION_LIST); |
67
|
|
|
|
68
|
6 |
|
$location = $request->get('location', '/'); |
69
|
|
|
|
70
|
6 |
|
return $this->jsonResponseSuccess([ |
71
|
6 |
|
'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
|
6 |
|
public function uploadMedia(Request $request) |
83
|
|
|
{ |
84
|
6 |
|
$this->authorize(MediasPolicy::PERMISSION_CREATE); |
85
|
|
|
|
86
|
6 |
|
$validator = validator($request->all(), [ |
87
|
6 |
|
'location' => ['required', 'string'], |
88
|
|
|
'medias' => ['required', 'array'], |
89
|
|
|
'medias.*' => ['required', 'file'] |
90
|
|
|
]); |
91
|
|
|
|
92
|
6 |
|
if ($validator->fails()) { |
93
|
3 |
|
return $this->jsonResponseError([ |
94
|
3 |
|
'messages' => $validator->messages(), |
95
|
3 |
|
], 422); |
96
|
|
|
} |
97
|
|
|
|
98
|
3 |
|
$uploaded = $this->media->storeMany( |
99
|
3 |
|
$request->get('location'), $request->file('medias') |
100
|
|
|
); |
101
|
|
|
|
102
|
3 |
|
return $this->jsonResponseSuccess([ |
103
|
3 |
|
'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
|
|
|
public function createDirectory(Request $request) |
115
|
|
|
{ |
116
|
|
|
$this->authorize(MediasPolicy::PERMISSION_CREATE); |
117
|
|
|
|
118
|
|
|
$data = $request->all(); |
119
|
|
|
$validator = validator($data, [ |
120
|
|
|
'name' => ['required', 'string'], // TODO: check if the folder does not exists |
121
|
|
|
'location' => ['required', 'string'], |
122
|
|
|
]); |
123
|
|
|
|
124
|
|
|
if ($validator->fails()) { |
125
|
|
|
return $this->jsonResponseError([ |
126
|
|
|
'messages' => $validator->messages(), |
127
|
|
|
], 422); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$this->media->makeDirectory( |
131
|
|
|
$path = trim($data['location'], '/').'/'.Str::slug($data['name']) |
132
|
|
|
); |
133
|
|
|
|
134
|
|
|
return $this->jsonResponseSuccess(compact('path')); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function renameMedia(Request $request) |
138
|
|
|
{ |
139
|
|
|
$this->authorize(MediasPolicy::PERMISSION_UPDATE); |
140
|
|
|
|
141
|
|
|
$data = $request->all(); |
142
|
|
|
|
143
|
|
|
// TODO: check if the folder does not exists |
144
|
|
|
$validator = validator($data, [ |
145
|
|
|
'media' => ['required', 'array'], |
146
|
|
|
'newName' => ['required', 'string'], |
147
|
|
|
'location' => ['required', 'string'], |
148
|
|
|
]); |
149
|
|
|
|
150
|
|
|
if ($validator->fails()) { |
151
|
|
|
return $this->jsonResponseError([ |
152
|
|
|
'messages' => $validator->messages(), |
153
|
|
|
], 422); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
// TODO Refactor this... |
157
|
|
|
$location = trim($data['location'], '/'); |
158
|
|
|
$from = $location.'/'.$data['media']['name']; |
159
|
|
|
|
160
|
|
|
switch ($data['media']['type']) { |
161
|
|
|
case Media::MEDIA_TYPE_FILE: |
162
|
|
|
return $this->jsonResponseSuccess([ |
163
|
|
|
'path' => $this->moveFile($location, $from, $data) |
164
|
|
|
]); |
165
|
|
|
|
166
|
|
|
case Media::MEDIA_TYPE_DIRECTORY: |
167
|
|
|
return $this->jsonResponseSuccess([ |
168
|
|
|
'path' => $this->moveDirectory($location, $from, $data) |
169
|
|
|
]); |
170
|
|
|
|
171
|
|
|
default: |
172
|
|
|
$this->jsonResponseError([ |
173
|
|
|
'message' => 'Something wrong was happened while renaming the media.', |
174
|
|
|
]); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function deleteMedia(Request $request) |
179
|
|
|
{ |
180
|
|
|
$this->authorize(MediasPolicy::PERMISSION_DELETE); |
181
|
|
|
|
182
|
|
|
// TODO: Add validation |
183
|
|
|
$data = $request->all(); |
184
|
|
|
|
185
|
|
|
// TODO Refactor this... |
186
|
|
|
if ($data['media']['type'] == Media::MEDIA_TYPE_FILE) { |
187
|
|
|
$deleted = $this->media->deleteFile($data['media']['path']); |
188
|
|
|
} |
189
|
|
|
else { |
190
|
|
|
$deleted = $this->media->deleteDirectory( |
191
|
|
|
trim($data['media']['path'], '/') |
192
|
|
|
); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $deleted ? $this->jsonResponseSuccess() : $this->jsonResponseError(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function moveLocations(Request $request) |
199
|
|
|
{ |
200
|
|
|
$this->authorize(MediasPolicy::PERMISSION_UPDATE); |
201
|
|
|
|
202
|
|
|
$location = $request->get('location'); |
203
|
|
|
$name = $request->get('name'); |
204
|
|
|
$isHome = $location == '/'; |
205
|
|
|
$selected = $isHome ? $name : $location.'/'.$name; |
206
|
|
|
|
207
|
|
|
/** @var \Illuminate\Support\Collection $destinations */ |
208
|
|
|
$destinations = $this->media->directories($location) |
209
|
|
|
->transform(function ($directory) { |
210
|
|
|
return $directory['path']; |
211
|
|
|
}) |
212
|
|
|
->reject(function ($path) use ($selected) { |
213
|
|
|
return $path === $selected; |
214
|
|
|
}) |
215
|
|
|
->values(); |
216
|
|
|
|
217
|
|
|
if ( ! $isHome) { |
218
|
|
|
$destinations->prepend('..'); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return $this->jsonResponseSuccess([ |
222
|
|
|
'destinations' => $destinations, |
223
|
|
|
]); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function moveMedia(Request $request) |
227
|
|
|
{ |
228
|
|
|
$this->authorize(MediasPolicy::PERMISSION_UPDATE); |
229
|
|
|
|
230
|
|
|
$validator = validator($data = $request->all(), [ |
231
|
|
|
'old-path' => ['required', 'string'], |
232
|
|
|
'new-path' => ['required', 'string'], |
233
|
|
|
]); |
234
|
|
|
|
235
|
|
|
if ($validator->fails()) { |
236
|
|
|
return $this->jsonResponseError([ |
237
|
|
|
'messages' => $validator->messages(), |
238
|
|
|
], 422); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return $this->media->move($data['old-path'], $data['new-path']) |
242
|
|
|
? $this->jsonResponseSuccess() |
243
|
|
|
: $this->jsonResponseError(['message' => 'Something wrong happened !'], 500); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/* ----------------------------------------------------------------- |
247
|
|
|
| Other Methods |
248
|
|
|
| ----------------------------------------------------------------- |
249
|
|
|
*/ |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Move a file. |
253
|
|
|
* |
254
|
|
|
* @param string $location |
255
|
|
|
* @param string $from |
256
|
|
|
* @param array $data |
257
|
|
|
* |
258
|
|
|
* @return string |
259
|
|
|
*/ |
260
|
|
|
private function moveFile($location, $from, array $data) |
261
|
|
|
{ |
262
|
|
|
$ext = pathinfo($data['media']['url'], PATHINFO_EXTENSION); |
263
|
|
|
$to = $location.'/'.Str::slug(str_replace(".{$ext}", '', $data['newName'])).'.'.$ext; |
264
|
|
|
|
265
|
|
|
$this->media->move($from, $to); |
266
|
|
|
|
267
|
|
|
return $to; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Move a directory. |
272
|
|
|
* |
273
|
|
|
* @param string $location |
274
|
|
|
* @param string $from |
275
|
|
|
* @param array $data |
276
|
|
|
* |
277
|
|
|
* @return string |
278
|
|
|
*/ |
279
|
|
|
private function moveDirectory($location, $from, array $data) |
280
|
|
|
{ |
281
|
|
|
$to = $location.'/'.Str::slug($data['newName']); |
282
|
|
|
|
283
|
|
|
$this->media->move($from, $to); |
284
|
|
|
|
285
|
|
|
return $to; |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|