1 | <?php namespace Arcanesoft\Media; |
||
19 | class Media implements MediaContract |
||
20 | { |
||
21 | /* ----------------------------------------------------------------- |
||
22 | | Constants |
||
23 | | ----------------------------------------------------------------- |
||
24 | */ |
||
25 | |||
26 | const VERSION = '2.2.2'; |
||
27 | |||
28 | /* ----------------------------------------------------------------- |
||
29 | | Properties |
||
30 | | ----------------------------------------------------------------- |
||
31 | */ |
||
32 | |||
33 | /** |
||
34 | * The application instance. |
||
35 | * |
||
36 | * @var \Illuminate\Contracts\Foundation\Application |
||
37 | */ |
||
38 | protected $app; |
||
39 | |||
40 | /* ----------------------------------------------------------------- |
||
41 | | Constructor |
||
42 | | ----------------------------------------------------------------- |
||
43 | */ |
||
44 | |||
45 | /** |
||
46 | * Media constructor. |
||
47 | * |
||
48 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
49 | */ |
||
50 | 60 | public function __construct(Application $app) |
|
54 | |||
55 | /* ----------------------------------------------------------------- |
||
56 | | Getters & Setters |
||
57 | | ----------------------------------------------------------------- |
||
58 | */ |
||
59 | |||
60 | /** |
||
61 | * Get the Filesystem Manager instance. |
||
62 | * |
||
63 | * @return \Illuminate\Contracts\Filesystem\Factory |
||
64 | */ |
||
65 | 40 | public function filesystem() |
|
69 | |||
70 | /** |
||
71 | * Get the Config Repository. |
||
72 | * |
||
73 | * @return \Illuminate\Contracts\Config\Repository |
||
74 | */ |
||
75 | 44 | protected function config() |
|
79 | |||
80 | /** |
||
81 | * Get the default disk name. |
||
82 | * |
||
83 | * @return string |
||
84 | */ |
||
85 | 40 | public function getDefaultDiskName() |
|
89 | |||
90 | /** |
||
91 | * Get excluded directories. |
||
92 | * |
||
93 | * @return array |
||
94 | */ |
||
95 | 22 | public function getExcludedDirectories() |
|
96 | { |
||
97 | 22 | return Helpers\ExcludePattern::directories( |
|
98 | 22 | $this->config()->get('arcanesoft.media.directories.excluded', []) |
|
99 | ); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Get excluded files. |
||
104 | * |
||
105 | * @return array |
||
106 | */ |
||
107 | 16 | public function getExcludedFiles() |
|
111 | |||
112 | /* ----------------------------------------------------------------- |
||
113 | | Main Methods |
||
114 | | ----------------------------------------------------------------- |
||
115 | */ |
||
116 | |||
117 | /** |
||
118 | * Get a filesystem adapter. |
||
119 | * |
||
120 | * @param string|null $driver |
||
121 | * |
||
122 | * @return \Illuminate\Filesystem\FilesystemAdapter|\Illuminate\Contracts\Filesystem\Filesystem |
||
123 | */ |
||
124 | 40 | public function disk($driver = null) |
|
128 | |||
129 | /** |
||
130 | * Get all the directories & files from a given location. |
||
131 | * |
||
132 | * @param string $directory |
||
133 | * |
||
134 | * @return array |
||
135 | */ |
||
136 | 6 | public function all($directory = '/') |
|
148 | |||
149 | /** |
||
150 | * Get all of the directories within a given directory. |
||
151 | * |
||
152 | * @param string $directory |
||
153 | * |
||
154 | * @return \Arcanesoft\Media\Entities\DirectoryCollection |
||
155 | */ |
||
156 | 14 | public function directories($directory) |
|
157 | { |
||
158 | 14 | $this->checkDirectory($directory); |
|
159 | |||
160 | $directories = array_map(function ($dir) use ($directory) { |
||
161 | return [ |
||
162 | 10 | 'name' => str_replace("$directory/", '', $dir), |
|
163 | 10 | 'path' => $dir, |
|
164 | ]; |
||
165 | 10 | }, $this->disk()->directories($directory)); |
|
166 | |||
167 | 10 | return DirectoryCollection::make($directories) |
|
168 | 10 | ->exclude($this->getExcludedDirectories()); |
|
169 | } |
||
170 | |||
171 | /** |
||
172 | * Get a collection of all files in a directory. |
||
173 | * |
||
174 | * @param string $directory |
||
175 | * |
||
176 | * @return \Arcanesoft\Media\Entities\FileCollection |
||
177 | */ |
||
178 | 14 | public function files($directory) |
|
179 | { |
||
180 | 14 | $this->checkDirectory($directory); |
|
181 | |||
182 | 14 | $disk = $this->disk(); |
|
183 | |||
184 | // TODO: Add a feature to exclude unwanted files. |
||
185 | $files = array_map(function ($filePath) use ($disk, $directory) { |
||
186 | return [ |
||
187 | 12 | 'name' => str_replace("$directory/", '', $filePath), |
|
188 | 12 | 'path' => $filePath, |
|
189 | 12 | 'url' => $disk->url($filePath), |
|
190 | 12 | 'mimetype' => $disk->mimeType($filePath), |
|
191 | 12 | 'lastModified' => Carbon::createFromTimestamp($disk->lastModified($filePath))->toDateTimeString(), |
|
192 | 12 | 'visibility' => $disk->getVisibility($filePath), |
|
193 | 12 | 'size' => $disk->size($filePath), |
|
194 | ]; |
||
195 | 14 | }, $disk->files($directory)); |
|
196 | |||
197 | 14 | return FileCollection::make($files)->exclude($this->getExcludedFiles()); |
|
198 | } |
||
199 | |||
200 | /** |
||
201 | * Get the file details. |
||
202 | * |
||
203 | * @param string $path |
||
204 | * |
||
205 | * @return array |
||
206 | */ |
||
207 | 4 | public function file($path) |
|
215 | |||
216 | /** |
||
217 | * Store an array of files. |
||
218 | * |
||
219 | * @param string $directory |
||
220 | * @param array $files |
||
221 | * |
||
222 | * @return array |
||
223 | */ |
||
224 | 4 | public function storeMany($directory, array $files) |
|
235 | |||
236 | /** |
||
237 | * Store a file. |
||
238 | * |
||
239 | * @param string $path |
||
240 | * @param \Illuminate\Http\UploadedFile $file |
||
241 | * @param array $options |
||
242 | * |
||
243 | * @return string|false |
||
244 | */ |
||
245 | 8 | public function store($path, UploadedFile $file, array $options = []) |
|
255 | |||
256 | /** |
||
257 | * Create a directory. |
||
258 | * |
||
259 | * @param string $path |
||
260 | * |
||
261 | * @return bool |
||
262 | */ |
||
263 | 8 | public function makeDirectory($path) |
|
271 | |||
272 | /** |
||
273 | * Delete a directory. |
||
274 | * |
||
275 | * @param string $directory |
||
276 | * |
||
277 | * @return bool |
||
278 | */ |
||
279 | 8 | public function deleteDirectory($directory) |
|
287 | |||
288 | /** |
||
289 | * Delete a file. |
||
290 | * |
||
291 | * @param string $path |
||
292 | * |
||
293 | * @return bool |
||
294 | */ |
||
295 | 6 | public function deleteFile($path) |
|
303 | |||
304 | /** |
||
305 | * Move a file to a new location. |
||
306 | * |
||
307 | * @param string $from |
||
308 | * @param string $to |
||
309 | * |
||
310 | * @return bool |
||
311 | */ |
||
312 | 8 | public function move($from, $to) |
|
320 | |||
321 | /* ----------------------------------------------------------------- |
||
322 | | Check Methods |
||
323 | | ----------------------------------------------------------------- |
||
324 | */ |
||
325 | |||
326 | /** |
||
327 | * Determine if a file/directory exists. |
||
328 | * |
||
329 | * @param string $path |
||
330 | * |
||
331 | * @return bool |
||
332 | */ |
||
333 | 26 | public function exists($path) |
|
337 | |||
338 | /** |
||
339 | * Check if the directory is excluded. |
||
340 | * |
||
341 | * @param string $directory |
||
342 | * |
||
343 | * @return bool |
||
344 | */ |
||
345 | 20 | public function isExcludedDirectory($directory) |
|
353 | |||
354 | /* ----------------------------------------------------------------- |
||
355 | | Other Methods |
||
356 | | ----------------------------------------------------------------- |
||
357 | */ |
||
358 | |||
359 | /** |
||
360 | * Check the given directory location. |
||
361 | * |
||
362 | * @param string &$directory |
||
363 | * |
||
364 | * @throws \Arcanesoft\Media\Exceptions\DirectoryNotFound |
||
365 | * @throws \Arcanesoft\Media\Exceptions\AccessNotAllowed |
||
366 | */ |
||
367 | 22 | protected function checkDirectory(&$directory) |
|
374 | |||
375 | /** |
||
376 | * Check if the directory exists. |
||
377 | * |
||
378 | * @param string $directory |
||
379 | * |
||
380 | * @throws \Arcanesoft\Media\Exceptions\DirectoryNotFound |
||
381 | */ |
||
382 | 22 | protected function checkDirectoryExists($directory) |
|
388 | |||
389 | /** |
||
390 | * Check if can access the directory. |
||
391 | * |
||
392 | * @param string $directory |
||
393 | * |
||
394 | * @throws \Arcanesoft\Media\Exceptions\AccessNotAllowed |
||
395 | */ |
||
396 | 20 | protected function checkDirectoryAccess($directory) |
|
402 | } |
||
403 |