Module: Gluttonberg::Library::Storage::S3

Extended by:
ActiveSupport::Concern
Defined in:
lib/gluttonberg/library/storage/s3.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#assets_directory_public_urlObject



108
109
110
# File 'lib/gluttonberg/library/storage/s3.rb', line 108

def assets_directory_public_url
  "#{s3_bucket_root_url}/user_assets"
end

#bucket_handleObject

InstanceMethods



84
85
86
# File 'lib/gluttonberg/library/storage/s3.rb', line 84

def bucket_handle
  @bucket ||= self.class.bucket_handle
end

#bucket_handle=(handle) ⇒ Object



88
89
90
# File 'lib/gluttonberg/library/storage/s3.rb', line 88

def bucket_handle=(handle)
  @bucket = handle
end

#copy_audios_to_s3Object

This method is used for delayed job



166
167
168
# File 'lib/gluttonberg/library/storage/s3.rb', line 166

def copy_audios_to_s3
  copy_file_to_s3(self.file_name)
end

#copy_file_to_s3(file_name) ⇒ Object

takes file from tmp folder and upload to s3 if s3 info is given in CMS settings



154
155
156
157
158
159
160
161
162
163
# File 'lib/gluttonberg/library/storage/s3.rb', line 154

def copy_file_to_s3(file_name)
  bucket = bucket_handle
  if bucket
    local_file = self.tmp_directory + "/" + file_name
    puts "Copying #{file_name} (#{local_file}) to #{self.class.s3_bucket_name}"
    bucket_key = bucket.objects[self.directory + "/" + file_name]
    self.class.upload_file_to(self, bucket_key, self.mime_type, local_file)
    self.update_column(:copied_to_s3 , true)
  end
end

#directoryObject

The generated directory where this file is located.



93
94
95
96
# File 'lib/gluttonberg/library/storage/s3.rb', line 93

def directory
  self.class.storage_setup if Library.root.blank?
  Library.root + "/" + self.asset_hash
end

#download_asset_to_tmp_fileObject



181
182
183
# File 'lib/gluttonberg/library/storage/s3.rb', line 181

def download_asset_to_tmp_file
  download_orginal_file_from_s3
end

#download_orginal_file_from_s3Object



185
186
187
188
189
# File 'lib/gluttonberg/library/storage/s3.rb', line 185

def download_orginal_file_from_s3
  FileUtils.mkdir(self.tmp_directory) unless File.exists?(self.tmp_directory)
  _download_file_from_s3(self.location_on_disk, self.tmp_location_on_disk)
  _download_file_from_s3(self.original_file_on_disk, self.tmp_original_file_on_disk)
end

#make_backup(replace_backup = true) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/gluttonberg/library/storage/s3.rb', line 112

def make_backup(replace_backup=true)
  unless File.exist?(tmp_original_file_on_disk)
    FileUtils.cp tmp_location_on_disk, tmp_original_file_on_disk
    FileUtils.chmod(0755,tmp_original_file_on_disk)
    move_tmp_file_to_actual_directory("original_" + file_name , true)
  end
end

#move_tmp_file_to_actual_directory(file_name, tmp_file_dirty = true) ⇒ Object



146
147
148
149
150
151
# File 'lib/gluttonberg/library/storage/s3.rb', line 146

def move_tmp_file_to_actual_directory(file_name , tmp_file_dirty=true)
  if self.file_dirty == true || tmp_file_dirty == true
    copy_file_to_s3(file_name)
    self.file_dirty = false
  end
end

#remove_asset_folder_from_s3Object



174
175
176
177
178
179
# File 'lib/gluttonberg/library/storage/s3.rb', line 174

def remove_asset_folder_from_s3
  bucket = bucket_handle
  unless bucket.blank?
    bucket.objects.with_prefix(self.directory).delete_all
  end
end

#remove_file_from_s3(file_name) ⇒ Object

TODO



171
172
# File 'lib/gluttonberg/library/storage/s3.rb', line 171

def remove_file_from_s3(file_name)
end

#remove_file_from_storageObject



120
121
122
123
# File 'lib/gluttonberg/library/storage/s3.rb', line 120

def remove_file_from_storage
  remove_file_from_tmp_storage
  remove_asset_folder_from_s3
end

#remove_file_from_tmp_storageObject



125
126
127
128
129
130
# File 'lib/gluttonberg/library/storage/s3.rb', line 125

def remove_file_from_tmp_storage
  if File.exists?(tmp_directory)
    puts "Remove assset folder from tmp storage (#{tmp_directory})"
    FileUtils.rm_r(tmp_directory)
  end
end

#s3_bucket_root_urlObject



104
105
106
# File 'lib/gluttonberg/library/storage/s3.rb', line 104

def s3_bucket_root_url
  "http://#{self.class.s3_server_url}/#{self.class.s3_bucket_name}"
end

#tmp_directoryObject

The generated tmp directory where we will locate this file temporarily for processing.



99
100
101
102
# File 'lib/gluttonberg/library/storage/s3.rb', line 99

def tmp_directory
  self.class.storage_setup if Library.tmp_root.blank?
  Library.tmp_root + "/" + self.asset_hash
end

#update_file_on_storageObject



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/gluttonberg/library/storage/s3.rb', line 132

def update_file_on_storage
  if file
    FileUtils.mkdir(tmp_directory) unless File.exists?(tmp_directory)
    FileUtils.cp file.tempfile.path, tmp_location_on_disk
    FileUtils.chmod(0755, tmp_location_on_disk)

    move_tmp_file_to_actual_directory(file_name , false)
    #  new file has been upload, if its image generate thumbnails, if mp3 collect sound info.
    asset_processing
    # delete local tmp folder
    remove_file_from_tmp_storage  unless self.asset_type.asset_category.name == "video"
  end
end