Commit 9e0246684d14e5323cfb71291faff290e2762f48
1 parent
ced044ad
Exists in
master
and in
4 other branches
Files controller handle redirect to remote storage. Added config block to carrierwave init
Showing
3 changed files
with
24 additions
and
18 deletions
Show diff stats
app/controllers/files_controller.rb
| 1 | class FilesController < ApplicationController | 1 | class FilesController < ApplicationController |
| 2 | def download | 2 | def download |
| 3 | note = Note.find(params[:id]) | 3 | note = Note.find(params[:id]) |
| 4 | + uploader = note.attachment | ||
| 4 | 5 | ||
| 5 | - if can?(current_user, :read_project, note.project) | ||
| 6 | - uploader = note.attachment | ||
| 7 | - send_file uploader.file.path, disposition: 'attachment' | 6 | + if uploader.file_storage? |
| 7 | + if can?(current_user, :read_project, note.project) | ||
| 8 | + send_file uploader.file.path, disposition: 'attachment' | ||
| 9 | + else | ||
| 10 | + not_found! | ||
| 11 | + end | ||
| 8 | else | 12 | else |
| 9 | - not_found! | 13 | + redirect_to uploader.url |
| 10 | end | 14 | end |
| 11 | end | 15 | end |
| 12 | end | 16 | end |
app/uploaders/attachment_uploader.rb
| @@ -21,10 +21,10 @@ class AttachmentUploader < CarrierWave::Uploader::Base | @@ -21,10 +21,10 @@ class AttachmentUploader < CarrierWave::Uploader::Base | ||
| 21 | end | 21 | end |
| 22 | 22 | ||
| 23 | def secure_url | 23 | def secure_url |
| 24 | - if self.class.storage == CarrierWave::Storage::File | ||
| 25 | - "/files/#{model.class.to_s.underscore}/#{model.id}/#{file.filename}" | ||
| 26 | - else | ||
| 27 | - url | ||
| 28 | - end | 24 | + "/files/#{model.class.to_s.underscore}/#{model.id}/#{file.filename}" |
| 25 | + end | ||
| 26 | + | ||
| 27 | + def file_storage? | ||
| 28 | + self.class.storage == CarrierWave::Storage::File | ||
| 29 | end | 29 | end |
| 30 | end | 30 | end |
config/initializers/carrierwave.rb
| @@ -5,13 +5,15 @@ aws_file = Rails.root.join('config', 'aws.yml') | @@ -5,13 +5,15 @@ aws_file = Rails.root.join('config', 'aws.yml') | ||
| 5 | if File.exists?(aws_file) | 5 | if File.exists?(aws_file) |
| 6 | AWS_CONFIG = YAML.load(File.read(aws_file))[Rails.env] | 6 | AWS_CONFIG = YAML.load(File.read(aws_file))[Rails.env] |
| 7 | 7 | ||
| 8 | - config.fog_credentials = { | ||
| 9 | - provider: 'AWS', # required | ||
| 10 | - aws_access_key_id: AWS_CONFIG['access_key_id'], # required | ||
| 11 | - aws_secret_access_key: AWS_CONFIG['secret_access_key'], # required | ||
| 12 | - region: AWS_CONFIG['region'], # optional, defaults to 'us-east-1' | ||
| 13 | - } | ||
| 14 | - config.fog_directory = AWS_CONFIG['bucket'] # required | ||
| 15 | - config.fog_public = false # optional, defaults to true | ||
| 16 | - config.fog_attributes = {'Cache-Control'=>'max-age=315576000'} # optional, defaults to {} | 8 | + CarrierWave.configure do |config| |
| 9 | + config.fog_credentials = { | ||
| 10 | + provider: 'AWS', # required | ||
| 11 | + aws_access_key_id: AWS_CONFIG['access_key_id'], # required | ||
| 12 | + aws_secret_access_key: AWS_CONFIG['secret_access_key'], # required | ||
| 13 | + region: AWS_CONFIG['region'], # optional, defaults to 'us-east-1' | ||
| 14 | + } | ||
| 15 | + config.fog_directory = AWS_CONFIG['bucket'] # required | ||
| 16 | + config.fog_public = false # optional, defaults to true | ||
| 17 | + config.fog_attributes = {'Cache-Control'=>'max-age=315576000'} # optional, defaults to {} | ||
| 18 | + end | ||
| 17 | end | 19 | end |