5
5
use Image ;
6
6
use Storage ;
7
7
use Illuminate \Support \Str ;
8
- use Illuminate \Http \UploadedFile ;
9
8
10
9
class Uploader
11
10
{
12
11
/**
13
12
* Put the file into the storage.
14
13
*
15
14
* @param string $directory
16
- * @param Illuminate\Http\UploadedFile $file
15
+ * @param mixed $file
17
16
*
18
17
* @return array
19
18
*/
20
- public static function upload (string $ directory , UploadedFile $ file )
19
+ public static function upload (string $ directory , $ file )
21
20
{
22
21
$ disk = config ('filesystems.default ' );
23
22
$ filename = str_random (64 ).'. ' .$ file ->getClientOriginalExtension ();
24
23
$ original_filename = $ file ->getClientOriginalName ();
24
+ $ filesize = $ file ->getSize ();
25
+ $ thumbnail_filesize = null ;
25
26
26
27
// Upload the file
27
28
$ path = Storage::putFileAs ($ directory , $ file , $ filename );
@@ -42,15 +43,38 @@ public static function upload(string $directory, UploadedFile $file)
42
43
$ fullThumbnailPath =
43
44
"{$ fileSystemRoot }/ {$ thumbnailDirectory }/ {$ filename }" ;
44
45
45
- Image::make ($ fullPath )
46
+ $ image = Image::make ($ fullPath )
46
47
->fit (240 )
47
48
->save ($ fullThumbnailPath , 95 );
48
49
50
+ $ thumbnail_filesize = Storage::size ($ thumbnailPath );
49
51
$ thumbnail_url = Storage::url ($ thumbnailPath );
50
52
}
51
53
52
54
return compact ([
53
- 'directory ' , 'filename ' , 'original_filename ' , 'url ' , 'thumbnail_url '
55
+ 'directory ' ,
56
+ 'filename ' ,
57
+ 'original_filename ' ,
58
+ 'filesize ' ,
59
+ 'thumbnail_filesize ' ,
60
+ 'url ' ,
61
+ 'thumbnail_url '
54
62
]);
55
63
}
64
+
65
+ /**
66
+ * Destroy files from disk.
67
+ *
68
+ * @param array $upload
69
+ *
70
+ * @return bool
71
+ */
72
+ public static function destroy (array $ upload )
73
+ {
74
+ $ path = "{$ upload ['directory ' ]}/ {$ upload ['filename ' ]}" ;
75
+ $ thumbnailPath =
76
+ "{$ upload ['directory ' ]}/thumbnails/ {$ upload ['filename ' ]}" ;
77
+
78
+ return Storage::delete ([$ path , $ thumbnailPath ]);
79
+ }
56
80
}
0 commit comments