@@ -182,50 +182,43 @@ const downloadFineTuningOutput = async (req: Request, res: Response, next: NextF
182182
183183 const fs = require ( 'fs' )
184184
185+ // Get file stats for Content-Length header (enables browser progress bar)
186+ const fileStats = fs . statSync ( filePath )
187+ const fileSize = fileStats . size
188+
185189 // Set response headers for file download
186190 const fileName = `${ jobId } -output.zip`
187191 res . setHeader ( 'Content-Type' , 'application/zip' )
188192 res . setHeader ( 'Content-Disposition' , `attachment; filename="${ fileName } "` )
193+ res . setHeader ( 'Content-Length' , fileSize )
189194
190195 // Stream the file
191196 const fileStream = fs . createReadStream ( filePath )
192197
193198 // Log when stream opens
194199 fileStream . on ( 'open' , ( ) => {
195- console . debug ( `finetuningController.downloadFineTuningOutput - starting to stream: ${ filePath } ` )
200+ console . debug ( `finetuningController.downloadFineTuningOutput - starting to stream: ${ filePath } ( ${ fileSize } bytes) ` )
196201 } )
197-
198- // Delete zip file after response fully finishes (browser completes download)
199- res . on ( 'finish' , ( ) => {
200- setTimeout ( ( ) => {
201- try {
202- if ( fs . existsSync ( filePath ) ) {
203- fs . unlinkSync ( filePath )
204- console . debug ( `finetuningController.downloadFineTuningOutput - deleted zip after download complete: ${ filePath } ` )
205- }
206- } catch ( deleteErr : any ) {
207- console . warn ( `finetuningController.downloadFineTuningOutput - failed to delete zip: ${ deleteErr ?. message || deleteErr } ` )
208- }
209- } , 100 )
202+
203+ // Log when the file stream closes (end of stream on server side)
204+ fileStream . on ( 'close' , ( ) => {
205+ console . debug ( `finetuningController.downloadFineTuningOutput - end stream: ${ filePath } ` )
210206 } )
211207
208+ // Multiple users can download the same ZIP simultaneously
212209 fileStream . on ( 'error' , ( err : any ) => {
213210 console . error ( 'finetuningController.downloadFineTuningOutput - error streaming file:' , err )
214- // Try to delete zip on error too
215- try {
216- if ( fs . existsSync ( filePath ) ) {
217- fs . unlinkSync ( filePath )
218- console . debug ( `finetuningController.downloadFineTuningOutput - deleted zip after error: ${ filePath } ` )
219- }
220- } catch ( deleteErr : any ) {
221- console . warn ( `finetuningController.downloadFineTuningOutput - failed to delete zip on error: ${ deleteErr ?. message || deleteErr } ` )
222- }
223211 if ( ! res . headersSent ) {
224212 res . status ( StatusCodes . INTERNAL_SERVER_ERROR ) . json ( {
225213 error : 'Error streaming fine-tuning output file'
226214 } )
227215 }
228216 } )
217+
218+ // Log when HTTP response finishes sending bytes to client
219+ res . on ( 'finish' , ( ) => {
220+ console . debug ( `finetuningController.downloadFineTuningOutput - response finished streaming: ${ filePath } ` )
221+ } )
229222
230223 fileStream . pipe ( res )
231224 } catch ( error ) {
0 commit comments