inveniosoftware / invenio-files-rest

REST API for uploading/downloading files for Invenio.
https://invenio-files-rest.readthedocs.io
MIT License
9 stars 68 forks source link

signals: "file_downloaded" sent too early #180

Open slint opened 6 years ago

slint commented 6 years ago

The file_downloaded signal is sent too early during the file download request. This is bad for e.g. invenio-stats, which relies on the signal for gathering file download statistics. In case the download is interrupted for some reason (e.g. connection dropped or the user canceled the download), the signal will already have been sent.

A possible solution is to use the flask.after_this_request callback, and send the signal afterward if the request was successful (i.e. 2xx/3xx).

lnielsen commented 6 years ago

In case send_file() sends a redirect (e.g. x-sendfile) won't this be the same problem?

slint commented 6 years ago

True. In general, after some local testing, apparently, it's not easy (if even possible at all) to keep track of when a file was actually downloaded or if the connection was dropped... One idea is using werkzeug.wsgi.ClosingIterator which can execute a callback after the .close() is called for the file, but this still doesn't solve the issue...

(TBH the issue rises from what COUNTER and other discussions related to datasets/files would like to track about files. One of the things e.g. is the total transferred volume of a dataset/file, which one cannot know unless we know that the file arrived someplace...)