Module: Cms::S3::Attachment
- Defined in:
- lib/bcms_s3/s3_module.rb
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.included(model_class) ⇒ Object
52 53 54 55 |
# File 'lib/bcms_s3/s3_module.rb', line 52 def self.included(model_class) model_class.alias_method_chain :set_file_location, :s3 model_class.alias_method_chain :write_temp_file_to_storage_location, :s3 end |
Instance Method Details
#set_file_location_with_s3 ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/bcms_s3/s3_module.rb', line 56 def set_file_location_with_s3 unless temp_file.blank? prefix = temp_file.original_filename.sub(/\..+$/,'') if temp_file.original_filename =~ /.+(\..+)$/ suffix = $1 else suffix = "" end new_filename = "#{prefix}-#{ActiveSupport::SecureRandom.hex(4)}#{suffix}" self.file_location = "#{Time.now.strftime("%Y/%m/%d")}/#{new_filename}" end end |
#write_temp_file_to_storage_location_with_s3 ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/bcms_s3/s3_module.rb', line 68 def write_temp_file_to_storage_location_with_s3 unless temp_file.blank? FileUtils.mkdir_p File.dirname(full_file_location) if !Cms::S3.enabled if temp_file.local_path if Cms::S3.enabled s3 = RightAws::S3.new(Cms::S3.[:access_key_id], Cms::S3.[:secret_access_key] ) bucket = s3.bucket(Cms::S3.[:bucket], true, 'public-read') key = RightAws::S3::Key.create(bucket, file_location) key.put(temp_file.read,'public-read', {"Content-Type" => file_type}) else FileUtils.copy temp_file.local_path, full_file_location end else open(full_file_location, 'w') {|f| f << temp_file.read } end if Cms. && !Cms::S3.enabled FileUtils.chmod Cms., full_file_location end end end |