From 40a9e19d173ea71572185b3acab3f4ea52473481 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Sun, 29 Mar 2015 15:58:02 -0700 Subject: Adding jsonp support and incrementing the hit counter. --- Gemfile | 5 ++--- Gemfile.lock | 11 +++-------- app/controllers/downloads_controller.rb | 26 +++++++++++++++++--------- config/application.rb | 2 ++ 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/Gemfile b/Gemfile index eeb80d1..d42396b 100644 --- a/Gemfile +++ b/Gemfile @@ -34,10 +34,9 @@ gem 'sdoc', '~> 0.4.0', group: :doc gem 'aws-sdk' -group :development, :test do - # Call 'debugger' anywhere in the code to stop execution and get a debugger console - gem 'debugger' +gem 'rack-jsonp-middleware', :require => 'rack/jsonp' +group :development, :test do # Access an IRB console on exception pages or by using <%= console %> in views gem 'web-console', '~> 2.0' diff --git a/Gemfile.lock b/Gemfile.lock index 5943695..8c97be6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -56,14 +56,7 @@ GEM coffee-script-source execjs coffee-script-source (1.9.1) - columnize (0.9.0) debug_inspector (0.0.2) - debugger (1.6.8) - columnize (>= 0.3.1) - debugger-linecache (~> 1.2.0) - debugger-ruby_core_source (~> 1.3.5) - debugger-linecache (1.2.0) - debugger-ruby_core_source (1.3.8) dotenv (2.0.0) dotenv-rails (2.0.0) dotenv (= 2.0.0) @@ -95,6 +88,8 @@ GEM nokogiri (1.6.6.2) mini_portile (~> 0.6.0) rack (1.6.0) + rack-jsonp-middleware (0.0.10) + rack rack-test (0.6.3) rack (>= 1.0) rails (4.2.0) @@ -167,10 +162,10 @@ PLATFORMS DEPENDENCIES aws-sdk coffee-rails (~> 4.1.0) - debugger dotenv-rails jbuilder (~> 2.0) jquery-rails + rack-jsonp-middleware rails (= 4.2.0) sass-rails (~> 5.0) sdoc (~> 0.4.0) 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 diff --git a/config/application.rb b/config/application.rb index f12e289..6f95750 100644 --- a/config/application.rb +++ b/config/application.rb @@ -22,5 +22,7 @@ module DownloadsJesterpmNet # Do not swallow errors in after_commit/after_rollback callbacks. config.active_record.raise_in_transactional_callbacks = true + + config.middleware.use Rack::JSONP end end -- cgit v1.2.3