1
|
|
|
<?php namespace Arcanesoft\Media; |
2
|
|
|
|
3
|
|
|
use Arcanesoft\Media\Entities\DirectoryItem; |
4
|
|
|
use Arcanesoft\Media\Entities\FileItem; |
5
|
|
|
use Arcanesoft\Media\Entities\MediaCollection; |
6
|
|
|
use Arcanesoft\Media\Exceptions\DirectoryNotFoundException; |
7
|
|
|
use Arcanesoft\Media\Exceptions\FileNotFoundException; |
8
|
|
|
use Carbon\Carbon; |
9
|
|
|
use Illuminate\Contracts\Filesystem\Factory; |
10
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem; |
11
|
|
|
use phpDocumentor\Reflection\File; |
12
|
|
|
|
13
|
|
|
class MediaManager |
14
|
|
|
{ |
15
|
|
|
/* ----------------------------------------------------------------- |
16
|
|
|
| Properties |
17
|
|
|
| ----------------------------------------------------------------- |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \Illuminate\Contracts\Filesystem\Factory |
22
|
|
|
*/ |
23
|
|
|
private $filesystem; |
24
|
|
|
|
25
|
|
|
/* ----------------------------------------------------------------- |
26
|
|
|
| Constructor |
27
|
|
|
| ----------------------------------------------------------------- |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* MediaManager constructor. |
32
|
|
|
* |
33
|
|
|
* @param \Illuminate\Contracts\Filesystem\Factory $filesystem |
34
|
|
|
*/ |
35
|
|
|
public function __construct(Factory $filesystem) |
36
|
|
|
{ |
37
|
|
|
$this->filesystem = $filesystem; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get the filesystem's storage disk. |
42
|
|
|
* |
43
|
|
|
* @param string|null $name |
44
|
|
|
* |
45
|
|
|
* @return \Illuminate\Contracts\Filesystem\Filesystem|\Illuminate\Filesystem\FilesystemAdapter |
46
|
|
|
*/ |
47
|
|
|
public function disk($name = null): Filesystem |
48
|
|
|
{ |
49
|
|
|
return $this->filesystem->disk( |
50
|
|
|
$name ?: $this->getDefaultDisk() |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the default disk. |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function getDefaultDisk(): string |
60
|
|
|
{ |
61
|
|
|
return 'media'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/* ----------------------------------------------------------------- |
65
|
|
|
| Main Methods |
66
|
|
|
| ----------------------------------------------------------------- |
67
|
|
|
*/ |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string|null $directory |
71
|
|
|
* @param bool $recursive |
72
|
|
|
* |
73
|
|
|
* @return \Illuminate\Support\Collection |
74
|
|
|
*/ |
75
|
|
|
public function all(string $directory = null, bool $recursive = false) |
76
|
|
|
{ |
77
|
|
|
return $this->directories($directory, $recursive) |
78
|
|
|
->merge( |
79
|
|
|
$this->files($directory, $recursive) |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get all of the directories within a given directory. |
85
|
|
|
* |
86
|
|
|
* @param string|null $directory |
87
|
|
|
* @param bool $recursive |
88
|
|
|
* |
89
|
|
|
* @return \Illuminate\Support\Collection |
90
|
|
|
*/ |
91
|
|
|
public function directories(string $directory = null, bool $recursive = false) |
92
|
|
|
{ |
93
|
|
|
$disk = $this->disk(); |
94
|
|
|
$directories = $disk->directories($directory, $recursive); |
95
|
|
|
|
96
|
|
|
return MediaCollection::directories(array_map(function ($path) use ($disk) { |
|
|
|
|
97
|
|
|
return [ |
98
|
|
|
'name' => basename($path), |
99
|
|
|
]; |
100
|
|
|
}, array_combine($directories, $directories))); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get an array of all files in a directory. |
105
|
|
|
* |
106
|
|
|
* @param string|null $directory |
107
|
|
|
* @param bool $recursive |
108
|
|
|
* |
109
|
|
|
* @return \Illuminate\Support\Collection |
110
|
|
|
*/ |
111
|
|
|
public function files(string $directory = null, bool $recursive = false) |
112
|
|
|
{ |
113
|
|
|
$disk = $this->disk(); |
114
|
|
|
$files = $disk->files($directory, $recursive); |
115
|
|
|
|
116
|
|
|
return MediaCollection::files(array_map(function ($path) use ($disk) { |
117
|
|
|
return [ |
118
|
|
|
'name' => basename($path), |
119
|
|
|
'path' => $path, |
120
|
|
|
'url' => $disk->url($path), |
121
|
|
|
'mimetype' => $disk->mimeType($path), |
122
|
|
|
'lastModified' => Carbon::createFromTimestamp($disk->lastModified($path))->toDateTimeString(), |
123
|
|
|
'visibility' => $disk->getVisibility($path), |
124
|
|
|
'size' => $disk->size($path), |
125
|
|
|
]; |
126
|
|
|
}, array_combine($files, $files))); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get the file. |
131
|
|
|
* |
132
|
|
|
* @param string $path |
133
|
|
|
* |
134
|
|
|
* @return \Arcanesoft\Media\Entities\FileItem |
135
|
|
|
*/ |
136
|
|
|
public function file(string $path) |
137
|
|
|
{ |
138
|
|
|
return $this->files(dirname($path))->first(function (FileItem $file) use ($path) { |
139
|
|
|
return $file->path === $path; |
140
|
|
|
}, function () use ($path) { |
141
|
|
|
throw new FileNotFoundException("File [$path] not found!"); |
142
|
|
|
}); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function directory(string $path) |
146
|
|
|
{ |
147
|
|
|
return $this->directories(dirname($path))->first(function (DirectoryItem $directory) use ($path) { |
148
|
|
|
return $directory->path === $path; |
149
|
|
|
}, function () use ($path) { |
150
|
|
|
throw new DirectoryNotFoundException("Directory [$path] not found!"); |
151
|
|
|
}); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Check if the path exists. |
156
|
|
|
* |
157
|
|
|
* @param string $path |
158
|
|
|
* |
159
|
|
|
* @return bool |
160
|
|
|
*/ |
161
|
|
|
public function exists($path): bool |
162
|
|
|
{ |
163
|
|
|
if ($path === '/') |
164
|
|
|
return true; |
165
|
|
|
|
166
|
|
|
return $this->disk()->exists($path); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Store the uploaded file on the disk. |
171
|
|
|
* |
172
|
|
|
* @param string $path |
173
|
|
|
* @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile $file |
174
|
|
|
* @param array $options |
175
|
|
|
* |
176
|
|
|
* @return string|false |
177
|
|
|
*/ |
178
|
|
|
public function putFile(string $path, $file, array $options = []) |
179
|
|
|
{ |
180
|
|
|
return $this->disk()->putFile($path, $file, $options); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Make a directory. |
185
|
|
|
* |
186
|
|
|
* @param string $path |
187
|
|
|
* |
188
|
|
|
* @return bool |
189
|
|
|
*/ |
190
|
|
|
public function makeDirectory(string $path): bool |
191
|
|
|
{ |
192
|
|
|
return $this->disk()->makeDirectory($path); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Rename the media. |
197
|
|
|
* |
198
|
|
|
* @param string $oldName |
199
|
|
|
* @param string $newName |
200
|
|
|
* |
201
|
|
|
* @return bool |
202
|
|
|
*/ |
203
|
|
|
public function rename(string $oldName, string $newName): bool |
204
|
|
|
{ |
205
|
|
|
return $this->disk()->rename($oldName, $newName); |
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Move the media. |
210
|
|
|
* |
211
|
|
|
* @param string $from |
212
|
|
|
* @param string $to |
213
|
|
|
* |
214
|
|
|
* @return bool |
215
|
|
|
*/ |
216
|
|
|
public function move(string $from, string $to): bool |
217
|
|
|
{ |
218
|
|
|
return $this->disk()->move($from, $to); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Delete the file at a given path. |
223
|
|
|
* |
224
|
|
|
* @param string|array $paths |
225
|
|
|
* |
226
|
|
|
* @return bool |
227
|
|
|
*/ |
228
|
|
|
public function deleteFile($paths): bool |
229
|
|
|
{ |
230
|
|
|
return $this->disk()->delete($paths); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Recursively delete a directory. |
235
|
|
|
* |
236
|
|
|
* @param string $directory |
237
|
|
|
* |
238
|
|
|
* @return bool |
239
|
|
|
*/ |
240
|
|
|
public function deleteDirectory(string $directory): bool |
241
|
|
|
{ |
242
|
|
|
return $this->disk()->deleteDirectory($directory); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Create a streamed download response for a given file. |
247
|
|
|
* |
248
|
|
|
* @param string $path |
249
|
|
|
* @param string|null $name |
250
|
|
|
* @param array|null $headers |
251
|
|
|
* |
252
|
|
|
* @return \Symfony\Component\HttpFoundation\StreamedResponse |
253
|
|
|
*/ |
254
|
|
|
public function download(string $path, string $name = null, array $headers = []) |
255
|
|
|
{ |
256
|
|
|
return $this->disk()->download($path, $name, $headers); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
This check looks for imports that have been defined, but are not used in the scope.