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



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

def assets_directory_public_url
  "#{s3_bucket_root_url}/user_assets"
end

#bucket_handleObject



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

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



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

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.key(self.directory + "/" + file_name, true)
    unless self.mime_type.blank?
      key.put(File.open(local_file), 'public-read', {"Expires" => date.rfc2822, "content-type" => self.mime_type})
    else
      key.put(File.open(local_file), 'public-read', {"Expires" => date.rfc2822})
    end
    self.update_column(:copied_to_s3 , true)
    puts "Copied"
  end
end

#directoryObject

The generated directory where this file is located.



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

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

#download_asset_to_tmp_fileObject



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

def download_asset_to_tmp_file
  download_orginal_file_from_s3
end

#download_orginal_file_from_s3Object



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

def download_orginal_file_from_s3
  FileUtils.mkdir(self.tmp_directory) unless File.exists?(self.tmp_directory)
  bucket = bucket_handle
  key = bucket.key(self.location_on_disk)
  File.open(self.tmp_location_on_disk, "w") do |f|
    f.write(key.get)
  end
  key = bucket.key(self.original_file_on_disk)
  if key.exists?
    File.open(self.tmp_original_file_on_disk, "w") do |f|
      f.write(key.get)
    end
  end
end

#make_backupObject



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

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



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

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



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

def remove_asset_folder_from_s3
  bucket = bucket_handle
  unless bucket.blank?
    bucket.delete_folder(self.directory)
  end
end

#remove_file_from_s3(file_name) ⇒ Object

TODO



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

def remove_file_from_s3(file_name)
end

#remove_file_from_storageObject



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

def remove_file_from_storage
  remove_file_from_tmp_storage
  remove_asset_folder_from_s3
end

#remove_file_from_tmp_storageObject



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

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



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

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.



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

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

#update_file_on_storageObject



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

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