Commit 9e024668 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Files controller handle redirect to remote storage. Added config block to carrierwave init

parent ced044ad
class FilesController < ApplicationController class FilesController < ApplicationController
def download def download
note = Note.find(params[:id]) note = Note.find(params[:id])
uploader = note.attachment
if uploader.file_storage?
if can?(current_user, :read_project, note.project) if can?(current_user, :read_project, note.project)
uploader = note.attachment
send_file uploader.file.path, disposition: 'attachment' send_file uploader.file.path, disposition: 'attachment'
else else
not_found! not_found!
end end
else
redirect_to uploader.url
end
end end
end end
...@@ -21,10 +21,10 @@ class AttachmentUploader < CarrierWave::Uploader::Base ...@@ -21,10 +21,10 @@ class AttachmentUploader < CarrierWave::Uploader::Base
end end
def secure_url def secure_url
if self.class.storage == CarrierWave::Storage::File
"/files/#{model.class.to_s.underscore}/#{model.id}/#{file.filename}" "/files/#{model.class.to_s.underscore}/#{model.id}/#{file.filename}"
else
url
end end
def file_storage?
self.class.storage == CarrierWave::Storage::File
end end
end end
...@@ -5,6 +5,7 @@ aws_file = Rails.root.join('config', 'aws.yml') ...@@ -5,6 +5,7 @@ aws_file = Rails.root.join('config', 'aws.yml')
if File.exists?(aws_file) if File.exists?(aws_file)
AWS_CONFIG = YAML.load(File.read(aws_file))[Rails.env] AWS_CONFIG = YAML.load(File.read(aws_file))[Rails.env]
CarrierWave.configure do |config|
config.fog_credentials = { config.fog_credentials = {
provider: 'AWS', # required provider: 'AWS', # required
aws_access_key_id: AWS_CONFIG['access_key_id'], # required aws_access_key_id: AWS_CONFIG['access_key_id'], # required
...@@ -14,4 +15,5 @@ if File.exists?(aws_file) ...@@ -14,4 +15,5 @@ if File.exists?(aws_file)
config.fog_directory = AWS_CONFIG['bucket'] # required config.fog_directory = AWS_CONFIG['bucket'] # required
config.fog_public = false # optional, defaults to true config.fog_public = false # optional, defaults to true
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'} # optional, defaults to {} config.fog_attributes = {'Cache-Control'=>'max-age=315576000'} # optional, defaults to {}
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment