# Send\_file and locking files after download?

**URL:** <https://www.ruby-forum.com/t/send-file-and-locking-files-after-download/60504>\
**Category:** Rails\
**Created:** [May 15, 2006, 1:08am UTC](https://www.ruby-forum.com/t/send-file-and-locking-files-after-download/60504 "2006-05-15T01:08:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![markedman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f05b48/32.png) [@markedman](https://www.ruby-forum.com/u/markedman)\
**Post date:** [May 15, 2006, 1:08am UTC](https://www.ruby-forum.com/t/send-file-and-locking-files-after-download/60504/1 "2006-05-15T01:08:13Z")

</div>

Hello,

I’m working on a download service which grants users a one-time-download  
access to files. The idea is to write an entry to a database table after  
a successful download that “locks” the download link for the user.

After doing some quick tests with send\_file I noticed that send\_file  
does not block until the user has downloaded the full file, but proceeds  
with the rest of the controller code. For example:

send\_file fullpath, :stream =\> false, :disposition =\> ‘attachment’,  
:filename =\> filename  
logger.info “\*\*\*\*\*\*\*\*\*\*\* LOCK DOWNLOAD!! \*\*\*\*\*\*\*\*\*\*\*\*\*\*”

After send\_file starts the file transfer, I can immediately see the  
“LOCK DOWNLOAD” entry in the log files. This would mean that if the user  
interrupts a big file download, it would still be locked.

What would be the solution? Do I have to somehow (…how?) write my own  
buffering file sending which blocks the control and breaks out of the  
controller if the user interrupts the transfer?

I would imagine :stream =\> true could be a step closer to my needs, but  
it doesn’t seem to work since all I get is “Rails application failed to  
start properly”.

Any ideas?

---

<div class="post-metadata">

**Author:** ![markedman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f05b48/32.png) [@markedman](https://www.ruby-forum.com/u/markedman)\
**Post date:** [May 15, 2006, 8:38am UTC](https://www.ruby-forum.com/t/send-file-and-locking-files-after-download/60504/2 "2006-05-15T08:38:44Z")

</div>

On Mon, 2006-05-15 at 01:08 +0200, Markku MÃ¤kelÃ¤ wrote:

> Hello,
> 
> I’m working on a download service which grants users a one-time-download  
> access to files. The idea is to write an entry to a database table after  
> a successful download that “locks” the download link for the user.

You should google “X-Sendfile”. It might help.

–  
Zed A. Shaw

> **[Zed A. shaw](https://zedshaw.com/)**
>
> Personal essays and writings by Zed A. Shaw.

[http://mongrel.rubyforge.org/](http://mongrel.rubyforge.org/)

---

<div class="post-metadata">

**Author:** ![markedman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f05b48/32.png) [@markedman](https://www.ruby-forum.com/u/markedman)\
**Post date:** [May 15, 2006, 8:47pm UTC](https://www.ruby-forum.com/t/send-file-and-locking-files-after-download/60504/3 "2006-05-15T20:47:39Z")

</div>

Zed S. wrote:

> You should google “X-Sendfile”. It might help.

Hmm I don’t see how? My problem is that I want to execute code in the  
controller _after_ a successful download, but not if the download was  
interrupted.

mod\_xsendfile just seems to replace Rails’ send\_file, but doesn’t allow  
the control to return to the controller after download.

Nice looking little module anyway, might prove useful in some other  
project, so thanks anyway.

-markku

---

<div class="post-metadata">

**Author:** ![markedman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f05b48/32.png) [@markedman](https://www.ruby-forum.com/u/markedman)\
**Post date:** [May 15, 2006, 8:55pm UTC](https://www.ruby-forum.com/t/send-file-and-locking-files-after-download/60504/4 "2006-05-15T20:55:49Z")

</div>

To be more exact, what I’m looking for is

- first looping through a file send: block until user has downloaded the  
last byte of the file, or interrupts the transfer

- then check if the connection was aborted, like PHP’s if(  
connection\_status() == 0 ) { lock\_the\_file(); }

The problem is that send\_file seems to be non-blocking.
