@@ -609,6 +609,7 @@ private void fileCreated(WritingFileState newState) {
609
609
}
610
610
else {
611
611
MultipartUtils .closeChannel (newState .channel );
612
+ MultipartUtils .deleteFile (newState .file );
612
613
this .content .forEach (DataBufferUtils ::release );
613
614
}
614
615
}
@@ -640,6 +641,8 @@ private final class IdleFileState implements State {
640
641
641
642
private volatile boolean closeOnDispose = true ;
642
643
644
+ private volatile boolean deleteOnDispose = true ;
645
+
643
646
644
647
public IdleFileState (WritingFileState state ) {
645
648
this .headers = state .headers ;
@@ -654,16 +657,20 @@ public void body(DataBuffer dataBuffer) {
654
657
if (PartGenerator .this .maxDiskUsagePerPart == -1 || count <= PartGenerator .this .maxDiskUsagePerPart ) {
655
658
656
659
this .closeOnDispose = false ;
660
+ this .deleteOnDispose = false ;
657
661
WritingFileState newState = new WritingFileState (this );
658
662
if (changeState (this , newState )) {
659
663
newState .writeBuffer (dataBuffer );
660
664
}
661
665
else {
662
666
MultipartUtils .closeChannel (this .channel );
667
+ MultipartUtils .deleteFile (this .file );
663
668
DataBufferUtils .release (dataBuffer );
664
669
}
665
670
}
666
671
else {
672
+ MultipartUtils .closeChannel (this .channel );
673
+ MultipartUtils .deleteFile (this .file );
667
674
DataBufferUtils .release (dataBuffer );
668
675
emitError (new DataBufferLimitException (
669
676
"Part exceeded the disk usage limit of " + PartGenerator .this .maxDiskUsagePerPart +
@@ -674,6 +681,7 @@ public void body(DataBuffer dataBuffer) {
674
681
@ Override
675
682
public void partComplete (boolean finalPart ) {
676
683
MultipartUtils .closeChannel (this .channel );
684
+ this .deleteOnDispose = false ;
677
685
emitPart (DefaultParts .part (this .headers , this .file , PartGenerator .this .blockingOperationScheduler ));
678
686
if (finalPart ) {
679
687
emitComplete ();
@@ -685,6 +693,9 @@ public void dispose() {
685
693
if (this .closeOnDispose ) {
686
694
MultipartUtils .closeChannel (this .channel );
687
695
}
696
+ if (this .deleteOnDispose ) {
697
+ MultipartUtils .deleteFile (this .file );
698
+ }
688
699
}
689
700
690
701
@@ -710,6 +721,8 @@ private final class WritingFileState implements State {
710
721
711
722
private volatile boolean finalPart ;
712
723
724
+ private volatile boolean disposed ;
725
+
713
726
714
727
public WritingFileState (CreateFileState state , Path file , WritableByteChannel channel ) {
715
728
this .headers = state .headers ;
@@ -761,11 +774,15 @@ private void writeComplete() {
761
774
if (this .completed ) {
762
775
newState .partComplete (this .finalPart );
763
776
}
777
+ else if (this .disposed ) {
778
+ newState .dispose ();
779
+ }
764
780
else if (changeState (this , newState )) {
765
781
requestToken ();
766
782
}
767
783
else {
768
784
MultipartUtils .closeChannel (this .channel );
785
+ MultipartUtils .deleteFile (this .file );
769
786
}
770
787
}
771
788
@@ -779,13 +796,21 @@ private Mono<Void> writeInternal(DataBuffer dataBuffer) {
779
796
return Mono .empty ();
780
797
}
781
798
catch (IOException ex ) {
799
+ MultipartUtils .closeChannel (this .channel );
800
+ MultipartUtils .deleteFile (this .file );
782
801
return Mono .error (ex );
783
802
}
784
803
finally {
785
804
DataBufferUtils .release (dataBuffer );
786
805
}
787
806
}
788
807
808
+ @ Override
809
+ public void dispose () {
810
+ this .disposed = true ;
811
+ }
812
+
813
+
789
814
@ Override
790
815
public String toString () {
791
816
return "WRITE-FILE" ;
0 commit comments