Skip to content

Commit 47f9d53

Browse files
committed
Update StorageTrait.php
1 parent 00b0f78 commit 47f9d53

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

src/app/Traits/StorageTrait.php

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace App\Traits;
44

5-
use Exception;
65
use Illuminate\Support\Facades\Storage;
76
use 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

Comments
 (0)