22
33namespace App \Traits ;
44
5- use Exception ;
65use Illuminate \Support \Facades \Storage ;
76use Symfony \Component \HttpFoundation \StreamedResponse ;
87
@@ -11,13 +10,12 @@ trait StorageTrait
1110 /**
1211 * Save a file to the storage.
1312 *
14- * @param object $file
15- * @param string $folder
16- * @param string $fileName
17- * @param string $disk
13+ * @param object $file
14+ * @param string $path
15+ * @param string $fileName
1816 * @return bool|string
1917 */
20- public function saveFileToDisk (object $ file , string $ folder , string $ fileName , string $ disk = 'public ' ): bool |string
18+ public function saveFileToDisk ($ file , $ folder , $ fileName , $ disk = 'public ' ): bool |string
2119 {
2220 $ ext = $ file ->getClientOriginalExtension ();
2321 $ fullFilePath = "{$ folder }/ {$ fileName }. {$ ext }" ;
@@ -30,12 +28,25 @@ public function saveFileToDisk(object $file, string $folder, string $fileName, s
3028 return Storage::disk ($ disk )->put ($ fullFilePath , $ fileContent ) ? $ fullFilePath : false ;
3129 }
3230
31+ /**
32+ * Get path of local path
33+ *
34+ * @param object $filePath
35+ * @param string $disk
36+ * @return bool|string
37+ */
38+ public function getStoragePath (string $ filePath , string $ disk = 'public ' ): bool |string
39+ {
40+ // get storage path
41+ return Storage::disk ($ disk )->path ($ filePath );
42+ }
43+
3344 /**
3445 * Move a file on disk
3546 *
36- * @param string $filePath
37- * @param string $newPath
38- * @param string $disk
47+ * @param object $file
48+ * @param string $path
49+ * @param string $fileName
3950 * @return bool|string
4051 */
4152 public function moveFileOnDisk (string $ filePath , string $ newPath , string $ disk = 'public ' ): bool |string
@@ -80,13 +91,24 @@ public function getFileFromDisk(string $filePath, string $disk = 'public'): stri
8091 }
8192
8293 /**
83- * Get a file from the storage.
94+ * Check file exists in the storage.
95+ *
96+ * @param string $filePath
97+ * @param string $disk
98+ * @return string|bool
99+ */
100+ public function fileExists (string $ filePath , string $ disk = 'public ' ): bool
101+ {
102+ return Storage::disk ($ disk )->exists ($ filePath );
103+ }
104+
105+ /**
106+ * Download a file from the storage.
84107 *
85- * @param string $filePath
86- * @param string $fileName
87- * @param string $disk
108+ * @param string $filePath
109+ * @param string $fileName
110+ * @param string $disk
88111 * @return StreamedResponse
89- * @throws Exception
90112 */
91113 public function downloadFileFromDisk (string $ filePath , string $ fileName , string $ disk = 'public ' ): StreamedResponse
92114 {
@@ -98,6 +120,6 @@ public function downloadFileFromDisk(string $filePath, string $fileName, string
98120 return Storage::disk ($ disk )->download ($ filePath , $ fileName , $ headers );
99121 }
100122
101- throw new Exception ( ' File not found ' );
123+ // TODO: add exception
102124 }
103125}
0 commit comments