From 9e0246684d14e5323cfb71291faff290e2762f48 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Wed, 15 May 2013 22:35:59 +0300 Subject: [PATCH] Files controller handle redirect to remote storage. Added config block to carrierwave init --- app/controllers/files_controller.rb | 12 ++++++++---- app/uploaders/attachment_uploader.rb | 10 +++++----- config/initializers/carrierwave.rb | 20 +++++++++++--------- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/app/controllers/files_controller.rb b/app/controllers/files_controller.rb index 3cd2e77..bf30de5 100644 --- a/app/controllers/files_controller.rb +++ b/app/controllers/files_controller.rb @@ -1,12 +1,16 @@ class FilesController < ApplicationController def download note = Note.find(params[:id]) + uploader = note.attachment - if can?(current_user, :read_project, note.project) - uploader = note.attachment - send_file uploader.file.path, disposition: 'attachment' + if uploader.file_storage? + if can?(current_user, :read_project, note.project) + send_file uploader.file.path, disposition: 'attachment' + else + not_found! + end else - not_found! + redirect_to uploader.url end end end diff --git a/app/uploaders/attachment_uploader.rb b/app/uploaders/attachment_uploader.rb index 960e71e..c0afe96 100644 --- a/app/uploaders/attachment_uploader.rb +++ b/app/uploaders/attachment_uploader.rb @@ -21,10 +21,10 @@ class AttachmentUploader < CarrierWave::Uploader::Base end def secure_url - if self.class.storage == CarrierWave::Storage::File - "/files/#{model.class.to_s.underscore}/#{model.id}/#{file.filename}" - else - url - end + "/files/#{model.class.to_s.underscore}/#{model.id}/#{file.filename}" + end + + def file_storage? + self.class.storage == CarrierWave::Storage::File end end diff --git a/config/initializers/carrierwave.rb b/config/initializers/carrierwave.rb index 6659c1a..45bc68f 100644 --- a/config/initializers/carrierwave.rb +++ b/config/initializers/carrierwave.rb @@ -5,13 +5,15 @@ aws_file = Rails.root.join('config', 'aws.yml') if File.exists?(aws_file) AWS_CONFIG = YAML.load(File.read(aws_file))[Rails.env] - config.fog_credentials = { - provider: 'AWS', # required - aws_access_key_id: AWS_CONFIG['access_key_id'], # required - aws_secret_access_key: AWS_CONFIG['secret_access_key'], # required - region: AWS_CONFIG['region'], # optional, defaults to 'us-east-1' - } - config.fog_directory = AWS_CONFIG['bucket'] # required - config.fog_public = false # optional, defaults to true - config.fog_attributes = {'Cache-Control'=>'max-age=315576000'} # optional, defaults to {} + CarrierWave.configure do |config| + config.fog_credentials = { + provider: 'AWS', # required + aws_access_key_id: AWS_CONFIG['access_key_id'], # required + aws_secret_access_key: AWS_CONFIG['secret_access_key'], # required + region: AWS_CONFIG['region'], # optional, defaults to 'us-east-1' + } + config.fog_directory = AWS_CONFIG['bucket'] # required + config.fog_public = false # optional, defaults to true + config.fog_attributes = {'Cache-Control'=>'max-age=315576000'} # optional, defaults to {} + end end -- libgit2 0.21.2