2
2
3
3
namespace Unisharp \FileApi ;
4
4
5
+ use Illuminate \Support \Facades \File ;
6
+ use Illuminate \Support \Facades \Request ;
7
+ use Illuminate \Support \Facades \Storage ;
5
8
use League \Flysystem \FileNotFoundException ;
6
9
use Symfony \Component \HttpFoundation \File \UploadedFile ;
7
10
@@ -11,8 +14,9 @@ class FileApi
11
14
protected $ default_sizes = ['S ' => '96x96 ' , 'M ' => '256x256 ' , 'L ' => '480x480 ' ];
12
15
protected $ thumb_sizes = null ;
13
16
protected $ shouldCropThumb = false ;
17
+ protected $ visibility ;
14
18
15
- public function __construct ($ basepath = '/ ' )
19
+ public function __construct ($ basepath = '/ ' , $ visibility = ' public ' )
16
20
{
17
21
if (mb_substr ($ basepath , -1 , 1 , 'utf8 ' ) != '/ ' ) {
18
22
$ basepath .= '/ ' ;
@@ -22,6 +26,7 @@ public function __construct($basepath = '/')
22
26
$ basepath = mb_substr ($ basepath , 1 , null , 'utf8 ' );
23
27
}
24
28
29
+ $ this ->visibility = $ visibility ;
25
30
$ this ->basepath = $ basepath ;
26
31
}
27
32
@@ -30,9 +35,9 @@ public function get($filename, $size = null)
30
35
// Cut original file name
31
36
$ file = explode ('. ' , $ filename );
32
37
33
- if (empty ($ size ) && \ Storage::exists ($ this ->basepath . $ file [0 ] . '_L. ' . $ file [1 ])) {
38
+ if (empty ($ size ) && Storage::exists ($ this ->basepath . $ file [0 ] . '_L. ' . $ file [1 ])) {
34
39
return $ this ->basepath . $ file [0 ] . '_L. ' . $ file [1 ];
35
- } else if (\ Storage::exists ($ this ->basepath . $ file [0 ] . '_ ' . $ size . '. ' . $ file [1 ])) {
40
+ } elseif ( Storage::exists ($ this ->basepath . $ file [0 ] . '_ ' . $ size . '. ' . $ file [1 ])) {
36
41
return $ this ->basepath . $ file [0 ] . '_ ' . $ size . '. ' . $ file [1 ];
37
42
} else {
38
43
return $ this ->basepath . $ filename ;
@@ -76,11 +81,13 @@ public function getPath($filename)
76
81
77
82
public function getUrl ($ filename )
78
83
{
79
- if (\Config:: get ( ' filesystems.default ' ) == 's3 ' ) {
80
- return \ Storage::getDriver ()->getAdapter ()->getClient ()->getObjectUrl (
81
- \ Storage::getDriver ()->getAdapter ()->getBucket (),
84
+ if (Storage:: getDriver ()-> getAdapter ( ) == 's3 ' ) {
85
+ return Storage::getDriver ()->getAdapter ()->getClient ()->getObjectUrl (
86
+ Storage::getDriver ()->getAdapter ()->getBucket (),
82
87
$ this ->basepath . $ filename
83
88
);
89
+ } elseif (config ('filesystems.default ' ) == 'gcs ' ) {
90
+ return Storage::getDriver ()->getAdapter ()->getUrl ($ this ->basepath . $ filename );
84
91
} else {
85
92
return $ this ->basepath . $ filename ;
86
93
}
@@ -90,15 +97,15 @@ public function getResponse($filename, $headers = [])
90
97
{
91
98
try {
92
99
$ path = $ this ->basepath . $ filename ;
93
- $ file = \ Storage::get ($ path );
94
- $ filetime = \ Storage::lastModified ($ path );
100
+ $ file = Storage::get ($ path );
101
+ $ filetime = Storage::lastModified ($ path );
95
102
$ etag = md5 ($ filetime );
96
103
$ time = date ('r ' , $ filetime );
97
104
$ expires = date ('r ' , $ filetime + 3600 );
98
- if (trim (\ Request::header ('If-None-Match ' ), '\'\" ' ) != $ etag ||
99
- new \DateTime (\ Request::header ('If-Modified-Since ' )) != new \DateTime ($ time )
105
+ if (trim (Request::header ('If-None-Match ' ), '\'\" ' ) != $ etag ||
106
+ new \DateTime (Request::header ('If-Modified-Since ' )) != new \DateTime ($ time )
100
107
) {
101
- return response ($ file , 200 , $ headers )->header ('Content-Type ' , \ Storage::mimeType ($ path ))
108
+ return response ($ file , 200 , $ headers )->header ('Content-Type ' , Storage::mimeType ($ path ))
102
109
->setEtag ($ etag )
103
110
->setLastModified (new \DateTime ($ time ))
104
111
->setExpires (new \DateTime ($ expires ))
@@ -124,15 +131,14 @@ public function drop($filename)
124
131
$ origin_name = substr ($ filename , 0 , $ dot );
125
132
126
133
// Find all images in basepath
127
- $ allFiles = \Storage::files ($ this ->basepath );
128
- $ files = array_filter ($ allFiles , function ($ file ) use ($ origin_name )
129
- {
134
+ $ allFiles = Storage::files ($ this ->basepath );
135
+ $ files = array_filter ($ allFiles , function ($ file ) use ($ origin_name ) {
130
136
return preg_match ('/^(.*) ' .$ origin_name .'(.*)$/ ' , $ file );
131
137
});
132
138
133
139
// Delete original image and thumbnails
134
140
foreach ($ files as $ file ) {
135
- \ Storage::delete ($ file );
141
+ Storage::delete ($ file );
136
142
}
137
143
}
138
144
@@ -153,12 +159,13 @@ private function moveFile($upload_file, $cus_name)
153
159
154
160
$ img = $ this ->setTmpImage ($ upload_file );
155
161
156
- \ Storage::put (
162
+ Storage::put (
157
163
$ this ->basepath . $ filename ,
158
- file_get_contents ($ upload_file ->getRealPath ())
164
+ file_get_contents ($ upload_file ->getRealPath ()),
165
+ $ this ->visibility
159
166
);
160
167
161
- \ File::delete ($ upload_file ->getRealPath ());
168
+ File::delete ($ upload_file ->getRealPath ());
162
169
163
170
if (!is_null ($ img ) && !empty ($ this ->getThumbSizes ())) {
164
171
$ this ->saveThumb ($ img , $ original_name , $ suffix );
@@ -171,8 +178,8 @@ private function setTmpImage($upload_file)
171
178
{
172
179
$ image_types = array ('image/png ' , 'image/gif ' , 'image/jpeg ' , 'image/jpg ' );
173
180
174
- if (in_array (\ File::mimeType ($ upload_file ), $ image_types )) {
175
- switch (\ File::mimeType ($ upload_file )) {
181
+ if (in_array (File::mimeType ($ upload_file ), $ image_types )) {
182
+ switch (File::mimeType ($ upload_file )) {
176
183
case 'image/png ' :
177
184
$ img = imagecreatefrompng ($ upload_file ->getRealPath ());
178
185
break ;
@@ -223,16 +230,16 @@ private function saveThumb($img, $original_name, $suffix)
223
230
$ tmp_thumb = $ this ->resizeOrCropThumb ($ img , $ size );
224
231
225
232
// save tmp image
226
- \ Storage::disk ('local ' )->put ($ tmp_filename , \Storage::get ($ this ->basepath . $ main_image ));
233
+ Storage::disk ('local ' )->put ($ tmp_filename , \Storage::get ($ this ->basepath . $ main_image ));
227
234
$ tmp_path = \Storage::disk ('local ' )->getDriver ()->getAdapter ()->getPathPrefix ();
228
235
229
236
// save thumbnail image
230
237
imagepng ($ tmp_thumb , $ tmp_path . $ tmp_filename );
231
238
$ tmp_file = \Storage::disk ('local ' )->get ($ tmp_filename );
232
- \ Storage::put ($ thumb_name , $ tmp_file );
239
+ Storage::put ($ thumb_name , $ tmp_file );
233
240
234
241
// remove tmp image
235
- \ Storage::disk ('local ' )->delete ($ tmp_filename );
242
+ Storage::disk ('local ' )->delete ($ tmp_filename );
236
243
}
237
244
}
238
245
@@ -281,7 +288,7 @@ private function cropThumb($img, &$width, &$height, $thumb_width, $thumb_height)
281
288
];
282
289
283
290
$ width = $ new_width ;
284
- } else if ($ image_ratio > $ thumb_ratio ) {
291
+ } elseif ($ image_ratio > $ thumb_ratio ) {
285
292
$ new_height = $ thumb_height *$ width /$ thumb_width ;
286
293
287
294
$ square = [
@@ -308,7 +315,7 @@ private function resizeThumb($width, $height, &$thumb_width, &$thumb_height)
308
315
if ($ image_ratio !== $ thumb_ratio ) {
309
316
if ($ image_ratio < $ thumb_ratio ) {
310
317
$ thumb_height = $ thumb_width *$ height /$ width ;
311
- } else if ($ image_ratio > $ thumb_ratio ) {
318
+ } elseif ($ image_ratio > $ thumb_ratio ) {
312
319
$ thumb_width = $ thumb_height *$ width /$ height ;
313
320
}
314
321
}
@@ -320,7 +327,7 @@ private function getThumbSizes()
320
327
321
328
if (!is_null ($ this ->thumb_sizes )) {
322
329
return $ this ->thumb_sizes ;
323
- } else if (!is_null ($ config )) {
330
+ } elseif (!is_null ($ config )) {
324
331
return $ config ;
325
332
} else {
326
333
return $ this ->default_sizes ;
0 commit comments