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
|
20 |
|
public function __construct(Media $media) |
49
|
|
|
{ |
50
|
20 |
|
$this->media = $media; |
51
|
20 |
|
} |
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
|
6 |
|
public function renameMedia(Request $request) |
138
|
|
|
{ |
139
|
6 |
|
$this->authorize(MediasPolicy::PERMISSION_UPDATE); |
140
|
|
|
|
141
|
|
|
// TODO: check if the folder does not exists |
142
|
6 |
|
$validator = validator($data = $request->all(), [ |
143
|
6 |
|
'media' => ['required', 'array'], |
144
|
6 |
|
'media.type' => ['required', 'string', 'in:'.implode(',', [Media::MEDIA_TYPE_FILE, Media::MEDIA_TYPE_DIRECTORY])], |
145
|
|
|
'media.name' => ['required', 'string'], |
146
|
|
|
'newName' => ['required', 'string'], |
147
|
|
|
'location' => ['required', 'string'], |
148
|
|
|
]); |
149
|
|
|
|
150
|
6 |
|
if ($validator->fails()) { |
151
|
2 |
|
return $this->jsonResponseError([ |
152
|
2 |
|
'messages' => $validator->messages(), |
153
|
2 |
|
], 422); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
// TODO Refactor this... |
157
|
4 |
|
$location = trim($data['location'], '/'); |
158
|
4 |
|
$from = $location.'/'.$data['media']['name']; |
159
|
|
|
|
160
|
4 |
|
switch ($data['media']['type']) { |
161
|
4 |
|
case Media::MEDIA_TYPE_FILE: |
162
|
2 |
|
$path = $this->moveFile($location, $from, $data['newName']); |
163
|
2 |
|
break; |
164
|
|
|
|
165
|
2 |
|
case Media::MEDIA_TYPE_DIRECTORY: |
166
|
2 |
|
$path = $this->moveDirectory($location, $from, $data['newName']); |
167
|
2 |
|
break; |
168
|
|
|
|
169
|
|
|
default: |
170
|
|
|
return $this->jsonResponseError([ |
171
|
|
|
'message' => 'Something wrong was happened while renaming the media.', |
172
|
|
|
]); |
173
|
|
|
} |
174
|
|
|
|
175
|
4 |
|
return $this->jsonResponseSuccess([ |
176
|
4 |
|
'data' => compact('path'), |
177
|
|
|
]); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function deleteMedia(Request $request) |
181
|
|
|
{ |
182
|
|
|
$this->authorize(MediasPolicy::PERMISSION_DELETE); |
183
|
|
|
|
184
|
|
|
// TODO: Add validation |
185
|
|
|
$data = $request->all(); |
186
|
|
|
|
187
|
|
|
// TODO Refactor this... |
188
|
|
|
if ($data['media']['type'] == Media::MEDIA_TYPE_FILE) { |
189
|
|
|
$deleted = $this->media->deleteFile($data['media']['path']); |
190
|
|
|
} |
191
|
|
|
else { |
192
|
|
|
$deleted = $this->media->deleteDirectory( |
193
|
|
|
trim($data['media']['path'], '/') |
194
|
|
|
); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $deleted ? $this->jsonResponseSuccess() : $this->jsonResponseError(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function moveLocations(Request $request) |
201
|
|
|
{ |
202
|
|
|
$this->authorize(MediasPolicy::PERMISSION_UPDATE); |
203
|
|
|
|
204
|
|
|
$location = $request->get('location'); |
205
|
|
|
$name = $request->get('name'); |
206
|
|
|
$isHome = $location == '/'; |
207
|
|
|
$selected = $isHome ? $name : $location.'/'.$name; |
208
|
|
|
|
209
|
|
|
/** @var \Illuminate\Support\Collection $destinations */ |
210
|
|
|
$destinations = $this->media->directories($location) |
211
|
|
|
->transform(function ($directory) { |
212
|
|
|
return $directory['path']; |
213
|
|
|
}) |
214
|
|
|
->reject(function ($path) use ($selected) { |
215
|
|
|
return $path === $selected; |
216
|
|
|
}) |
217
|
|
|
->values(); |
218
|
|
|
|
219
|
|
|
if ( ! $isHome) { |
220
|
|
|
$destinations->prepend('..'); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $this->jsonResponseSuccess([ |
224
|
|
|
'destinations' => $destinations, |
225
|
|
|
]); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function moveMedia(Request $request) |
229
|
|
|
{ |
230
|
|
|
$this->authorize(MediasPolicy::PERMISSION_UPDATE); |
231
|
|
|
|
232
|
|
|
$validator = validator($data = $request->all(), [ |
233
|
|
|
'old-path' => ['required', 'string'], |
234
|
|
|
'new-path' => ['required', 'string'], |
235
|
|
|
]); |
236
|
|
|
|
237
|
|
|
if ($validator->fails()) { |
238
|
|
|
return $this->jsonResponseError([ |
239
|
|
|
'messages' => $validator->messages(), |
240
|
|
|
], 422); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return $this->media->move($data['old-path'], $data['new-path']) |
244
|
|
|
? $this->jsonResponseSuccess() |
245
|
|
|
: $this->jsonResponseError(['message' => 'Something wrong happened !'], 500); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/* ----------------------------------------------------------------- |
249
|
|
|
| Other Methods |
250
|
|
|
| ----------------------------------------------------------------- |
251
|
|
|
*/ |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Move a file. |
255
|
|
|
* |
256
|
|
|
* @param string $location |
257
|
|
|
* @param string $from |
258
|
|
|
* @param string $newName |
259
|
|
|
* |
260
|
|
|
* @return string |
261
|
|
|
*/ |
262
|
2 |
|
private function moveFile($location, $from, $newName) |
263
|
|
|
{ |
264
|
2 |
|
$filename = Str::slug(pathinfo($newName, PATHINFO_FILENAME)).'.'.pathinfo($newName, PATHINFO_EXTENSION); |
265
|
|
|
|
266
|
2 |
|
$this->media->move($from, $to = $location.'/'.$filename); |
267
|
|
|
|
268
|
2 |
|
return $to; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Move a directory. |
273
|
|
|
* |
274
|
|
|
* @param string $location |
275
|
|
|
* @param string $from |
276
|
|
|
* @param string $newName |
277
|
|
|
* |
278
|
|
|
* @return string |
279
|
|
|
*/ |
280
|
2 |
|
private function moveDirectory($location, $from, $newName) |
281
|
|
|
{ |
282
|
2 |
|
$to = $location.'/'.Str::slug($newName); |
283
|
|
|
|
284
|
2 |
|
$this->media->move($from, $to); |
285
|
|
|
|
286
|
2 |
|
return $to; |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|