1212use Illuminate \Support \Str ;
1313use RahulHaque \Filepond \Contracts \UploaderInterface ;
1414use RahulHaque \Filepond \Exceptions \InvalidChunkException ;
15+ use RahulHaque \Filepond \Models \Filepond ;
1516
1617class 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