33namespace App \Http \Traits ;
44
55use Illuminate \Support \Facades \App ;
6+ use Illuminate \Support \Facades \Storage ;
67use Illuminate \Support \Facades \URL ;
78
89trait URLScheme {
@@ -28,4 +29,44 @@ public function checkURLScheme() {
2829 URL ::forceScheme ('https ' );
2930 }
3031 }
32+
33+
34+ /**
35+ * Old way to store images
36+ * $filename = 'categories/' . uniqid() . '.' . $file->extension(); // categories/6546dgfgfdgfg.jpg
37+ * $file->storePubliclyAs('public', $filename); // Store inside filesystem categories/6546dgfgfdgfg.jpg
38+ */
39+
40+ /**
41+ * @param $file
42+ * @return string
43+ */
44+ public function categoryFileStore ($ file ): string
45+ {
46+ $ fileDirectory = 'categories/ ' ;
47+ $ fileName = uniqid ().'. ' .$ file ->extension ();
48+ if (App::environment ('local ' )) {
49+ Storage::disk ('public ' )->put ($ fileDirectory .$ fileName , file_get_contents ($ file ));
50+ return Storage::disk ('public ' )->url ($ fileDirectory .$ fileName );
51+ } else {
52+ Storage::disk ('s3 ' )->put ($ fileDirectory .$ fileName , file_get_contents ($ file ));
53+ return Storage::disk ('s3 ' )->url ($ fileDirectory .$ fileName );
54+ }
55+ }
56+
57+
58+ /**
59+ * @param string $oldFilePath
60+ */
61+ public function categoryFileDelete (string $ oldFilePath )
62+ {
63+ $ fileDirectory = 'categories/ ' ;
64+ // Returns trailing name component of path e.g. ( 6546dgfgfdgfg.jpg)
65+ $ oldPath = basename ($ oldFilePath ); //
66+ if (App::environment ('local ' )) {
67+ Storage::disk ('public ' )->delete ($ fileDirectory .$ oldPath );
68+ } else {
69+ Storage::disk ('s3 ' )->delete ($ fileDirectory .$ oldPath );
70+ }
71+ }
3172}
0 commit comments