3
3
namespace Unisharp \FileApi ;
4
4
5
5
use Illuminate \Support \Facades \File ;
6
+ use Illuminate \Support \Facades \Request ;
6
7
use Illuminate \Support \Facades \Storage ;
7
8
use League \Flysystem \FileNotFoundException ;
8
9
use Symfony \Component \HttpFoundation \File \UploadedFile ;
@@ -19,8 +20,9 @@ class FileApi
19
20
protected $ thumb_sizes = null ;
20
21
protected $ shouldCropThumb = false ;
21
22
protected $ compress_quality = 90 ;
23
+ protected $ visibility ;
22
24
23
- public function __construct ($ basepath = DIRECTORY_SEPARATOR )
25
+ public function __construct ($ basepath = DIRECTORY_SEPARATOR , $ visibility = ' public ' )
24
26
{
25
27
if (mb_substr ($ basepath , -1 , 1 , 'utf8 ' ) != DIRECTORY_SEPARATOR ) {
26
28
$ basepath .= DIRECTORY_SEPARATOR ;
@@ -30,7 +32,17 @@ public function __construct($basepath = DIRECTORY_SEPARATOR)
30
32
$ basepath = mb_substr ($ basepath , 1 , null , 'utf8 ' );
31
33
}
32
34
35
+ $ this ->visibility = $ visibility ;
36
+ }
37
+
38
+ public function setBasePath ($ basepath )
39
+ {
40
+ if (mb_substr ($ basepath , -1 , 1 , 'utf8 ' ) != '/ ' ) {
41
+ $ basepath .= '/ ' ;
42
+ }
43
+
33
44
$ this ->basepath = $ basepath ;
45
+ return $ this ;
34
46
}
35
47
36
48
public function get ($ filename , $ size = self ::SIZE_LARGE )
@@ -49,13 +61,13 @@ public function get($filename, $size = self::SIZE_LARGE)
49
61
$ file_path = $ this ->basepath . $ filename ;
50
62
51
63
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 ])) {
53
65
$ file_path = $ this ->basepath . $ file [0 ] . '_ ' . $ size . '. ' . $ file [1 ];
54
66
}
55
67
56
68
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 (),
59
71
$ this ->basepath . $ filename
60
72
);
61
73
} else {
@@ -100,11 +112,13 @@ public function getPath($filename)
100
112
101
113
public function getUrl ($ filename )
102
114
{
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 (),
106
118
$ this ->basepath . $ filename
107
119
);
120
+ } elseif (config ('filesystems.default ' ) == 'gcs ' ) {
121
+ return Storage::getDriver ()->getAdapter ()->getUrl ($ this ->basepath . $ filename );
108
122
} else {
109
123
return $ this ->basepath . $ filename ;
110
124
}
@@ -114,19 +128,19 @@ public function getResponse($filename, $headers = [])
114
128
{
115
129
try {
116
130
$ path = $ this ->basepath . $ filename ;
117
- $ file = \ Storage::get ($ path );
118
- $ filetime = \ Storage::lastModified ($ path );
131
+ $ file = Storage::get ($ path );
132
+ $ filetime = Storage::lastModified ($ path );
119
133
$ etag = md5 ($ filetime );
120
134
$ time = date ('r ' , $ filetime );
121
135
$ expires = date ('r ' , $ filetime + 3600 );
122
136
123
137
$ response = response (null , 304 , $ headers );
124
138
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 );
127
141
128
142
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 ));
130
144
}
131
145
132
146
return $ response
@@ -140,21 +154,22 @@ public function getResponse($filename, $headers = [])
140
154
abort (404 );
141
155
}
142
156
}
157
+
143
158
public function drop ($ filename )
144
159
{
145
160
// Cut original file name
146
161
$ dot = strpos ($ filename , '. ' );
147
162
$ origin_name = substr ($ filename , 0 , $ dot );
148
163
149
164
// Find all images in basepath
150
- $ allFiles = \ Storage::files ($ this ->basepath );
165
+ $ allFiles = Storage::files ($ this ->basepath );
151
166
$ files = array_filter ($ allFiles , function ($ file ) use ($ origin_name ) {
152
167
return preg_match ('/^(.*) ' .$ origin_name .'(.*)$/ ' , $ file );
153
168
});
154
169
155
170
// Delete original image and thumbnails
156
171
foreach ($ files as $ file ) {
157
- \ Storage::delete ($ file );
172
+ Storage::delete ($ file );
158
173
}
159
174
}
160
175
@@ -171,16 +186,17 @@ private function moveFile($upload_file, $cus_name, $can_make_watermark = false)
171
186
} else {
172
187
$ original_name = $ cus_name ;
173
188
}
174
- $ filename = $ original_name . '. ' .$ suffix ;
189
+ $ filename = $ original_name . (! empty ( $ suffix ) ? '. ' . $ suffix : '' ) ;
175
190
176
191
$ img = $ this ->setTmpImage ($ upload_file );
177
192
178
- \ Storage::put (
193
+ Storage::put (
179
194
$ this ->basepath . $ filename ,
180
- file_get_contents ($ upload_file ->getRealPath ())
195
+ file_get_contents ($ upload_file ->getRealPath ()),
196
+ $ this ->visibility
181
197
);
182
198
183
- \ File::delete ($ upload_file ->getRealPath ());
199
+ File::delete ($ upload_file ->getRealPath ());
184
200
185
201
if (!is_null ($ img ) && !empty ($ this ->getThumbSizes ())) {
186
202
$ this ->saveThumb ($ img , $ original_name , $ suffix );
@@ -199,8 +215,8 @@ private function setTmpImage($upload_file)
199
215
$ image_path = $ upload_file instanceof UploadedFile ? $ upload_file ->getRealPath () : $ upload_file ;
200
216
$ img = null ;
201
217
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 )) {
204
220
case 'image/png ' :
205
221
$ img = imagecreatefrompng ($ image_path );
206
222
break ;
@@ -253,16 +269,16 @@ private function saveThumb($img, $original_name, $suffix)
253
269
$ tmp_thumb = $ this ->resizeOrCropThumb ($ img , $ size );
254
270
255
271
// 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 ();
258
274
259
275
// save thumbnail image
260
276
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 );
263
279
264
280
// remove tmp image
265
- \ Storage::disk ('local ' )->delete ($ tmp_filename );
281
+ Storage::disk ('local ' )->delete ($ tmp_filename );
266
282
}
267
283
}
268
284
@@ -289,17 +305,17 @@ private function mergeWatermark($image, $original_name, $suffix)
289
305
imagesavealpha ($ image , true );
290
306
$ main_image = $ original_name . '. ' . $ suffix ;
291
307
$ 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 ();
293
309
294
310
295
311
$ watermark_filename = $ this ->basepath . $ original_name . '_W ' . '. ' . $ suffix ;
296
312
// save thumbnail image
297
313
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 );
300
316
301
317
// remove tmp image
302
- \ Storage::disk ('local ' )->delete ($ tmp_filename );
318
+ Storage::disk ('local ' )->delete ($ tmp_filename );
303
319
}
304
320
305
321
private function resizeOrCropThumb ($ img , $ size )
@@ -400,15 +416,15 @@ private function saveCompress($img, $original_name, $suffix)
400
416
$ main_image = $ original_name . '. ' . $ suffix ;
401
417
$ tmp_filename = 'tmp/ ' . $ main_image ;
402
418
403
- $ tmp_path = \ Storage::disk ('local ' )->getDriver ()->getAdapter ()->getPathPrefix ();
419
+ $ tmp_path = Storage::disk ('local ' )->getDriver ()->getAdapter ()->getPathPrefix ();
404
420
405
421
// save thumbnail image
406
422
imagejpeg ($ img , $ tmp_path . $ tmp_filename , config ('fileapi.compress_quality ' , $ this ->compress_quality ));
407
423
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 );
410
426
411
427
// remove tmp image
412
- \ Storage::disk ('local ' )->delete ($ tmp_filename );
428
+ Storage::disk ('local ' )->delete ($ tmp_filename );
413
429
}
414
430
}
0 commit comments