|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NwLaravel\FileStorage; |
|
4
|
|
|
|
|
5
|
|
|
use \Exception; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
7
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem as Storage; |
|
8
|
|
|
use Intervention\Image\Image; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class StorageManager |
|
12
|
|
|
*/ |
|
13
|
|
|
class StorageManager |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var Storage |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $storage; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var ImagineFactory |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $imagineFactory; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Construct |
|
27
|
|
|
* |
|
28
|
|
|
* @param Storage $storage |
|
29
|
|
|
* @param ImagineFactory $imagineFactory |
|
30
|
|
|
*/ |
|
31
|
27 |
|
public function __construct(Storage $storage, ImagineFactory $imagineFactory = null) |
|
32
|
|
|
{ |
|
33
|
27 |
|
$this->storage = $storage; |
|
34
|
27 |
|
$this->imagineFactory = $imagineFactory; |
|
35
|
27 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* File Exists |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $filename Path File |
|
41
|
|
|
* |
|
42
|
|
|
* @return bool |
|
43
|
|
|
*/ |
|
44
|
12 |
|
public function exists($filename) |
|
45
|
|
|
{ |
|
46
|
|
|
try { |
|
47
|
12 |
|
return $this->storage->exists($filename); |
|
48
|
|
|
|
|
49
|
1 |
|
} catch (\Exception $e) { |
|
50
|
1 |
|
return false; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get Size |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $filename Path File |
|
58
|
|
|
* |
|
59
|
|
|
* @return bool |
|
60
|
|
|
*/ |
|
61
|
2 |
|
public function size($filename) |
|
62
|
|
|
{ |
|
63
|
|
|
try { |
|
64
|
2 |
|
return intval($this->storage->size($filename)); |
|
65
|
|
|
|
|
66
|
1 |
|
} catch (\Exception $e) { |
|
67
|
1 |
|
return 0; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get MimeType File |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $filename Path File |
|
75
|
|
|
* |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
12 |
|
public function mimeType($filename) |
|
79
|
|
|
{ |
|
80
|
|
|
try { |
|
81
|
12 |
|
return $this->storage->mimeType($filename); |
|
82
|
|
|
|
|
83
|
1 |
|
} catch (\Exception $e) { |
|
84
|
1 |
|
return null; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Path is Directory |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $path Path Directory |
|
92
|
|
|
* |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
10 |
|
public function isDir($path) |
|
96
|
|
|
{ |
|
97
|
10 |
|
$mimeType = $this->mimeType($path); |
|
98
|
|
|
|
|
99
|
10 |
|
if ($this->exists($path) && (!$mimeType || $mimeType == 'directory')) { |
|
100
|
5 |
|
return true; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
5 |
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Is File |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $filename Path File |
|
110
|
|
|
* |
|
111
|
|
|
* @return bool |
|
112
|
|
|
*/ |
|
113
|
6 |
|
public function isFile($filename) |
|
114
|
|
|
{ |
|
115
|
6 |
|
return !$this->isDir($filename); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get Meta Data |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $filename Path File |
|
122
|
|
|
* |
|
123
|
|
|
* @return bool |
|
124
|
|
|
*/ |
|
125
|
2 |
|
public function metaData($filename) |
|
126
|
|
|
{ |
|
127
|
|
|
try { |
|
128
|
2 |
|
return $this->storage->getMetadata($filename); |
|
|
|
|
|
|
129
|
|
|
|
|
130
|
1 |
|
} catch (\Exception $e) { |
|
131
|
1 |
|
return null; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Read Content File |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $filename Path File |
|
139
|
|
|
* |
|
140
|
|
|
* @return bool |
|
141
|
|
|
*/ |
|
142
|
1 |
|
public function readFile($filename) |
|
143
|
|
|
{ |
|
144
|
1 |
|
return $this->storage->get($filename); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Delete File |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $filename Path File |
|
151
|
|
|
* |
|
152
|
|
|
* @return bool |
|
153
|
|
|
*/ |
|
154
|
2 |
|
public function deleteFile($filename) |
|
155
|
|
|
{ |
|
156
|
2 |
|
if ($this->isDir($filename)) { |
|
157
|
1 |
|
return false; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
1 |
|
return $this->storage->delete($filename); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Delete Folder |
|
165
|
|
|
* |
|
166
|
|
|
* @param string $folder |
|
167
|
|
|
* |
|
168
|
|
|
* @return bool |
|
169
|
|
|
*/ |
|
170
|
2 |
|
public function deleteFolder($folder) |
|
171
|
|
|
{ |
|
172
|
2 |
|
if ($this->isFile($folder)) { |
|
173
|
1 |
|
return false; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
1 |
|
return $this->storage->deleteDirectory($folder); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Files in Folder |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $path |
|
183
|
|
|
* @param bool $recursive |
|
184
|
|
|
* |
|
185
|
|
|
* @return array |
|
186
|
1 |
|
*/ |
|
187
|
|
|
public function files($path, $recursive = false) |
|
188
|
1 |
|
{ |
|
189
|
|
|
if ($this->isFile($path)) { |
|
190
|
|
|
return null; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
return $this->storage->files($path, (bool) $recursive); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* UploadFile |
|
198
|
1 |
|
* |
|
199
|
|
|
* @param UploadedFile $file Uploaded File |
|
200
|
1 |
|
* @param string $folder String Folder |
|
201
|
|
|
* @param string $name String Name |
|
202
|
|
|
* @param bool $override Boolean Over Ride |
|
203
|
|
|
* @param array $config Array Config Upload |
|
204
|
|
|
* |
|
205
|
|
|
* @return bool |
|
206
|
|
|
*/ |
|
207
|
|
|
public function uploadFile(UploadedFile $file, $folder = null, $name = null, $override = false, array $config = []) |
|
208
|
|
|
{ |
|
209
|
|
|
$data = $this->parseFile($file, $folder, $name, $override); |
|
210
|
|
|
|
|
211
|
2 |
|
$success = (bool) $this->storage->put($data['filename'], file_get_contents($file), $config); |
|
|
|
|
|
|
212
|
|
|
|
|
213
|
2 |
|
if ($success) { |
|
214
|
1 |
|
return $data; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
1 |
|
return false; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Upload Image |
|
222
|
|
|
* |
|
223
|
|
|
* @param UploadedFile $file Uploaded File |
|
224
|
|
|
* @param string $folder String Folder |
|
225
|
|
|
* @param string $name String Name |
|
226
|
|
|
* @param array $options Array Options |
|
227
|
|
|
* @param bool $override Boolean Over Ride |
|
228
|
|
|
* @param array $config Array Config Upload |
|
229
|
|
|
* |
|
230
|
2 |
|
* @return bool |
|
231
|
|
|
*/ |
|
232
|
2 |
|
public function uploadImage( |
|
233
|
|
|
UploadedFile $file, |
|
234
|
2 |
|
$folder = null, |
|
235
|
|
|
$name = null, |
|
236
|
2 |
|
array $options = [], |
|
237
|
1 |
|
$override = false, |
|
238
|
|
|
array $config = [] |
|
239
|
|
|
) { |
|
240
|
1 |
|
$pathImage = $file->getPathname(); |
|
241
|
|
|
$data = $this->parseFile($file, $folder, $name, $override); |
|
242
|
|
|
|
|
243
|
|
|
if ($this->imagineFactory) { |
|
244
|
|
|
$width = isset($options['width']) ? intval($options['width']) : 0; |
|
245
|
|
|
$height = isset($options['height']) ? intval($options['height']) : 0; |
|
246
|
|
|
$scale = isset($options['scale']) ? (bool) $options['scale'] : true; |
|
247
|
|
|
$opacity = isset($options['opacity']) ? (float) $options['opacity'] : null; |
|
248
|
|
|
$watermark = isset($options['watermark']) ? $options['watermark'] : null; |
|
249
|
|
|
$quality = isset($options['quality']) ? intval($options['quality']) : 85; // Quality Deufault: 85; |
|
250
|
|
|
|
|
251
|
|
|
$imagine = $this->imagineFactory->make($pathImage); |
|
252
|
|
|
$imagine->resize($width, $height, !$scale); |
|
253
|
|
|
$imagine->opacity($opacity); |
|
254
|
4 |
|
$imagine->watermark($watermark); |
|
255
|
|
|
$imagine = $imagine->save($pathImage.'.'.$data['extension'], $quality); |
|
256
|
|
|
$data['size'] = $imagine->filesize(); |
|
257
|
|
|
$content = $imagine->encode(); |
|
258
|
|
|
} else { |
|
259
|
|
|
$content = file_get_contents($file); |
|
260
|
|
|
} |
|
261
|
4 |
|
|
|
262
|
4 |
|
$success = $this->storage->put($data['filename'], $content, $config); |
|
|
|
|
|
|
263
|
|
|
|
|
264
|
4 |
|
if ($success) { |
|
265
|
1 |
|
return $data; |
|
266
|
1 |
|
} |
|
267
|
1 |
|
|
|
268
|
1 |
|
return false; |
|
269
|
1 |
|
} |
|
270
|
1 |
|
|
|
271
|
|
|
/** |
|
272
|
1 |
|
* Parse Filename |
|
273
|
1 |
|
* |
|
274
|
1 |
|
* @param UploadedFile $file Uploaded File |
|
275
|
1 |
|
* @param string $name String Name |
|
276
|
|
|
* @param string $folder String Folder |
|
277
|
1 |
|
* @param bool $override Boolean Over Ride |
|
278
|
1 |
|
* |
|
279
|
1 |
|
* @return bool|array |
|
280
|
1 |
|
*/ |
|
281
|
3 |
|
protected function parseFile($file, $folder = null, $name = null, $override = false) |
|
282
|
|
|
{ |
|
283
|
|
|
$folder = trim((string) $folder, '/'); |
|
284
|
4 |
|
$folder = $folder ? "{$folder}/" : ""; |
|
285
|
|
|
$this->storage->makeDirectory($folder); |
|
286
|
4 |
|
|
|
287
|
3 |
|
$name = $name ?: $file->getClientOriginalName(); |
|
288
|
|
|
$nameOriginal = str_slug(pathinfo($name, PATHINFO_FILENAME)); |
|
289
|
|
|
|
|
290
|
1 |
|
if (empty($nameOriginal)) { |
|
291
|
|
|
$nameOriginal = str_random(10); |
|
292
|
|
|
} |
|
293
|
|
|
$extension = $file->getClientOriginalExtension(); |
|
294
|
|
|
$size = $file->getClientSize(); |
|
295
|
|
|
$mime = $file->getClientMimeType(); |
|
296
|
|
|
|
|
297
|
|
|
$sufix = ''; |
|
298
|
|
|
$count = 1; |
|
299
|
|
|
do { |
|
300
|
|
|
$name = "{$nameOriginal}{$sufix}.{$extension}"; |
|
301
|
|
|
$filename = "{$folder}{$name}"; |
|
302
|
|
|
$sufix = "({$count})"; |
|
303
|
6 |
|
$count++; |
|
304
|
|
|
|
|
305
|
6 |
|
} while (!$override && $this->storage->exists($filename)); |
|
306
|
6 |
|
|
|
307
|
6 |
|
return compact('filename', 'name', 'extension', 'size', 'mime'); |
|
308
|
|
|
} |
|
309
|
6 |
|
|
|
310
|
6 |
|
/** |
|
311
|
|
|
* Dynamically handle calls into the query instance. |
|
312
|
6 |
|
* |
|
313
|
1 |
|
* @param string $method |
|
314
|
1 |
|
* @param array $parameters |
|
315
|
6 |
|
* @return mixed |
|
316
|
6 |
|
*/ |
|
317
|
6 |
|
public function __call($method, $parameters) |
|
318
|
|
|
{ |
|
319
|
6 |
|
return $this->storage->{$method}(...$parameters); |
|
320
|
6 |
|
} |
|
321
|
|
|
} |
|
322
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.