|
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
|
|
|
$location = trim($location, '/'); |
|
71
|
|
|
|
|
72
|
|
|
return response()->json([ |
|
73
|
|
|
'status' => 'success', |
|
74
|
|
|
'data' => $this->media->all($location), |
|
75
|
|
|
]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Upload a media file. |
|
80
|
|
|
* |
|
81
|
|
|
* @param \Illuminate\Http\Request $request |
|
82
|
|
|
* |
|
83
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
84
|
|
|
*/ |
|
85
|
|
|
public function uploadMedia(Request $request) |
|
86
|
|
|
{ |
|
87
|
|
|
$validator = validator($request->all(), [ |
|
88
|
|
|
'location' => 'required', |
|
89
|
|
|
'medias' => 'required|array', |
|
90
|
|
|
'medias.*' => 'required|file' |
|
91
|
|
|
]); |
|
92
|
|
|
|
|
93
|
|
|
if ($validator->fails()) { |
|
94
|
|
|
return response()->json([ |
|
95
|
|
|
'status' => 'error', |
|
96
|
|
|
'errors' => $validator->messages(), |
|
97
|
|
|
]); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$this->media->storeMany( |
|
101
|
|
|
$request->get('location'), $request->file('medias') |
|
102
|
|
|
); |
|
103
|
|
|
|
|
104
|
|
|
return response()->json(['status' => 'success']); |
|
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
|
|
|
$data = $request->all(); |
|
117
|
|
|
$validator = validator($data, [ |
|
118
|
|
|
'name' => 'required', // TODO: check if the folder does not exists |
|
119
|
|
|
'location' => 'required', |
|
120
|
|
|
]); |
|
121
|
|
|
|
|
122
|
|
|
if ($validator->fails()) { |
|
123
|
|
|
return response()->json([ |
|
124
|
|
|
'status' => 'error', |
|
125
|
|
|
'errors' => $validator->messages(), |
|
126
|
|
|
], 400); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$this->media->makeDirectory( |
|
130
|
|
|
$path = trim($data['location'], '/').'/'.Str::slug($data['name']) |
|
131
|
|
|
); |
|
132
|
|
|
|
|
133
|
|
|
return response()->json([ |
|
134
|
|
|
'status' => 'success', |
|
135
|
|
|
'data' => compact('path'), |
|
136
|
|
|
]); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function renameMedia(Request $request) |
|
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', |
|
147
|
|
|
'location' => 'required', |
|
148
|
|
|
]); |
|
149
|
|
|
|
|
150
|
|
|
if ($validator->fails()) { |
|
151
|
|
|
return response()->json([ |
|
152
|
|
|
'status' => 'error', |
|
153
|
|
|
'errors' => $validator->messages(), |
|
154
|
|
|
], 400); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
// TODO Refactor this... |
|
158
|
|
|
$location = trim($data['location'], '/'); |
|
159
|
|
|
$from = $location.'/'.$data['media']['name']; |
|
160
|
|
|
|
|
161
|
|
|
switch ($data['media']['type']) { |
|
162
|
|
|
case Media::MEDIA_TYPE_FILE: |
|
163
|
|
|
return $this->moveFile($location, $from, $data); |
|
164
|
|
|
|
|
165
|
|
|
case Media::MEDIA_TYPE_DIRECTORY: |
|
166
|
|
|
return $this->moveDirectory($location, $from, $data); |
|
167
|
|
|
|
|
168
|
|
|
default: |
|
169
|
|
|
return response()->json([ |
|
170
|
|
|
'status' => 'error', |
|
171
|
|
|
'message' => 'Something wrong was happened while renaming the media.', |
|
172
|
|
|
], 400); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function deleteMedia(Request $request) |
|
177
|
|
|
{ |
|
178
|
|
|
// TODO: Add validation |
|
179
|
|
|
$data = $request->all(); |
|
180
|
|
|
$disk = $this->media->defaultDisk(); |
|
181
|
|
|
|
|
182
|
|
|
// TODO Refactor this... |
|
183
|
|
|
if ($data['media']['type'] == Media::MEDIA_TYPE_FILE) { |
|
184
|
|
|
$deleted = $disk->delete($data['media']['path']); |
|
185
|
|
|
} |
|
186
|
|
|
else { |
|
187
|
|
|
$path = trim($data['media']['path'], '/'); |
|
188
|
|
|
|
|
189
|
|
|
$deleted = $disk->deleteDirectory($path); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
return response()->json(['status' => $deleted ? 'success' : 'error']); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/* ----------------------------------------------------------------- |
|
196
|
|
|
| Other Methods |
|
197
|
|
|
| ----------------------------------------------------------------- |
|
198
|
|
|
*/ |
|
199
|
|
|
private function moveFile($location, $from, array $data) |
|
200
|
|
|
{ |
|
201
|
|
|
$ext = pathinfo($data['media']['url'], PATHINFO_EXTENSION); |
|
202
|
|
|
$to = $location.'/'.Str::slug(str_replace(".{$ext}", '', $data['newName'])).'.'.$ext; |
|
203
|
|
|
|
|
204
|
|
|
$this->media->move($from, $to); |
|
205
|
|
|
|
|
206
|
|
|
return response()->json([ |
|
207
|
|
|
'status' => 'success', |
|
208
|
|
|
'data' => ['path' => $to], |
|
209
|
|
|
]); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
private function moveDirectory($location, $from, array $data) |
|
213
|
|
|
{ |
|
214
|
|
|
$to = $location.'/'.Str::slug($data['newName']); |
|
215
|
|
|
|
|
216
|
|
|
$this->media->move($from, $to); |
|
217
|
|
|
|
|
218
|
|
|
return response()->json([ |
|
219
|
|
|
'status' => 'success', |
|
220
|
|
|
'data' => ['path' => $to], |
|
221
|
|
|
]); |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|