1
|
|
|
<?php namespace Arcanesoft\Media\Http\Controllers\Admin; |
2
|
|
|
|
3
|
|
|
use Arcanesoft\Media\Contracts\Media; |
4
|
|
|
use Illuminate\Http\Request; |
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class MediasController |
9
|
|
|
* |
10
|
|
|
* @package Arcanesoft\Media\Http\Controllers\Admin |
11
|
|
|
* @author ARCANEDEV <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class MediasController extends Controller |
14
|
|
|
{ |
15
|
|
|
/* ----------------------------------------------------------------- |
16
|
|
|
| Properties |
17
|
|
|
| ----------------------------------------------------------------- |
18
|
|
|
*/ |
19
|
|
|
/** |
20
|
|
|
* The media instance. |
21
|
|
|
* |
22
|
|
|
* @var \Arcanesoft\Media\Contracts\Media |
23
|
|
|
*/ |
24
|
|
|
protected $media; |
25
|
|
|
|
26
|
|
|
/* ----------------------------------------------------------------- |
27
|
|
|
| Constructor |
28
|
|
|
| ----------------------------------------------------------------- |
29
|
|
|
*/ |
30
|
|
|
/** |
31
|
|
|
* MediasController constructor. |
32
|
|
|
* |
33
|
|
|
* @param \Arcanesoft\Media\Contracts\Media $media |
34
|
|
|
*/ |
35
|
|
|
public function __construct(Media $media) |
36
|
|
|
{ |
37
|
|
|
parent::__construct(); |
38
|
|
|
|
39
|
|
|
$this->media = $media; |
40
|
|
|
|
41
|
|
|
$this->setCurrentPage('media'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/* ----------------------------------------------------------------- |
45
|
|
|
| Main Methods |
46
|
|
|
| ----------------------------------------------------------------- |
47
|
|
|
*/ |
48
|
|
|
/** |
49
|
|
|
* Show the media manager page. |
50
|
|
|
* |
51
|
|
|
* @return \Illuminate\View\View |
52
|
|
|
*/ |
53
|
|
|
public function index() |
54
|
|
|
{ |
55
|
|
|
$this->setTitle('Media'); |
56
|
|
|
|
57
|
|
|
return $this->view('admin.manager'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get the the media files. |
62
|
|
|
* |
63
|
|
|
* @param \Illuminate\Http\Request $request |
64
|
|
|
* |
65
|
|
|
* @return \Illuminate\Http\JsonResponse |
66
|
|
|
*/ |
67
|
|
|
public function getAll(Request $request) |
68
|
|
|
{ |
69
|
|
|
$location = $request->get('location', '/'); |
70
|
|
|
|
71
|
|
|
return response()->json([ |
72
|
|
|
'status' => 'success', |
73
|
|
|
'data' => $this->media->all($location), |
74
|
|
|
]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Upload a media file. |
79
|
|
|
* |
80
|
|
|
* @param \Illuminate\Http\Request $request |
81
|
|
|
* |
82
|
|
|
* @return \Illuminate\Http\JsonResponse |
83
|
|
|
*/ |
84
|
|
|
public function uploadMedia(Request $request) |
85
|
|
|
{ |
86
|
|
|
$validator = validator($request->all(), [ |
87
|
|
|
'location' => 'required', |
88
|
|
|
'medias' => 'required|array', |
89
|
|
|
'medias.*' => 'required|file' |
90
|
|
|
]); |
91
|
|
|
|
92
|
|
|
if ($validator->fails()) { |
93
|
|
|
return response()->json([ |
94
|
|
|
'status' => 'error', |
95
|
|
|
'errors' => $validator->messages(), |
96
|
|
|
]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->media->storeMany( |
100
|
|
|
$request->get('location'), $request->file('medias') |
|
|
|
|
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
return response()->json(['status' => 'success']); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Create a directory. |
108
|
|
|
* |
109
|
|
|
* @param \Illuminate\Http\Request $request |
110
|
|
|
* |
111
|
|
|
* @return \Illuminate\Http\JsonResponse |
112
|
|
|
*/ |
113
|
|
|
public function createDirectory(Request $request) |
114
|
|
|
{ |
115
|
|
|
$data = $request->all(); |
116
|
|
|
$validator = validator($data, [ |
117
|
|
|
'name' => 'required', // TODO: check if the folder does not exists |
118
|
|
|
'location' => 'required', |
119
|
|
|
]); |
120
|
|
|
|
121
|
|
|
if ($validator->fails()) { |
122
|
|
|
return response()->json([ |
123
|
|
|
'status' => 'error', |
124
|
|
|
'errors' => $validator->messages(), |
125
|
|
|
], 400); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$this->media->makeDirectory( |
129
|
|
|
$path = trim($data['location'], '/').'/'.Str::slug($data['name']) |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
return response()->json([ |
133
|
|
|
'status' => 'success', |
134
|
|
|
'data' => compact('path'), |
135
|
|
|
]); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function renameMedia(Request $request) |
139
|
|
|
{ |
140
|
|
|
$data = $request->all(); |
141
|
|
|
|
142
|
|
|
// TODO: check if the folder does not exists |
143
|
|
|
$validator = validator($data, [ |
144
|
|
|
'media' => 'required|array', |
145
|
|
|
'newName' => 'required', |
146
|
|
|
'location' => 'required', |
147
|
|
|
]); |
148
|
|
|
|
149
|
|
|
if ($validator->fails()) { |
150
|
|
|
return response()->json([ |
151
|
|
|
'status' => 'error', |
152
|
|
|
'errors' => $validator->messages(), |
153
|
|
|
], 400); |
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
|
|
|
$to = $this->moveFile($location, $from, $data); |
163
|
|
|
|
164
|
|
|
return response()->json([ |
165
|
|
|
'status' => 'success', |
166
|
|
|
'data' => ['path' => $to], |
167
|
|
|
]); |
168
|
|
|
|
169
|
|
|
case Media::MEDIA_TYPE_DIRECTORY: |
170
|
|
|
$to = $this->moveDirectory($location, $from, $data); |
171
|
|
|
|
172
|
|
|
return response()->json([ |
173
|
|
|
'status' => 'success', |
174
|
|
|
'data' => ['path' => $to], |
175
|
|
|
]); |
176
|
|
|
|
177
|
|
|
default: |
178
|
|
|
return response()->json([ |
179
|
|
|
'status' => 'error', |
180
|
|
|
'message' => 'Something wrong was happened while renaming the media.', |
181
|
|
|
], 400); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function deleteMedia(Request $request) |
186
|
|
|
{ |
187
|
|
|
// TODO: Add validation |
188
|
|
|
$data = $request->all(); |
189
|
|
|
$disk = $this->media->defaultDisk(); |
190
|
|
|
|
191
|
|
|
// TODO Refactor this... |
192
|
|
|
if ($data['media']['type'] == Media::MEDIA_TYPE_FILE) { |
193
|
|
|
$deleted = $disk->delete($data['media']['path']); |
194
|
|
|
} |
195
|
|
|
else { |
196
|
|
|
$path = trim($data['media']['path'], '/'); |
197
|
|
|
|
198
|
|
|
$deleted = $disk->deleteDirectory($path); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return response()->json(['status' => $deleted ? 'success' : 'error']); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function moveLocations(Request $request) |
205
|
|
|
{ |
206
|
|
|
$location = $request->get('location'); |
207
|
|
|
$name = $request->get('name'); |
208
|
|
|
$isHome = $location == '/'; |
209
|
|
|
$selected = $isHome ? $name : $location.'/'.$name; |
210
|
|
|
|
211
|
|
|
/** @var \Illuminate\Support\Collection $destinations */ |
212
|
|
|
$destinations = $this->media->directories($location) |
213
|
|
|
->transform(function ($directory) { |
214
|
|
|
return $directory['path']; |
215
|
|
|
}) |
216
|
|
|
->reject(function ($path) use ($selected) { |
217
|
|
|
return $path === $selected; |
218
|
|
|
}); |
219
|
|
|
|
220
|
|
|
if ( ! $isHome) { |
221
|
|
|
$destinations->prepend('..'); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return response()->json([ |
225
|
|
|
'status' => 'success', |
226
|
|
|
'data' => $destinations, |
227
|
|
|
]); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function moveMedia(Request $request) |
231
|
|
|
{ |
232
|
|
|
$moved = $this->media->move( |
233
|
|
|
$request->get('old-path'), |
234
|
|
|
$request->get('new-path') |
235
|
|
|
); |
236
|
|
|
|
237
|
|
|
if ($moved) { |
238
|
|
|
return response()->json([ |
239
|
|
|
'status' => 'success', |
240
|
|
|
]); |
241
|
|
|
} |
242
|
|
|
else { |
243
|
|
|
// Return an error response |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/* ----------------------------------------------------------------- |
248
|
|
|
| Other Methods |
249
|
|
|
| ----------------------------------------------------------------- |
250
|
|
|
*/ |
251
|
|
|
private function moveFile($location, $from, array $data) |
252
|
|
|
{ |
253
|
|
|
$ext = pathinfo($data['media']['url'], PATHINFO_EXTENSION); |
254
|
|
|
$to = $location.'/'.Str::slug(str_replace(".{$ext}", '', $data['newName'])).'.'.$ext; |
255
|
|
|
|
256
|
|
|
$this->media->move($from, $to); |
257
|
|
|
|
258
|
|
|
return $to; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
private function moveDirectory($location, $from, array $data) |
262
|
|
|
{ |
263
|
|
|
$to = $location.'/'.Str::slug($data['newName']); |
264
|
|
|
|
265
|
|
|
$this->media->move($from, $to); |
266
|
|
|
|
267
|
|
|
return $to; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
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.