33namespace Unisharp \FileApi ;
44
55use Illuminate \Support \Facades \File ;
6+ use Illuminate \Support \Facades \Request ;
67use Illuminate \Support \Facades \Storage ;
78use League \Flysystem \FileNotFoundException ;
89use Symfony \Component \HttpFoundation \File \UploadedFile ;
@@ -19,8 +20,9 @@ class FileApi
1920 protected $ thumb_sizes = null ;
2021 protected $ shouldCropThumb = false ;
2122 protected $ compress_quality = 90 ;
23+ protected $ visibility ;
2224
23- public function __construct ($ basepath = DIRECTORY_SEPARATOR )
25+ public function __construct ($ basepath = DIRECTORY_SEPARATOR , $ visibility = ' public ' )
2426 {
2527 if (mb_substr ($ basepath , -1 , 1 , 'utf8 ' ) != DIRECTORY_SEPARATOR ) {
2628 $ basepath .= DIRECTORY_SEPARATOR ;
@@ -30,7 +32,17 @@ public function __construct($basepath = DIRECTORY_SEPARATOR)
3032 $ basepath = mb_substr ($ basepath , 1 , null , 'utf8 ' );
3133 }
3234
35+ $ this ->visibility = $ visibility ;
36+ }
37+
38+ public function setBasePath ($ basepath )
39+ {
40+ if (mb_substr ($ basepath , -1 , 1 , 'utf8 ' ) != '/ ' ) {
41+ $ basepath .= '/ ' ;
42+ }
43+
3344 $ this ->basepath = $ basepath ;
45+ return $ this ;
3446 }
3547
3648 public function get ($ filename , $ size = self ::SIZE_LARGE )
@@ -49,13 +61,13 @@ public function get($filename, $size = self::SIZE_LARGE)
4961 $ file_path = $ this ->basepath . $ filename ;
5062
5163 if ($ size != self ::SIZE_ORIGINAL
52- && \ Storage::exists ($ this ->basepath . $ file [0 ] . '_ ' . $ size . '. ' . $ file [1 ])) {
64+ && Storage::exists ($ this ->basepath . $ file [0 ] . '_ ' . $ size . '. ' . $ file [1 ])) {
5365 $ file_path = $ this ->basepath . $ file [0 ] . '_ ' . $ size . '. ' . $ file [1 ];
5466 }
5567
5668 if (\Config::get ('filesystems.default ' ) == 's3 ' ) {
57- return \ Storage::getDriver ()->getAdapter ()->getClient ()->getObjectUrl (
58- \ Storage::getDriver ()->getAdapter ()->getBucket (),
69+ return Storage::getDriver ()->getAdapter ()->getClient ()->getObjectUrl (
70+ Storage::getDriver ()->getAdapter ()->getBucket (),
5971 $ this ->basepath . $ filename
6072 );
6173 } else {
@@ -100,11 +112,13 @@ public function getPath($filename)
100112
101113 public function getUrl ($ filename )
102114 {
103- if (\Config:: get ( ' filesystems.default ' ) == 's3 ' ) {
104- return \ Storage::getDriver ()->getAdapter ()->getClient ()->getObjectUrl (
105- \ Storage::getDriver ()->getAdapter ()->getBucket (),
115+ if (Storage:: getDriver ()-> getAdapter ( ) == 's3 ' ) {
116+ return Storage::getDriver ()->getAdapter ()->getClient ()->getObjectUrl (
117+ Storage::getDriver ()->getAdapter ()->getBucket (),
106118 $ this ->basepath . $ filename
107119 );
120+ } elseif (config ('filesystems.default ' ) == 'gcs ' ) {
121+ return Storage::getDriver ()->getAdapter ()->getUrl ($ this ->basepath . $ filename );
108122 } else {
109123 return $ this ->basepath . $ filename ;
110124 }
@@ -114,19 +128,19 @@ public function getResponse($filename, $headers = [])
114128 {
115129 try {
116130 $ path = $ this ->basepath . $ filename ;
117- $ file = \ Storage::get ($ path );
118- $ filetime = \ Storage::lastModified ($ path );
131+ $ file = Storage::get ($ path );
132+ $ filetime = Storage::lastModified ($ path );
119133 $ etag = md5 ($ filetime );
120134 $ time = date ('r ' , $ filetime );
121135 $ expires = date ('r ' , $ filetime + 3600 );
122136
123137 $ response = response (null , 304 , $ headers );
124138
125- $ file_content_changed = trim (\ Request::header ('If-None-Match ' ), '\'\" ' ) != $ etag ;
126- $ file_modified = new \DateTime (\ Request::header ('If-Modified-Since ' )) != new \DateTime ($ time );
139+ $ file_content_changed = trim (Request::header ('If-None-Match ' ), '\'\" ' ) != $ etag ;
140+ $ file_modified = new \DateTime (Request::header ('If-Modified-Since ' )) != new \DateTime ($ time );
127141
128142 if ($ file_content_changed || $ file_modified ) {
129- $ response = response ($ file , 200 , $ headers )->header ('Content-Type ' , \ Storage::mimeType ($ path ));
143+ $ response = response ($ file , 200 , $ headers )->header ('Content-Type ' , Storage::mimeType ($ path ));
130144 }
131145
132146 return $ response
@@ -140,21 +154,22 @@ public function getResponse($filename, $headers = [])
140154 abort (404 );
141155 }
142156 }
157+
143158 public function drop ($ filename )
144159 {
145160 // Cut original file name
146161 $ dot = strpos ($ filename , '. ' );
147162 $ origin_name = substr ($ filename , 0 , $ dot );
148163
149164 // Find all images in basepath
150- $ allFiles = \ Storage::files ($ this ->basepath );
165+ $ allFiles = Storage::files ($ this ->basepath );
151166 $ files = array_filter ($ allFiles , function ($ file ) use ($ origin_name ) {
152167 return preg_match ('/^(.*) ' .$ origin_name .'(.*)$/ ' , $ file );
153168 });
154169
155170 // Delete original image and thumbnails
156171 foreach ($ files as $ file ) {
157- \ Storage::delete ($ file );
172+ Storage::delete ($ file );
158173 }
159174 }
160175
@@ -171,16 +186,17 @@ private function moveFile($upload_file, $cus_name, $can_make_watermark = false)
171186 } else {
172187 $ original_name = $ cus_name ;
173188 }
174- $ filename = $ original_name . '. ' .$ suffix ;
189+ $ filename = $ original_name . (! empty ( $ suffix ) ? '. ' . $ suffix : '' ) ;
175190
176191 $ img = $ this ->setTmpImage ($ upload_file );
177192
178- \ Storage::put (
193+ Storage::put (
179194 $ this ->basepath . $ filename ,
180- file_get_contents ($ upload_file ->getRealPath ())
195+ file_get_contents ($ upload_file ->getRealPath ()),
196+ $ this ->visibility
181197 );
182198
183- \ File::delete ($ upload_file ->getRealPath ());
199+ File::delete ($ upload_file ->getRealPath ());
184200
185201 if (!is_null ($ img ) && !empty ($ this ->getThumbSizes ())) {
186202 $ this ->saveThumb ($ img , $ original_name , $ suffix );
@@ -199,8 +215,8 @@ private function setTmpImage($upload_file)
199215 $ image_path = $ upload_file instanceof UploadedFile ? $ upload_file ->getRealPath () : $ upload_file ;
200216 $ img = null ;
201217
202- if (in_array (\ File::mimeType ($ upload_file ), $ image_types )) {
203- switch (\ File::mimeType ($ upload_file )) {
218+ if (in_array (File::mimeType ($ upload_file ), $ image_types )) {
219+ switch (File::mimeType ($ upload_file )) {
204220 case 'image/png ' :
205221 $ img = imagecreatefrompng ($ image_path );
206222 break ;
@@ -253,16 +269,16 @@ private function saveThumb($img, $original_name, $suffix)
253269 $ tmp_thumb = $ this ->resizeOrCropThumb ($ img , $ size );
254270
255271 // save tmp image
256- \ Storage::disk ('local ' )->put ($ tmp_filename , \ Storage::get ($ this ->basepath . $ main_image ));
257- $ tmp_path = \ Storage::disk ('local ' )->getDriver ()->getAdapter ()->getPathPrefix ();
272+ Storage::disk ('local ' )->put ($ tmp_filename , Storage::get ($ this ->basepath . $ main_image ));
273+ $ tmp_path = Storage::disk ('local ' )->getDriver ()->getAdapter ()->getPathPrefix ();
258274
259275 // save thumbnail image
260276 imagepng ($ tmp_thumb , $ tmp_path . $ tmp_filename );
261- $ tmp_file = \ Storage::disk ('local ' )->get ($ tmp_filename );
262- \ Storage::put ($ thumb_name , $ tmp_file );
277+ $ tmp_file = Storage::disk ('local ' )->get ($ tmp_filename );
278+ Storage::put ($ thumb_name , $ tmp_file, $ this -> visibility );
263279
264280 // remove tmp image
265- \ Storage::disk ('local ' )->delete ($ tmp_filename );
281+ Storage::disk ('local ' )->delete ($ tmp_filename );
266282 }
267283 }
268284
@@ -289,17 +305,17 @@ private function mergeWatermark($image, $original_name, $suffix)
289305 imagesavealpha ($ image , true );
290306 $ main_image = $ original_name . '. ' . $ suffix ;
291307 $ tmp_filename = 'tmp ' . DIRECTORY_SEPARATOR . $ main_image ;
292- $ tmp_path = \ Storage::disk ('local ' )->getDriver ()->getAdapter ()->getPathPrefix ();
308+ $ tmp_path = Storage::disk ('local ' )->getDriver ()->getAdapter ()->getPathPrefix ();
293309
294310
295311 $ watermark_filename = $ this ->basepath . $ original_name . '_W ' . '. ' . $ suffix ;
296312 // save thumbnail image
297313 imagepng ($ image , $ tmp_path . $ tmp_filename );
298- $ tmp_file = \ Storage::disk ('local ' )->get ($ tmp_filename );
299- \ Storage::put ($ watermark_filename , $ tmp_file );
314+ $ tmp_file = Storage::disk ('local ' )->get ($ tmp_filename );
315+ Storage::put ($ watermark_filename , $ tmp_file );
300316
301317 // remove tmp image
302- \ Storage::disk ('local ' )->delete ($ tmp_filename );
318+ Storage::disk ('local ' )->delete ($ tmp_filename );
303319 }
304320
305321 private function resizeOrCropThumb ($ img , $ size )
@@ -400,15 +416,15 @@ private function saveCompress($img, $original_name, $suffix)
400416 $ main_image = $ original_name . '. ' . $ suffix ;
401417 $ tmp_filename = 'tmp/ ' . $ main_image ;
402418
403- $ tmp_path = \ Storage::disk ('local ' )->getDriver ()->getAdapter ()->getPathPrefix ();
419+ $ tmp_path = Storage::disk ('local ' )->getDriver ()->getAdapter ()->getPathPrefix ();
404420
405421 // save thumbnail image
406422 imagejpeg ($ img , $ tmp_path . $ tmp_filename , config ('fileapi.compress_quality ' , $ this ->compress_quality ));
407423
408- $ tmp_file = \ Storage::disk ('local ' )->get ($ tmp_filename );
409- \ Storage::put ($ compress_name , $ tmp_file );
424+ $ tmp_file = Storage::disk ('local ' )->get ($ tmp_filename );
425+ Storage::put ($ compress_name , $ tmp_file );
410426
411427 // remove tmp image
412- \ Storage::disk ('local ' )->delete ($ tmp_filename );
428+ Storage::disk ('local ' )->delete ($ tmp_filename );
413429 }
414430}
0 commit comments