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



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

def assets_directory_public_url
  "#{s3_bucket_root_url}/user_assets"
end

#bucket_handleObject



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

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



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

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.



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

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

#download_asset_to_tmp_fileObject



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

def download_asset_to_tmp_file
  download_orginal_file_from_s3
end

#download_orginal_file_from_s3Object



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

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



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

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



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

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



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

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



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

def remove_file_from_s3(file_name)
end

#remove_file_from_storageObject



111
112
113
114
# File 'lib/gluttonberg/library/storage/s3.rb', line 111

def remove_file_from_storage
  remove_file_from_tmp_storage
  remove_asset_folder_from_s3
end

#remove_file_from_tmp_storageObject



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

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



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

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.



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

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

#update_file_on_storageObject



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

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