99
1010class Uploader
1111{
12- /**
13- * @var string The storage driver.
14- */
15- protected $ disk ;
16-
17- public function __construct ()
18- {
19- $ this ->disk = config ('filesystems.default ' );
20- }
21-
22- /**
23- * Specify which storage driver will be used.
24- *
25- * @param string $name
26- *
27- * @return App\Utils\Uploader
28- */
29- public function disk (string $ name = 'public ' )
30- {
31- $ this ->disk = $ name ;
32-
33- return $ this ;
34- }
35-
3612 /**
3713 * Put the file into the storage.
3814 *
@@ -41,38 +17,40 @@ public function disk(string $name = 'public')
4117 *
4218 * @return array
4319 */
44- public function put (string $ directory , UploadedFile $ file )
20+ public static function upload (string $ directory , UploadedFile $ file )
4521 {
22+ $ disk = config ('filesystems.default ' );
4623 $ filename = str_random (64 ).'. ' .$ file ->getClientOriginalExtension ();
4724 $ original_filename = $ file ->getClientOriginalName ();
4825
49- $ path = Storage::disk ($ this ->disk )->putFileAs (
50- $ directory ,
51- $ file ,
52- $ filename
53- );
26+ // Upload the file
27+ $ path = Storage::putFileAs ($ directory , $ file , $ filename );
28+ $ url = Storage::url ($ path );
29+ $ thumbnail_url = null ;
5430
31+ // Do image manipulations
5532 if (Str::startsWith ($ file ->getClientMimeType (), 'image ' )) {
5633 $ thumbnailDirectory = "{$ directory }/thumbnails " ;
34+ $ thumbnailPath = "{$ thumbnailDirectory }/ {$ filename }" ;
5735
5836 if (! Storage::exists ($ thumbnailDirectory )) {
5937 Storage::makeDirectory ($ thumbnailDirectory );
6038 }
6139
62- $ fileSystemRoot = config ("filesystems.disks. {$ this -> disk }.root " );
40+ $ fileSystemRoot = config ("filesystems.disks. {$ disk }.root " );
6341 $ fullPath = "{$ fileSystemRoot }/ {$ path }" ;
64- $ thumbnailPath =
42+ $ fullThumbnailPath =
6543 "{$ fileSystemRoot }/ {$ thumbnailDirectory }/ {$ filename }" ;
6644
6745 Image::make ($ fullPath )
6846 ->fit (240 )
69- ->save ($ thumbnailPath , 95 );
70- }
47+ ->save ($ fullThumbnailPath , 95 );
7148
72- $ path = Storage::url ($ path );
49+ $ thumbnail_url = Storage::url ($ thumbnailPath );
50+ }
7351
7452 return compact ([
75- 'directory ' , 'filename ' , 'original_filename ' , 'path '
53+ 'directory ' , 'filename ' , 'original_filename ' , 'url ' , ' thumbnail_url '
7654 ]);
7755 }
7856}
0 commit comments