|
1
|
|
|
<?php namespace Arcanesoft\Media\Http\Controllers\Admin; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanesoft\Media\Http\Controllers\Admin\Controller; |
|
4
|
|
|
use Carbon\Carbon; |
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Illuminate\Support\Facades\Storage; |
|
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
|
|
|
class MediasController extends Controller |
|
16
|
|
|
{ |
|
17
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
18
|
|
|
| Constructor |
|
19
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
20
|
|
|
*/ |
|
21
|
|
|
/** |
|
22
|
|
|
* MediasController constructor. |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct() |
|
25
|
|
|
{ |
|
26
|
|
|
parent::__construct(); |
|
27
|
|
|
|
|
28
|
|
|
$this->setCurrentPage('media'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
32
|
|
|
| Main Functions |
|
33
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
34
|
|
|
*/ |
|
35
|
|
|
public function index() |
|
36
|
|
|
{ |
|
37
|
|
|
$this->setTitle('Media'); |
|
38
|
|
|
|
|
39
|
|
|
return $this->view('manager'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function getAll(Request $request) |
|
43
|
|
|
{ |
|
44
|
|
|
$location = $request->get('location'); |
|
45
|
|
|
$location = trim($location, '/'); |
|
46
|
|
|
|
|
47
|
|
|
return response()->json([ |
|
48
|
|
|
'status' => 'success', |
|
49
|
|
|
'data' => array_merge( |
|
50
|
|
|
$this->getDirectoriesFromLocation($location), |
|
51
|
|
|
$this->getFilesFromLocation($location) |
|
52
|
|
|
), |
|
53
|
|
|
]); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function uploadMedia(Request $request) |
|
57
|
|
|
{ |
|
58
|
|
|
$validator = \Validator::make($request->all(), [ |
|
59
|
|
|
'location' => 'required', |
|
60
|
|
|
'medias' => 'required|array', |
|
61
|
|
|
'medias.*' => 'required|file' |
|
62
|
|
|
]); |
|
63
|
|
|
|
|
64
|
|
|
if ($validator->fails()) { |
|
65
|
|
|
return response()->json(['status' => 'error', 'errors' => $validator->messages()]); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$location = $request->get('location'); |
|
69
|
|
|
|
|
70
|
|
|
foreach ($request->file('medias') as $media) { |
|
71
|
|
|
/** @var \Illuminate\Http\UploadedFile $media */ |
|
72
|
|
|
$media->store($location, $this->getDefaultDiskDriver()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return response()->json(['status' => 'success']); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function createDirectory(Request $request) |
|
79
|
|
|
{ |
|
80
|
|
|
$data = $request->all(); |
|
81
|
|
|
$validator = \Validator::make($data, [ |
|
82
|
|
|
'name' => 'required', // TODO: check if the folder does not exists |
|
83
|
|
|
'location' => 'required', |
|
84
|
|
|
]); |
|
85
|
|
|
|
|
86
|
|
|
$path = trim($data['location'], '/') . '/' . str_slug($data['name']); |
|
87
|
|
|
|
|
88
|
|
|
if ($validator->fails()) { |
|
89
|
|
|
return response()->json([ |
|
90
|
|
|
'status' => 'error', |
|
91
|
|
|
], 400); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$this->disk()->makeDirectory($path); |
|
95
|
|
|
|
|
96
|
|
|
return response()->json([ |
|
97
|
|
|
'status' => 'success', |
|
98
|
|
|
'data' => compact('path'), |
|
99
|
|
|
]); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function renameMedia(Request $request) |
|
103
|
|
|
{ |
|
104
|
|
|
$data = $request->all(); |
|
105
|
|
|
|
|
106
|
|
|
// TODO: check if the folder does not exists |
|
107
|
|
|
$validator = \Validator::make($data, [ |
|
108
|
|
|
'media' => 'required', |
|
109
|
|
|
'newName' => 'required', |
|
110
|
|
|
'location' => 'required', |
|
111
|
|
|
]); |
|
112
|
|
|
|
|
113
|
|
|
if ($validator->fails()) { |
|
114
|
|
|
return response()->json([ |
|
115
|
|
|
'status' => 'error', |
|
116
|
|
|
], 400); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$location = trim($data['location'], '/'); |
|
120
|
|
|
$media = $data['media']; |
|
121
|
|
|
$src = $location . '/' . $media['name']; |
|
122
|
|
|
|
|
123
|
|
|
if ($media['type'] == 'file') { |
|
124
|
|
|
$ext = pathinfo($media['url'], PATHINFO_EXTENSION); |
|
125
|
|
|
$dest = $location . '/' . Str::slug(str_replace(".{$ext}", '', $data['newName'])).'.'.$ext; |
|
126
|
|
|
$this->disk()->move($src, $dest); |
|
127
|
|
|
|
|
128
|
|
|
return response()->json([ |
|
129
|
|
|
'status' => 'success', |
|
130
|
|
|
'data' => ['path' => $dest], |
|
131
|
|
|
]); |
|
132
|
|
|
} |
|
133
|
|
|
elseif ($media['type'] == 'directory') { |
|
134
|
|
|
$dest = $location . '/' . Str::slug($data['newName']); |
|
135
|
|
|
$this->disk()->move($src, $dest); |
|
136
|
|
|
|
|
137
|
|
|
return response()->json([ |
|
138
|
|
|
'status' => 'success', |
|
139
|
|
|
'data' => ['path' => $dest], |
|
140
|
|
|
]); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return response()->json([ |
|
144
|
|
|
'status' => 'error', |
|
145
|
|
|
'message' => 'Something wrong was happened while renaming the media.', |
|
146
|
|
|
], 400); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function deleteMedia(Request $request) |
|
150
|
|
|
{ |
|
151
|
|
|
// TODO: Add validation |
|
152
|
|
|
$data = $request->all(); |
|
153
|
|
|
|
|
154
|
|
|
if ($data['media']['type'] == 'file') { |
|
155
|
|
|
$deleted = $this->disk()->delete($data['media']['path']); |
|
156
|
|
|
} |
|
157
|
|
|
else { |
|
158
|
|
|
$path = trim($data['media']['path'], '/'); |
|
159
|
|
|
|
|
160
|
|
|
$deleted = $this->disk()->deleteDirectory($path); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return response()->json(['status' => $deleted ? 'success' : 'error']); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
167
|
|
|
| Other Functions |
|
168
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
169
|
|
|
*/ |
|
170
|
|
|
/** |
|
171
|
|
|
* Get the default disk driver. |
|
172
|
|
|
* |
|
173
|
|
|
* @return \Illuminate\Filesystem\FilesystemAdapter |
|
174
|
|
|
*/ |
|
175
|
|
|
private function getDefaultDiskDriver() |
|
176
|
|
|
{ |
|
177
|
|
|
return config('arcanesoft.media.filesystem.default'); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Get the disk adapter. |
|
182
|
|
|
* |
|
183
|
|
|
* @return \Illuminate\Filesystem\FilesystemAdapter |
|
184
|
|
|
*/ |
|
185
|
|
|
private function disk() |
|
186
|
|
|
{ |
|
187
|
|
|
return Storage::disk($this->getDefaultDiskDriver()); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @param string $location |
|
192
|
|
|
* |
|
193
|
|
|
* @return array |
|
194
|
|
|
*/ |
|
195
|
|
|
private function getDirectoriesFromLocation($location) |
|
196
|
|
|
{ |
|
197
|
|
|
return array_map(function ($directory) use ($location) { |
|
198
|
|
|
return [ |
|
199
|
|
|
'name' => str_replace("$location/", '', $directory), |
|
200
|
|
|
'path' => $directory, |
|
201
|
|
|
'type' => 'directory', |
|
202
|
|
|
]; |
|
203
|
|
|
}, $this->disk()->directories($location)); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @param string $location |
|
208
|
|
|
* |
|
209
|
|
|
* @return array |
|
210
|
|
|
*/ |
|
211
|
|
|
private function getFilesFromLocation($location) |
|
212
|
|
|
{ |
|
213
|
|
|
$disk = $this->disk(); |
|
214
|
|
|
|
|
215
|
|
|
return array_map(function ($path) use ($disk, $location) { |
|
216
|
|
|
return [ |
|
217
|
|
|
'name' => str_replace("$location/", '', $path), |
|
218
|
|
|
'type' => 'file', |
|
219
|
|
|
'path' => $path, |
|
220
|
|
|
'url' => $disk->url($path), |
|
221
|
|
|
'mimetype' => $disk->mimeType($path), |
|
222
|
|
|
'lastModified' => Carbon::createFromTimestamp($disk->lastModified($path))->toDateTimeString(), |
|
223
|
|
|
'visibility' => $disk->getVisibility($path), |
|
224
|
|
|
'size' => $disk->size($path), |
|
225
|
|
|
]; |
|
226
|
|
|
}, $disk->files($location)); |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|