diff options
| author | Jesse Morgan <jesse@jesterpm.net> | 2015-03-29 15:58:02 -0700 | 
|---|---|---|
| committer | Jesse Morgan <jesse@jesterpm.net> | 2015-03-29 15:58:02 -0700 | 
| commit | 40a9e19d173ea71572185b3acab3f4ea52473481 (patch) | |
| tree | 8b3e74df15deee6cd11b7729becf79e1ecc0d20c /app | |
| parent | 628d29d1e262114c3f95f12941268cd9e57cc0ad (diff) | |
Adding jsonp support and incrementing the hit counter.
Diffstat (limited to 'app')
| -rw-r--r-- | app/controllers/downloads_controller.rb | 26 | 
1 files changed, 17 insertions, 9 deletions
| diff --git a/app/controllers/downloads_controller.rb b/app/controllers/downloads_controller.rb index 14e3b04..d98ccc3 100644 --- a/app/controllers/downloads_controller.rb +++ b/app/controllers/downloads_controller.rb @@ -14,19 +14,27 @@ class DownloadsController < ApplicationController    def show      not_found if @download.nil? -    s3client = Aws::S3::Client.new() -    s3obj = Aws::S3::Object.new(ENV['S3_DOWNLOADS_BUCKET'], @download.filename, client: s3client) -    resp = s3obj.get +    begin +      s3client = Aws::S3::Client.new() +      s3obj = Aws::S3::Object.new(ENV['S3_DOWNLOADS_BUCKET'], @download.filename, client: s3client) +      resp = s3obj.get -    FileUtils.mkdir_p(Rails.root.join('tmp', 'downloads')) +      FileUtils.mkdir_p(Rails.root.join('tmp', 'downloads')) -    cached_filename = Rails.root.join('tmp', 'downloads', @download.filename) +      cached_filename = Rails.root.join('tmp', 'downloads', @download.filename) -    File.open(cached_filename, 'wb') do |file| -      file.write resp.body.read -    end +      File.open(cached_filename, 'wb') do |file| +        file.write resp.body.read +      end + +      @download.hits += 1 +      @download.save -    send_file cached_filename +      send_file cached_filename + +    rescue Aws::S3::Errors::NoSuchKey +      not_found +    end    end    private | 
