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