22
33namespace Unisharp \FileApi ;
44
5+ use Illuminate \Support \Facades \File ;
6+ use Illuminate \Support \Facades \Request ;
7+ use Illuminate \Support \Facades \Storage ;
58use League \Flysystem \FileNotFoundException ;
69use Symfony \Component \HttpFoundation \File \UploadedFile ;
710
@@ -11,8 +14,9 @@ class FileApi
1114 protected $ default_sizes = ['S ' => '96x96 ' , 'M ' => '256x256 ' , 'L ' => '480x480 ' ];
1215 protected $ thumb_sizes = null ;
1316 protected $ shouldCropThumb = false ;
17+ protected $ visibility ;
1418
15- public function __construct ($ basepath = '/ ' )
19+ public function __construct ($ basepath = '/ ' , $ visibility = ' public ' )
1620 {
1721 if (mb_substr ($ basepath , -1 , 1 , 'utf8 ' ) != '/ ' ) {
1822 $ basepath .= '/ ' ;
@@ -22,6 +26,7 @@ public function __construct($basepath = '/')
2226 $ basepath = mb_substr ($ basepath , 1 , null , 'utf8 ' );
2327 }
2428
29+ $ this ->visibility = $ visibility ;
2530 $ this ->basepath = $ basepath ;
2631 }
2732
@@ -30,9 +35,9 @@ public function get($filename, $size = null)
3035 // Cut original file name
3136 $ file = explode ('. ' , $ filename );
3237
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 ])) {
3439 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 ])) {
3641 return $ this ->basepath . $ file [0 ] . '_ ' . $ size . '. ' . $ file [1 ];
3742 } else {
3843 return $ this ->basepath . $ filename ;
@@ -76,11 +81,13 @@ public function getPath($filename)
7681
7782 public function getUrl ($ filename )
7883 {
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 (),
8287 $ this ->basepath . $ filename
8388 );
89+ } elseif (config ('filesystems.default ' ) == 'gcs ' ) {
90+ return Storage::getDriver ()->getAdapter ()->getUrl ($ this ->basepath . $ filename );
8491 } else {
8592 return $ this ->basepath . $ filename ;
8693 }
@@ -90,15 +97,15 @@ public function getResponse($filename, $headers = [])
9097 {
9198 try {
9299 $ path = $ this ->basepath . $ filename ;
93- $ file = \ Storage::get ($ path );
94- $ filetime = \ Storage::lastModified ($ path );
100+ $ file = Storage::get ($ path );
101+ $ filetime = Storage::lastModified ($ path );
95102 $ etag = md5 ($ filetime );
96103 $ time = date ('r ' , $ filetime );
97104 $ 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 )
100107 ) {
101- return response ($ file , 200 , $ headers )->header ('Content-Type ' , \ Storage::mimeType ($ path ))
108+ return response ($ file , 200 , $ headers )->header ('Content-Type ' , Storage::mimeType ($ path ))
102109 ->setEtag ($ etag )
103110 ->setLastModified (new \DateTime ($ time ))
104111 ->setExpires (new \DateTime ($ expires ))
@@ -124,15 +131,14 @@ public function drop($filename)
124131 $ origin_name = substr ($ filename , 0 , $ dot );
125132
126133 // 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 ) {
130136 return preg_match ('/^(.*) ' .$ origin_name .'(.*)$/ ' , $ file );
131137 });
132138
133139 // Delete original image and thumbnails
134140 foreach ($ files as $ file ) {
135- \ Storage::delete ($ file );
141+ Storage::delete ($ file );
136142 }
137143 }
138144
@@ -153,12 +159,13 @@ private function moveFile($upload_file, $cus_name)
153159
154160 $ img = $ this ->setTmpImage ($ upload_file );
155161
156- \ Storage::put (
162+ Storage::put (
157163 $ this ->basepath . $ filename ,
158- file_get_contents ($ upload_file ->getRealPath ())
164+ file_get_contents ($ upload_file ->getRealPath ()),
165+ $ this ->visibility
159166 );
160167
161- \ File::delete ($ upload_file ->getRealPath ());
168+ File::delete ($ upload_file ->getRealPath ());
162169
163170 if (!is_null ($ img ) && !empty ($ this ->getThumbSizes ())) {
164171 $ this ->saveThumb ($ img , $ original_name , $ suffix );
@@ -171,8 +178,8 @@ private function setTmpImage($upload_file)
171178 {
172179 $ image_types = array ('image/png ' , 'image/gif ' , 'image/jpeg ' , 'image/jpg ' );
173180
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 )) {
176183 case 'image/png ' :
177184 $ img = imagecreatefrompng ($ upload_file ->getRealPath ());
178185 break ;
@@ -223,16 +230,16 @@ private function saveThumb($img, $original_name, $suffix)
223230 $ tmp_thumb = $ this ->resizeOrCropThumb ($ img , $ size );
224231
225232 // 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 ));
227234 $ tmp_path = \Storage::disk ('local ' )->getDriver ()->getAdapter ()->getPathPrefix ();
228235
229236 // save thumbnail image
230237 imagepng ($ tmp_thumb , $ tmp_path . $ tmp_filename );
231238 $ tmp_file = \Storage::disk ('local ' )->get ($ tmp_filename );
232- \ Storage::put ($ thumb_name , $ tmp_file );
239+ Storage::put ($ thumb_name , $ tmp_file );
233240
234241 // remove tmp image
235- \ Storage::disk ('local ' )->delete ($ tmp_filename );
242+ Storage::disk ('local ' )->delete ($ tmp_filename );
236243 }
237244 }
238245
@@ -281,7 +288,7 @@ private function cropThumb($img, &$width, &$height, $thumb_width, $thumb_height)
281288 ];
282289
283290 $ width = $ new_width ;
284- } else if ($ image_ratio > $ thumb_ratio ) {
291+ } elseif ($ image_ratio > $ thumb_ratio ) {
285292 $ new_height = $ thumb_height *$ width /$ thumb_width ;
286293
287294 $ square = [
@@ -308,7 +315,7 @@ private function resizeThumb($width, $height, &$thumb_width, &$thumb_height)
308315 if ($ image_ratio !== $ thumb_ratio ) {
309316 if ($ image_ratio < $ thumb_ratio ) {
310317 $ thumb_height = $ thumb_width *$ height /$ width ;
311- } else if ($ image_ratio > $ thumb_ratio ) {
318+ } elseif ($ image_ratio > $ thumb_ratio ) {
312319 $ thumb_width = $ thumb_height *$ width /$ height ;
313320 }
314321 }
@@ -320,7 +327,7 @@ private function getThumbSizes()
320327
321328 if (!is_null ($ this ->thumb_sizes )) {
322329 return $ this ->thumb_sizes ;
323- } else if (!is_null ($ config )) {
330+ } elseif (!is_null ($ config )) {
324331 return $ config ;
325332 } else {
326333 return $ this ->default_sizes ;
0 commit comments