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

Defined in:
lib/gluttonberg/library/storage/s3.rb

Instance Method Summary collapse

Instance Method Details

#assets_directory_public_urlObject



97
98
99
# File 'lib/gluttonberg/library/storage/s3.rb', line 97

def assets_directory_public_url
  "#{s3_bucket_root_url}/user_assets"
end

#bucket_handleObject



73
74
75
76
77
78
79
# File 'lib/gluttonberg/library/storage/s3.rb', line 73

def bucket_handle
  unless @bucket.blank?
    @bucket
  else
    @bucket = S3::ClassMethods.bucket_handle
  end
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



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/gluttonberg/library/storage/s3.rb', line 143

def copy_file_to_s3(file_name)
  bucket = bucket_handle
  unless bucket.blank?
    local_file = self.tmp_directory + "/" + file_name
    folder = self.asset_hash
    date = Time.now+1.years
    puts "Copying #{file_name} (#{local_file}) to #{S3::ClassMethods.s3_bucket_name}"
    key = bucket.objects[self.directory + "/" + file_name]
    unless self.mime_type.blank?
      key.write(File.open(local_file), {:expires => date.rfc2822, :content_type => self.mime_type , :acl => :public_read })
    else
      key.write(File.open(local_file) , {:expires => date.rfc2822 , :acl => :public_read })
    end
    self.update_column(:copied_to_s3 , true)
    puts "Copied"
  end
end

#directoryObject

The generated directory where this file is located.



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

def directory
  S3::ClassMethods.storage_setup if Library.root.blank?
  Library.root + "/" + self.asset_hash
end

#download_asset_to_tmp_fileObject



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

def download_asset_to_tmp_file
  download_orginal_file_from_s3
end

#download_orginal_file_from_s3Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/gluttonberg/library/storage/s3.rb', line 178

def download_orginal_file_from_s3
  FileUtils.mkdir(self.tmp_directory) unless File.exists?(self.tmp_directory)
  bucket = bucket_handle
  key = bucket.objects[self.location_on_disk]
  File.open(self.tmp_location_on_disk, "w", encoding: "ASCII-8BIT") do |f|
    key.read do |chunk|
      f.write(chunk)
    end
  end
  key = bucket.objects[self.original_file_on_disk]
  if key.exists?
    File.open(self.tmp_original_file_on_disk, "w", encoding: "ASCII-8BIT") do |f|
      key.read do |chunk|
        f.write(chunk)
      end
    end
  end
end

#make_backupObject



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

def make_backup
  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



135
136
137
138
139
140
# File 'lib/gluttonberg/library/storage/s3.rb', line 135

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



167
168
169
170
171
172
# File 'lib/gluttonberg/library/storage/s3.rb', line 167

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



164
165
# File 'lib/gluttonberg/library/storage/s3.rb', line 164

def remove_file_from_s3(file_name)
end

#remove_file_from_storageObject



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

def remove_file_from_storage
  remove_file_from_tmp_storage
  remove_asset_folder_from_s3
end

#remove_file_from_tmp_storageObject



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

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



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

def s3_bucket_root_url
  "http://#{S3::ClassMethods.s3_server_url}/#{S3::ClassMethods.s3_bucket_name}"
end

#tmp_directoryObject

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



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

def tmp_directory
  S3::ClassMethods.storage_setup if Library.tmp_root.blank?
  Library.tmp_root + "/" + self.asset_hash
end

#update_file_on_storageObject



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/gluttonberg/library/storage/s3.rb', line 121

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
  end
end