Skip to content

Commit bd9b241

Browse files
authored
Fails to retrieve bucket name when disk name is not s3 fixed 🐛 (#83)
1 parent adf59fd commit bd9b241

File tree

2 files changed

+51
-25
lines changed

2 files changed

+51
-25
lines changed

src/Drivers/LocalUploadDriver.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,26 @@
99
use Illuminate\Support\Facades\Storage;
1010
use RahulHaque\Filepond\Contracts\UploaderInterface;
1111
use RahulHaque\Filepond\Exceptions\InvalidChunkException;
12+
use RahulHaque\Filepond\Models\Filepond;
1213

1314
class LocalUploadDriver implements UploaderInterface
1415
{
16+
private $tempDisk;
17+
18+
private $tempFolder;
19+
20+
private $model;
21+
22+
public function __construct()
23+
{
24+
$this->tempDisk = config('filepond.temp_disk', 'local');
25+
$this->tempFolder = config('filepond.temp_folder', 'filepond/temp');
26+
$this->model = config('filepond.model', Filepond::class);
27+
}
28+
1529
public function initChunkUpload(Request $request): string
1630
{
17-
$filepond = config('filepond.model')::create([
31+
$filepond = $this->model::create([
1832
'filepath' => '',
1933
'filename' => '',
2034
'extension' => '',
@@ -24,16 +38,15 @@ public function initChunkUpload(Request $request): string
2438
'expires_at' => now()->addMinutes(config('filepond.expiration', 30)),
2539
]);
2640

27-
Storage::disk(config('filepond.temp_disk'))->makeDirectory(config('filepond.temp_folder').DIRECTORY_SEPARATOR.$filepond->id);
41+
Storage::disk($this->tempDisk)->makeDirectory($this->tempFolder.DIRECTORY_SEPARATOR.$filepond->id);
2842

2943
return Crypt::encrypt(['id' => $filepond->id]);
3044
}
3145

3246
public function handleChunk(Request $request): int
3347
{
3448
$id = Crypt::decrypt($request->patch)['id'];
35-
$disk = Storage::disk(config('filepond.temp_disk'));
36-
$dir = $disk->path(config('filepond.temp_folder').DIRECTORY_SEPARATOR.$id.DIRECTORY_SEPARATOR);
49+
$dir = Storage::disk($this->tempDisk)->path($this->tempFolder.DIRECTORY_SEPARATOR.$id.DIRECTORY_SEPARATOR);
3750

3851
$contentLength = (int) $request->header('Content-Length');
3952
$uploadLength = (int) $request->header('Upload-Length');
@@ -69,10 +82,10 @@ public function handleChunk(Request $request): int
6982
}
7083
fclose($file);
7184

72-
$filepond = config('filepond.model')::findOrFail($id);
85+
$filepond = $this->model::findOrFail($id);
7386

7487
$filepond->update([
75-
'filepath' => config('filepond.temp_folder').DIRECTORY_SEPARATOR.$id.DIRECTORY_SEPARATOR.$uploadName,
88+
'filepath' => $this->tempFolder.DIRECTORY_SEPARATOR.$id.DIRECTORY_SEPARATOR.$uploadName,
7689
'filename' => $uploadName,
7790
'extension' => pathinfo($uploadName, PATHINFO_EXTENSION),
7891
'expires_at' => now()->addMinutes(config('filepond.expiration', 30)),
@@ -85,8 +98,8 @@ public function handleChunk(Request $request): int
8598
public function calculateOffset(Request $request): int
8699
{
87100
$id = Crypt::decrypt($request->patch)['id'];
88-
$filepond = config('filepond.model')::findOrFail($id);
89-
$dir = Storage::disk(config('filepond.temp_disk'))->path(config('filepond.temp_folder').DIRECTORY_SEPARATOR.$filepond->id.DIRECTORY_SEPARATOR);
101+
$filepond = $this->model::findOrFail($id);
102+
$dir = Storage::disk($this->tempDisk)->path($this->tempFolder.DIRECTORY_SEPARATOR.$filepond->id.DIRECTORY_SEPARATOR);
90103

91104
$size = 0;
92105
$chunks = glob($dir.'*');
@@ -101,14 +114,14 @@ public function deleteFile(Request $request): bool
101114
{
102115
$id = Crypt::decrypt($request->getContent())['id'];
103116

104-
$filepond = config('filepond.model')::findOrFail($id);
117+
$filepond = $this->model::findOrFail($id);
105118

106119
if (config('filepond.soft_delete', true)) {
107120
return $filepond->delete();
108121
}
109122

110-
Storage::disk(config('filepond.temp_disk'))->delete($filepond->filepath);
111-
Storage::disk(config('filepond.temp_disk'))->deleteDirectory(config('filepond.temp_folder').DIRECTORY_SEPARATOR.$filepond->id);
123+
Storage::disk($this->tempDisk)->delete($filepond->filepath);
124+
Storage::disk($this->tempDisk)->deleteDirectory($this->tempFolder.DIRECTORY_SEPARATOR.$filepond->id);
112125

113126
return $filepond->forceDelete();
114127
}

src/Drivers/S3UploadDriver.php

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,32 @@
1212
use Illuminate\Support\Str;
1313
use RahulHaque\Filepond\Contracts\UploaderInterface;
1414
use RahulHaque\Filepond\Exceptions\InvalidChunkException;
15+
use RahulHaque\Filepond\Models\Filepond;
1516

1617
class S3UploadDriver implements UploaderInterface
1718
{
19+
private $tempDisk;
20+
21+
private $tempFolder;
22+
23+
private $bucket;
24+
25+
private $model;
26+
1827
private S3Client $client;
1928

2029
public function __construct()
2130
{
22-
$this->client = Storage::disk(config('filepond.temp_disk'))->getClient();
31+
$this->tempDisk = config('filepond.temp_disk', 'local');
32+
$this->tempFolder = config('filepond.temp_folder', 'filepond/temp');
33+
$this->bucket = config('filesystems.disks.'.$this->tempDisk.'.bucket');
34+
$this->model = config('filepond.model', Filepond::class);
35+
$this->client = Storage::disk($this->tempDisk)->getClient();
2336
}
2437

2538
public function initChunkUpload(Request $request): string
2639
{
27-
$filepond = config('filepond.model')::create([
40+
$filepond = $this->model::create([
2841
'filepath' => '',
2942
'filename' => Str::uuid().'.tmp',
3043
'extension' => '',
@@ -34,10 +47,10 @@ public function initChunkUpload(Request $request): string
3447
'expires_at' => now()->addMinutes(config('filepond.expiration', 30)),
3548
]);
3649

37-
$key = config('filepond.temp_folder').'/'.$filepond->id.'/'.$filepond->filename;
50+
$key = $this->tempFolder.'/'.$filepond->id.'/'.$filepond->filename;
3851

3952
$initChunkResponse = $this->client->createMultipartUpload([
40-
'Bucket' => config('filesystems.disks.s3.bucket'),
53+
'Bucket' => $this->bucket,
4154
'Key' => $key,
4255
]);
4356

@@ -52,8 +65,8 @@ public function initChunkUpload(Request $request): string
5265
public function handleChunk(Request $request): int
5366
{
5467
$id = Crypt::decrypt($request->patch)['id'];
55-
$filepond = config('filepond.model')::findOrFail($id);
56-
$key = config('filepond.temp_folder').'/'.$filepond->id.'/'.$filepond->filename;
68+
$filepond = $this->model::findOrFail($id);
69+
$key = $this->tempFolder.'/'.$filepond->id.'/'.$filepond->filename;
5770

5871
$contentLength = (int) $request->header('Content-Length');
5972
$uploadLength = (int) $request->header('Upload-Length');
@@ -64,7 +77,7 @@ public function handleChunk(Request $request): int
6477
// Check if the uploaded file and chunk are S3 compatible
6578
if ($partNumber === 1 && $error = $this->checkS3Compatibility($contentLength, $uploadLength)) {
6679
$this->client->abortMultipartUpload([
67-
'Bucket' => config('filesystems.disks.s3.bucket'),
80+
'Bucket' => $this->bucket,
6881
'Key' => $key,
6982
'UploadId' => $filepond->upload_id,
7083
]);
@@ -74,7 +87,7 @@ public function handleChunk(Request $request): int
7487

7588
try {
7689
$uploadPartResponse = $this->client->uploadPart([
77-
'Bucket' => config('filesystems.disks.s3.bucket'),
90+
'Bucket' => $this->bucket,
7891
'Key' => $key,
7992
'UploadId' => $filepond->upload_id,
8093
'PartNumber' => $partNumber,
@@ -110,14 +123,14 @@ public function handleChunk(Request $request): int
110123

111124
try {
112125
$this->client->completeMultipartUpload([
113-
'Bucket' => config('filesystems.disks.s3.bucket'),
126+
'Bucket' => $this->bucket,
114127
'Key' => $key,
115128
'UploadId' => $filepond->upload_id,
116129
'MultipartUpload' => ['Parts' => $tags],
117130
]);
118131
} catch (S3Exception $e) {
119132
$this->client->abortMultipartUpload([
120-
'Bucket' => config('filesystems.disks.s3.bucket'),
133+
'Bucket' => $this->bucket,
121134
'Key' => $key,
122135
'UploadId' => $filepond->upload_id,
123136
]);
@@ -141,7 +154,7 @@ public function handleChunk(Request $request): int
141154
public function calculateOffset(Request $request): int
142155
{
143156
$id = Crypt::decrypt($request->patch)['id'];
144-
$filepond = config('filepond.model')::findOrFail($id);
157+
$filepond = $this->model::findOrFail($id);
145158

146159
return array_sum(array_column($filepond->upload_tags ?? [], 'Size'));
147160
}
@@ -150,14 +163,14 @@ public function deleteFile(Request $request): bool
150163
{
151164
$id = Crypt::decrypt($request->getContent())['id'];
152165

153-
$filepond = config('filepond.model')::findOrFail($id);
166+
$filepond = $this->model::findOrFail($id);
154167

155168
if (config('filepond.soft_delete', true)) {
156169
return $filepond->delete();
157170
}
158171

159-
Storage::disk(config('filepond.temp_disk'))->delete($filepond->filepath);
160-
Storage::disk(config('filepond.temp_disk'))->deleteDirectory(config('filepond.temp_folder').'/'.$filepond->id);
172+
Storage::disk($this->tempDisk)->delete($filepond->filepath);
173+
Storage::disk($this->tempDisk)->deleteDirectory($this->tempFolder.'/'.$filepond->id);
161174

162175
return $filepond->forceDelete();
163176
}

0 commit comments

Comments
 (0)