0

This is my controller

Cotroller

def download
    data = open(@attachment.file.url).read

    @attachment.clicks = @attachment.clicks.to_i + 1
    @attachment.save
    send_data data, :type => @attachment.content_type, :filename => @attachment.name
  end

example:

@attachment.file.url = "http://my_bucket.cloudfront.net/uploads/attachment/file/50/huge_file.pptx" 

I did this, but if @attachement is a huge file (eg. 300MB), my server crash. I want to allow users to download the file in the browser directly from my AWS server?

2) tip: Do you suggest to download file from S3 (where they are stored) or with CloudFront?

sparkle
  • 7,530
  • 22
  • 69
  • 131
  • Why you not give the @attachment.file.url to user directly? So it won't eat your cpu memory? – Saiqul Haq May 15 '15 at 15:33
  • Because I want to track number of clicks. So I have to pass through a controller – sparkle May 15 '15 at 15:39
  • 1
    Nginx X-Accel-Redirect is the only way to serve big files. Basically Rails action returns back a file name via HTTP header to Nginx internal location. This location serves through Nginx S3 proxy without any backend. In that case Rails finishes request quickly and not blocked by downloading process. – Anatoly May 17 '15 at 19:48

1 Answers1

2

If you using carrierwave gem, you can try this to track number of clicks

def download
  @attachment.clicks.to_i += 1
  @attachment.save
  redirect_to @attachment.file.url(query: {"response-content-disposition" => "attachment;"})
end

references:

Community
  • 1
  • 1
Saiqul Haq
  • 2,287
  • 16
  • 18