redstone-dart / redstone

A metadata driven microframework for Dart.
http://redstone-dart.github.io/redstone
MIT License
342 stars 42 forks source link

File not closed after its downloaded #138

Closed vimalraj-a closed 8 years ago

vimalraj-a commented 9 years ago

Hi,

I have created a simple dart file server for downloading a file from server. I can download the file. But, I cannot delete the file from server after its downloaded.

here is my code :

import 'package:redstone/server.dart' as app; import 'dart:io'; import 'dart:async';

@app.Interceptor("/files/.*") downloadFileInterceptor() { app.chain.next(() => app.response.change( headers: { "Content-Disposition": "attachment; filename=\"${app.request.url.pathSegments.last}\"" })); }

@app.Route("/files/:fileName") Future helloWorld(String fileName) { File f = new File("C:\Users\xxxx\Desktop\$fileName"); Future ff = new Future(() => f); return ff; }

main() { app.setupConsoleLog(); app.start(); }

Lets say I am downloading s.7z file from my desktop. I hope File instance for s.7z is not closed after response sent to client.

vimalraj-a commented 8 years ago

You guys are not taking this as serious issue?. I can't remove the file which is been downloaded before......

azenla commented 8 years ago

@cgarciae @austincummings @luizmineo Anyone have any idea?

Pacane commented 8 years ago

@vimalraj-a What do you mean by "you can't remove it". Your Dart app can't remove it or the OS can't remove it? What happens if you run rm -rf path-to-filename ? What does the OS say?

vimalraj-a commented 8 years ago

I am using windows machine. I got error message from windows like 'file has been used by some other process'

Pacane commented 8 years ago

Even after rebooting your computer?

vimalraj-a commented 8 years ago

No, Dart.exe process using that file. I can delete the file only if I stop redstone server app or killing dart.exe process.

Pacane commented 8 years ago

By checking at https://github.com/dart-lang/shelf_static/blob/master/lib/src/static_handler.dart#L110 you might want to try to return a response.ok(file.openRead())

vimalraj-a commented 8 years ago

I am not able to find app.response.ok() method on redstone: 0.5.21+1. If you would add the snippet in this method would be great to me.

Pacane commented 8 years ago

I'm not sure anymore about what the api looks like in 0.5.21+1, but you may want to look for shelf responses.

vimalraj-a commented 8 years ago

shelf.Response helloWorld(String fileName) { File f = new File("C:\Users\xxx\Desktop\$fileName"); Map headers = {"Content-Disposition" : 'attachment; filename="${web.request.url.pathSegments.last}"}';

return new shelf.Response.ok(f.openRead(), headers: headers); }

This works for me. Thanks @Pacane