dart-lang / shelf

Web server middleware for Dart
https://pub.dev/packages/shelf
BSD 3-Clause "New" or "Revised" License
893 stars 125 forks source link

how to safety close file stream #407

Open MisterChangRay opened 5 months ago

MisterChangRay commented 5 months ago

i wanna write a file download server.

as Response code as following

        return Response(
          200,
          body: File(file.path!).openRead(),
          headers: headers,
        );

as u see, i send file as stream for return;

but, i has a proplem, on browser download large file , if user cancel download process, the file has never be closed!

so, how to get the socket error for close the file stream?

MisterChangRay commented 5 months ago

i saw the code in file shelf_io.dart at line 254

  return httpResponse
      .addStream(response.read())
      .then((_) => httpResponse.close());

this then call be stream read end, if exception has occur, the call not be execute.

and i search on google, find the HttpResonse has a done method, this method has called by any case;

so , can expose the httpResonse.done method?

kevmoo commented 5 months ago

@MisterChangRay – ooo...this might be a bug, honestly!

MisterChangRay commented 5 months ago

@MisterChangRay – ooo...this might be a bug, honestly!

can fixed the bug in next version?

MisterChangRay commented 4 months ago

@kevmoo

wengxianxun commented 4 months ago

i need too, tk

pedia commented 3 months ago

I think #399 is same issue.