1
|
|
|
<?php namespace Arcanesoft\Media; |
2
|
|
|
|
3
|
|
|
use Arcanesoft\Media\Contracts\Media as MediaContract; |
4
|
|
|
use Arcanesoft\Media\Exceptions\DirectoryNotFound; |
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Contracts\Foundation\Application; |
7
|
|
|
use Illuminate\Http\UploadedFile; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Media |
12
|
|
|
* |
13
|
|
|
* @package Arcanesoft\Media |
14
|
|
|
* @author ARCANEDEV <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class Media implements MediaContract |
17
|
|
|
{ |
18
|
|
|
/* ----------------------------------------------------------------- |
19
|
|
|
| Properties |
20
|
|
|
| ----------------------------------------------------------------- |
21
|
|
|
*/ |
22
|
|
|
/** |
23
|
|
|
* The application instance. |
24
|
|
|
* |
25
|
|
|
* @var \Illuminate\Contracts\Foundation\Application |
26
|
|
|
*/ |
27
|
|
|
protected $app; |
28
|
|
|
|
29
|
|
|
/* ----------------------------------------------------------------- |
30
|
|
|
| Constructor |
31
|
|
|
| ----------------------------------------------------------------- |
32
|
|
|
*/ |
33
|
|
|
/** |
34
|
|
|
* Media constructor. |
35
|
|
|
* |
36
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
37
|
|
|
*/ |
38
|
6 |
|
public function __construct(Application $app) |
39
|
|
|
{ |
40
|
6 |
|
$this->app = $app; |
41
|
6 |
|
} |
42
|
|
|
|
43
|
|
|
/* ----------------------------------------------------------------- |
44
|
|
|
| Getters & Setters |
45
|
|
|
| ----------------------------------------------------------------- |
46
|
|
|
*/ |
47
|
|
|
/** |
48
|
|
|
* Get the Filesystem Manager instance. |
49
|
|
|
* |
50
|
|
|
* @return \Illuminate\Contracts\Filesystem\Factory |
51
|
|
|
*/ |
52
|
3 |
|
public function filesystem() |
53
|
|
|
{ |
54
|
3 |
|
return $this->app->make('filesystem'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the Config Repository. |
59
|
|
|
* |
60
|
|
|
* @return \Illuminate\Contracts\Config\Repository |
61
|
|
|
*/ |
62
|
3 |
|
protected function config() |
63
|
|
|
{ |
64
|
3 |
|
return $this->app->make('config'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the default disk name. |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
3 |
|
public function getDefaultDiskName() |
73
|
|
|
{ |
74
|
3 |
|
return $this->config()->get('arcanesoft.media.filesystem.default'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get excluded directories. |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
public function getExcludedDirectories() |
83
|
|
|
{ |
84
|
|
|
return Helpers\ExcludePattern::directories( |
85
|
|
|
$this->config()->get('arcanesoft.media.directories.excluded', []) |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get excluded files. |
91
|
|
|
* |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
public function getExcludedFiles() |
95
|
|
|
{ |
96
|
|
|
return $this->config()->get('arcanesoft.media.files.excluded', []); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/* ----------------------------------------------------------------- |
100
|
|
|
| Main Methods |
101
|
|
|
| ----------------------------------------------------------------- |
102
|
|
|
*/ |
103
|
|
|
/** |
104
|
|
|
* Get a filesystem adapter. |
105
|
|
|
* |
106
|
|
|
* @param string|null $driver |
107
|
|
|
* |
108
|
|
|
* @return \Illuminate\Filesystem\FilesystemAdapter|\Illuminate\Contracts\Filesystem\Filesystem |
109
|
|
|
*/ |
110
|
3 |
|
public function disk($driver = null) |
111
|
|
|
{ |
112
|
3 |
|
return $this->filesystem()->disk($driver); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get the default filesystem adapter. |
117
|
|
|
* |
118
|
|
|
* @return \Illuminate\Filesystem\FilesystemAdapter|\Illuminate\Contracts\Filesystem\Filesystem |
119
|
|
|
*/ |
120
|
3 |
|
public function defaultDisk() |
121
|
|
|
{ |
122
|
3 |
|
return $this->disk($this->getDefaultDiskName()); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get all the directories & files from a given location. |
127
|
|
|
* |
128
|
|
|
* @param string $directory |
129
|
|
|
* |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
public function all($directory) |
133
|
|
|
{ |
134
|
|
|
$directories = $this->directories($directory)->transform(function ($item) { |
135
|
|
|
return $item + ['type' => self::MEDIA_TYPE_DIRECTORY]; |
136
|
|
|
})->toArray(); |
137
|
|
|
|
138
|
|
|
$files = $this->files($directory)->transform(function (array $item) { |
139
|
|
|
return $item + ['type' => self::MEDIA_TYPE_FILE]; |
140
|
|
|
})->toArray(); |
141
|
|
|
|
142
|
|
|
return array_merge($directories, $files); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get all of the directories within a given directory. |
147
|
|
|
* |
148
|
|
|
* @param string $directory |
149
|
|
|
* |
150
|
|
|
* @return \Arcanesoft\Media\Entities\DirectoryCollection |
151
|
|
|
*/ |
152
|
|
|
public function directories($directory) |
153
|
|
|
{ |
154
|
|
|
$this->checkDirectory($directory); |
155
|
|
|
|
156
|
|
|
$directories = array_map(function ($dir) use ($directory) { |
157
|
|
|
return [ |
158
|
|
|
'name' => str_replace("$directory/", '', $dir), |
159
|
|
|
'path' => $dir, |
160
|
|
|
]; |
161
|
|
|
}, $this->defaultDisk()->directories($directory)); |
162
|
|
|
|
163
|
|
|
return Entities\DirectoryCollection::make($directories) |
164
|
|
|
->exclude($this->getExcludedDirectories()); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get a collection of all files in a directory. |
169
|
|
|
* |
170
|
|
|
* @param string $directory |
171
|
|
|
* |
172
|
|
|
* @return \Arcanesoft\Media\Entities\FileCollection |
173
|
|
|
*/ |
174
|
|
|
public function files($directory) |
175
|
|
|
{ |
176
|
|
|
$this->checkDirectory($directory); |
177
|
|
|
|
178
|
|
|
$disk = $this->defaultDisk(); |
179
|
|
|
|
180
|
|
|
// TODO: Add a feature to exclude unwanted files. |
181
|
|
|
$files = array_map(function ($filePath) use ($disk, $directory) { |
182
|
|
|
return [ |
183
|
|
|
'name' => str_replace("$directory/", '', $filePath), |
184
|
|
|
'path' => $filePath, |
185
|
|
|
'url' => $disk->url($filePath), |
186
|
|
|
'mimetype' => $disk->mimeType($filePath), |
187
|
|
|
'lastModified' => Carbon::createFromTimestamp($disk->lastModified($filePath))->toDateTimeString(), |
188
|
|
|
'visibility' => $disk->getVisibility($filePath), |
189
|
|
|
'size' => $disk->size($filePath), |
190
|
|
|
]; |
191
|
|
|
}, $disk->files($directory)); |
192
|
|
|
|
193
|
|
|
return Entities\FileCollection::make($files) |
194
|
|
|
->exclude($this->getExcludedFiles()); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Store an array of files. |
199
|
|
|
* |
200
|
|
|
* @param string $directory |
201
|
|
|
* @param array $files |
202
|
|
|
*/ |
203
|
|
|
public function storeMany($directory, array $files) |
204
|
|
|
{ |
205
|
|
|
foreach ($files as $file) { |
206
|
|
|
$this->store($directory, $file); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Store a file. |
212
|
|
|
* |
213
|
|
|
* @param string $directory |
214
|
|
|
* @param \Illuminate\Http\UploadedFile $file |
215
|
|
|
* |
216
|
|
|
* @return string|false |
217
|
|
|
*/ |
218
|
|
|
public function store($directory, UploadedFile $file) |
219
|
|
|
{ |
220
|
|
|
return $file->store($directory, $this->getDefaultDiskName()); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Create a directory. |
225
|
|
|
* |
226
|
|
|
* @param string $path |
227
|
|
|
* |
228
|
|
|
* @return bool |
229
|
|
|
*/ |
230
|
|
|
public function makeDirectory($path) |
231
|
|
|
{ |
232
|
|
|
return $this->defaultDisk()->makeDirectory($path); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Move a file to a new location. |
237
|
|
|
* |
238
|
|
|
* @param string $from |
239
|
|
|
* @param string $to |
240
|
|
|
* |
241
|
|
|
* @return bool |
242
|
|
|
*/ |
243
|
|
|
public function move($from, $to) |
244
|
|
|
{ |
245
|
|
|
return $this->defaultDisk()->move($from, $to); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/* ----------------------------------------------------------------- |
249
|
|
|
| Check Methods |
250
|
|
|
| ----------------------------------------------------------------- |
251
|
|
|
*/ |
252
|
|
|
/** |
253
|
|
|
* Determine if a file/directory exists. |
254
|
|
|
* |
255
|
|
|
* @param string $path |
256
|
|
|
* |
257
|
|
|
* @return bool |
258
|
|
|
*/ |
259
|
|
|
public function exists($path) |
260
|
|
|
{ |
261
|
|
|
return $this->defaultDisk()->exists($path); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Check if the directory is excluded. |
266
|
|
|
* |
267
|
|
|
* @param string $directory |
268
|
|
|
* |
269
|
|
|
* @return bool |
270
|
|
|
*/ |
271
|
|
|
public function isExcludedDirectory($directory) |
272
|
|
|
{ |
273
|
|
|
foreach ($this->getExcludedDirectories() as $pattern) { |
274
|
|
|
if (Str::is($pattern, $directory)) return true; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
return false; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/* ----------------------------------------------------------------- |
281
|
|
|
| Other Methods |
282
|
|
|
| ----------------------------------------------------------------- |
283
|
|
|
*/ |
284
|
|
|
/** |
285
|
|
|
* Check the given directory location. |
286
|
|
|
* |
287
|
|
|
* @param string &$directory |
288
|
|
|
* |
289
|
|
|
* @throws \Arcanesoft\Media\Exceptions\DirectoryNotFound |
290
|
|
|
* @throws \Arcanesoft\Media\Exceptions\AccessNotAllowed |
291
|
|
|
*/ |
292
|
|
|
protected function checkDirectory(&$directory) |
293
|
|
|
{ |
294
|
|
|
$directory = trim($directory, '/'); |
295
|
|
|
|
296
|
|
|
$this->checkDirectoryExists($directory); |
297
|
|
|
$this->checkDirectoryAccess($directory); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Check if the directory exists. |
302
|
|
|
* |
303
|
|
|
* @param string $directory |
304
|
|
|
* |
305
|
|
|
* @throws \Arcanesoft\Media\Exceptions\DirectoryNotFound |
306
|
|
|
*/ |
307
|
|
|
protected function checkDirectoryExists($directory) |
308
|
|
|
{ |
309
|
|
|
if ( ! empty($directory) && ! $this->exists($directory)) { |
310
|
|
|
throw new DirectoryNotFound("Directory [$directory] not found !", 404); |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Check if can access the directory. |
316
|
|
|
* |
317
|
|
|
* @param string $directory |
318
|
|
|
* |
319
|
|
|
* @throws \Arcanesoft\Media\Exceptions\AccessNotAllowed |
320
|
|
|
*/ |
321
|
|
|
protected function checkDirectoryAccess($directory) |
322
|
|
|
{ |
323
|
|
|
if ($this->isExcludedDirectory($directory)) { |
324
|
|
|
throw new Exceptions\AccessNotAllowed('Access not allowed.', 405); |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
|