1
|
|
|
<?php namespace Arcanesoft\Media; |
2
|
|
|
|
3
|
|
|
use Arcanesoft\Media\Contracts\Media as MediaContract; |
4
|
|
|
use Carbon\Carbon; |
5
|
|
|
use Illuminate\Contracts\Foundation\Application; |
6
|
|
|
use Illuminate\Http\UploadedFile; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Media |
10
|
|
|
* |
11
|
|
|
* @package Arcanesoft\Media |
12
|
|
|
* @author ARCANEDEV <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Media implements MediaContract |
15
|
|
|
{ |
16
|
|
|
/* ----------------------------------------------------------------- |
17
|
|
|
| Properties |
18
|
|
|
| ----------------------------------------------------------------- |
19
|
|
|
*/ |
20
|
|
|
/** |
21
|
|
|
* The application instance. |
22
|
|
|
* |
23
|
|
|
* @var \Illuminate\Contracts\Foundation\Application |
24
|
|
|
*/ |
25
|
|
|
protected $app; |
26
|
|
|
|
27
|
|
|
/* ----------------------------------------------------------------- |
28
|
|
|
| Constructor |
29
|
|
|
| ----------------------------------------------------------------- |
30
|
|
|
*/ |
31
|
|
|
/** |
32
|
|
|
* Media constructor. |
33
|
|
|
* |
34
|
6 |
|
* @param \Illuminate\Contracts\Foundation\Application $app |
35
|
|
|
*/ |
36
|
6 |
|
public function __construct(Application $app) |
37
|
6 |
|
{ |
38
|
|
|
$this->app = $app; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/* ----------------------------------------------------------------- |
42
|
|
|
| Getters & Setters |
43
|
|
|
| ----------------------------------------------------------------- |
44
|
|
|
*/ |
45
|
|
|
/** |
46
|
|
|
* Get the Filesystem Manager instance. |
47
|
|
|
* |
48
|
3 |
|
* @return \Illuminate\Contracts\Filesystem\Factory |
49
|
|
|
*/ |
50
|
3 |
|
public function filesystem() |
51
|
|
|
{ |
52
|
|
|
return $this->app->make('filesystem'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the Config Repository. |
57
|
|
|
* |
58
|
3 |
|
* @return \Illuminate\Contracts\Config\Repository |
59
|
|
|
*/ |
60
|
3 |
|
protected function config() |
61
|
|
|
{ |
62
|
|
|
return $this->app->make('config'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the default disk name. |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function getDefaultDiskName() |
71
|
|
|
{ |
72
|
|
|
return $this->config()->get('arcanesoft.media.filesystem.default'); |
73
|
|
|
} |
74
|
3 |
|
|
75
|
|
|
/* ----------------------------------------------------------------- |
76
|
3 |
|
| Main Methods |
77
|
|
|
| ----------------------------------------------------------------- |
78
|
|
|
*/ |
79
|
|
|
/** |
80
|
|
|
* Get a filesystem adapter. |
81
|
|
|
* |
82
|
|
|
* @param string|null $driver |
83
|
|
|
* |
84
|
3 |
|
* @return \Illuminate\Filesystem\FilesystemAdapter|\Illuminate\Contracts\Filesystem\Filesystem |
85
|
|
|
*/ |
86
|
3 |
|
public function disk($driver = null) |
87
|
3 |
|
{ |
88
|
1 |
|
return $this->filesystem()->disk($driver); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the default filesystem adapter. |
93
|
|
|
* |
94
|
|
|
* @return \Illuminate\Filesystem\FilesystemAdapter|\Illuminate\Contracts\Filesystem\Filesystem |
95
|
|
|
*/ |
96
|
|
|
public function defaultDisk() |
97
|
|
|
{ |
98
|
|
|
return $this->disk($this->getDefaultDiskName()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get all of the directories within a given directory. |
103
|
|
|
* |
104
|
|
|
* @param string $directory |
105
|
|
|
* |
106
|
|
|
* @return \Arcanesoft\Media\Entities\DirectoryCollection |
107
|
|
|
*/ |
108
|
|
|
public function directories($directory) |
109
|
|
|
{ |
110
|
|
|
$directories = array_map(function ($dir) use ($directory) { |
111
|
|
|
return [ |
112
|
|
|
'name' => str_replace("$directory/", '', $dir), |
113
|
|
|
'path' => $dir, |
114
|
|
|
]; |
115
|
|
|
}, $this->defaultDisk()->directories($directory)); |
116
|
|
|
|
117
|
|
|
return Entities\DirectoryCollection::make($directories); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get a collection of all files in a directory. |
122
|
|
|
* |
123
|
|
|
* @param string $directory |
124
|
|
|
* |
125
|
|
|
* @return \Arcanesoft\Media\Entities\FileCollection |
126
|
|
|
*/ |
127
|
|
|
public function files($directory) |
128
|
|
|
{ |
129
|
|
|
$disk = $this->defaultDisk(); |
130
|
|
|
|
131
|
|
|
// TODO: Add a feature to exclude unwanted files. |
132
|
|
|
$files = array_map(function ($filePath) use ($disk, $directory) { |
133
|
|
|
return [ |
134
|
|
|
'name' => str_replace("$directory/", '', $filePath), |
135
|
|
|
'path' => $filePath, |
136
|
|
|
'url' => $disk->url($filePath), |
137
|
|
|
'mimetype' => $disk->mimeType($filePath), |
138
|
|
|
'lastModified' => Carbon::createFromTimestamp($disk->lastModified($filePath))->toDateTimeString(), |
139
|
|
|
'visibility' => $disk->getVisibility($filePath), |
140
|
|
|
'size' => $disk->size($filePath), |
141
|
|
|
]; |
142
|
|
|
}, $disk->files($directory)); |
143
|
|
|
|
144
|
|
|
return Entities\FileCollection::make($files); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get all the directories & files from a given location. |
149
|
|
|
* |
150
|
|
|
* @param string $directory |
151
|
|
|
* |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
|
|
public function all($directory) |
155
|
|
|
{ |
156
|
|
|
$directories = $this->directories($directory)->transform(function ($item) { |
157
|
|
|
return $item + ['type' => self::MEDIA_TYPE_DIRECTORY]; |
158
|
|
|
})->toArray(); |
159
|
|
|
|
160
|
|
|
$files = $this->files($directory)->transform(function (array $item) { |
161
|
|
|
return $item + ['type' => self::MEDIA_TYPE_FILE]; |
162
|
|
|
})->toArray(); |
163
|
|
|
|
164
|
|
|
return array_merge($directories, $files); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Store an array of files. |
169
|
|
|
* |
170
|
|
|
* @param string $directory |
171
|
|
|
* @param array $files |
172
|
|
|
*/ |
173
|
|
|
public function storeMany($directory, array $files) |
174
|
|
|
{ |
175
|
|
|
foreach ($files as $file) { |
176
|
|
|
$this->store($directory, $file); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Store a file. |
182
|
|
|
* |
183
|
|
|
* @param string $directory |
184
|
|
|
* @param \Illuminate\Http\UploadedFile $file |
185
|
|
|
* |
186
|
|
|
* @return string|false |
187
|
|
|
*/ |
188
|
|
|
public function store($directory, UploadedFile $file) |
189
|
|
|
{ |
190
|
|
|
return $file->store($directory, $this->getDefaultDiskName()); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Create a directory. |
195
|
|
|
* |
196
|
|
|
* @param string $path |
197
|
|
|
* |
198
|
|
|
* @return bool |
199
|
|
|
*/ |
200
|
|
|
public function makeDirectory($path) |
201
|
|
|
{ |
202
|
|
|
return $this->defaultDisk()->makeDirectory($path); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Move a file to a new location. |
207
|
|
|
* |
208
|
|
|
* @param string $from |
209
|
|
|
* @param string $to |
210
|
|
|
* |
211
|
|
|
* @return bool |
212
|
|
|
*/ |
213
|
|
|
public function move($from, $to) |
214
|
|
|
{ |
215
|
|
|
return $this->defaultDisk()->move($from, $to); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|