File tree Expand file tree Collapse file tree 2 files changed +33
-3
lines changed Expand file tree Collapse file tree 2 files changed +33
-3
lines changed Original file line number Diff line number Diff line change @@ -861,15 +861,21 @@ public function fileWrite($resource, $data)
861
861
*/
862
862
public function fileClose ($ resource ): bool
863
863
{
864
+ if (!is_resource ($ resource )) {
865
+ return false ;
866
+ }
864
867
//phpcs:disable
865
- $ resourcePath = stream_get_meta_data ($ resource )[ ' uri ' ] ;
868
+ $ meta = stream_get_meta_data ($ resource );
866
869
//phpcs:enable
867
870
868
871
foreach ($ this ->streams as $ path => $ stream ) {
869
872
// phpcs:ignore
870
- if (stream_get_meta_data ($ stream )['uri ' ] === $ resourcePath ) {
873
+ if (stream_get_meta_data ($ stream )['uri ' ] === $ meta ['uri ' ]) {
874
+ if (isset ($ meta ['seekable ' ]) && $ meta ['seekable ' ]) {
875
+ // rewind the file pointer to make sure the full content of the file is saved
876
+ $ this ->fileSeek ($ resource , 0 );
877
+ }
871
878
$ this ->adapter ->writeStream ($ path , $ resource , new Config (self ::CONFIG ));
872
-
873
879
// Remove path from streams after
874
880
unset($ this ->streams [$ path ]);
875
881
Original file line number Diff line number Diff line change @@ -513,4 +513,28 @@ public function testRenameSameDestination(): void
513
513
514
514
self ::assertTrue ($ this ->driver ->rename ('test/path ' , 'test/path ' ));
515
515
}
516
+
517
+ public function testFileShouldBeRewindBeforeSave (): void
518
+ {
519
+ $ resource = $ this ->driver ->fileOpen ('test/path ' , 'w ' );
520
+ $ this ->driver ->fileWrite ($ resource , 'abc ' );
521
+ $ this ->adapterMock ->method ('fileExists ' )->willReturn (false );
522
+ $ this ->adapterMock ->expects ($ this ->once ())
523
+ ->method ('writeStream ' )
524
+ ->with (
525
+ 'test/path ' ,
526
+ $ this ->callback (
527
+ // assert that the file pointer is at the beginning of the file before saving it in aws
528
+ fn ($ stream ) => $ stream === $ resource && is_resource ($ stream ) && ftell ($ stream ) === 0
529
+ )
530
+ );
531
+ $ this ->driver ->fileClose ($ resource );
532
+ }
533
+
534
+ public function testFileCloseShouldReturnFalseIfTheArgumentIsNotAResource (): void
535
+ {
536
+ $ this ->assertEquals (false , $ this ->driver ->fileClose ('' ));
537
+ $ this ->assertEquals (false , $ this ->driver ->fileClose (null ));
538
+ $ this ->assertEquals (false , $ this ->driver ->fileClose (false ));
539
+ }
516
540
}
You can’t perform that action at this time.
0 commit comments