Class: ErpTechSvcs::FileSupport::S3Manager

Inherits:
Manager
  • Object
show all
Defined in:
lib/erp_tech_svcs/file_support/s3_manager.rb

Constant Summary

Constants inherited from Manager

Manager::FILE_DOES_NOT_EXIST, Manager::FILE_FOLDER_DOES_NOT_EXIST, Manager::FOLDER_IS_NOT_EMPTY

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Manager

#build_file_assets_tree_for_model, find_parent, #insert_folders, #sync

Class Method Details

.setup_connectionObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/erp_tech_svcs/file_support/s3_manager.rb', line 10

def setup_connection
  @@configuration = YAML::load_file(File.join(Rails.root, 'config', 's3.yml'))[Rails.env]

  # S3 debug logging
  AWS.config(
      :logger => Rails.logger,
      :log_level => :info
  )

  @@s3_connection = AWS::S3.new(
      :access_key_id => @@configuration['access_key_id'],
      :secret_access_key => @@configuration['secret_access_key']
  )

  @@s3_bucket = @@s3_connection.buckets[@@configuration['bucket'].to_sym]
end

Instance Method Details

#bucketObject



37
38
39
# File 'lib/erp_tech_svcs/file_support/s3_manager.rb', line 37

def bucket
  @@s3_bucket
end

#bucket=(name) ⇒ Object



33
34
35
# File 'lib/erp_tech_svcs/file_support/s3_manager.rb', line 33

def bucket=(name)
  @@s3_bucket = @@s3_connection.buckets[name.to_sym]
end

#bucketsObject



29
30
31
# File 'lib/erp_tech_svcs/file_support/s3_manager.rb', line 29

def buckets
  @@s3_connection.buckets
end

#build_tree(starting_path, options = {}) ⇒ Object



171
172
173
174
# File 'lib/erp_tech_svcs/file_support/s3_manager.rb', line 171

def build_tree(starting_path, options={})
  node_tree = find_node(starting_path, options)
  node_tree.nil? ? [] : node_tree
end

#copy_file(origination_file, path, name) ⇒ Object

copy a file



60
61
62
63
64
# File 'lib/erp_tech_svcs/file_support/s3_manager.rb', line 60

def copy_file(origination_file, path, name)
  contents = get_contents(origination_file)

  create_file(path, name, contents)
end

#create_file(path, name, content) ⇒ Object



53
54
55
56
57
# File 'lib/erp_tech_svcs/file_support/s3_manager.rb', line 53

def create_file(path, name, content)
  path = path.sub(%r{^/}, '')
  full_filename = (path.blank? ? name : File.join(path, name))
  bucket.objects[full_filename].write(content, {:acl => :public_read})
end

#create_folder(path, name) ⇒ Object



66
67
68
69
70
71
# File 'lib/erp_tech_svcs/file_support/s3_manager.rb', line 66

def create_folder(path, name)
  path = path.sub(%r{^/}, '')
  full_filename = (path.blank? ? name : File.join(path, name))
  folder = full_filename + "/"
  bucket.objects[folder].write('', {:acl => :public_read})
end

#delete_file(path, options = {}) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/erp_tech_svcs/file_support/s3_manager.rb', line 129

def delete_file(path, options={})
  is_directory = File.extname(path).blank?
  path = path.sub(%r{^/}, '')
  result = false
  message = nil
  begin
    bucket.objects.with_prefix(path).delete_all
    message = "File was deleted successfully"
    result = true
  rescue => ex
    result = false
    message = ex
  end

  return result, message, is_directory
end

#exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
149
150
151
152
153
# File 'lib/erp_tech_svcs/file_support/s3_manager.rb', line 146

def exists?(path)
  begin
    path = path.sub(%r{^/}, '')
    return bucket.objects[path].exists?
  rescue AWS::S3::Errors::NoSuchKey
    return false
  end
end

#find_node(path, options = {}) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/erp_tech_svcs/file_support/s3_manager.rb', line 176

def find_node(path, options={})
  parent = {
      :text => path.split('/').pop,
      :iconCls => "icon-content",
      :leaf => false,
      :id => path,
      :children => []
  }

  #remove proceeding slash for s3
  path.sub!(%r{^/}, '')

  if File.extname(path).blank?
    tree = bucket.as_tree(:prefix => path)

    tree.children.each do |node|
      if node.leaf?
        #ignore current path that comes as leaf from s3
        next if node.key == path + '/'

        leaf_hash = {
            :text => node.key.split('/').pop,
            :downloadPath => "/#{node.key.split('/')[0..-2].join('/')}",
            :id => "/#{node.key}",
            :iconCls => 'icon-document',
            :leaf => true
        }

        if options[:file_asset_holder]
          leaf_hash = apply_file_asset_properties(options[:file_asset_holder], leaf_hash)
        end

        parent[:children] << leaf_hash
      else
        parent[:children] << {
            :iconCls => "icon-content",
            :text => node.prefix.split('/').pop,
            :id => "/#{node.prefix}".chop,
            :leaf => false
        }
      end
    end

  else
    parent[:iconCls] = 'icon-document'
    parent[:leaf] = true
    parent[:downloadPath] = "/#{path.split('/')[0..-2].join('/')}"

    if options[:file_asset_holder]
      parent = apply_file_asset_properties(options[:file_asset_holder], parent)
    end
  end

  # add preceding slash back to parent
  parent[:id] = "/#{parent[:id]}"

  parent
end

#get_contents(path) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/erp_tech_svcs/file_support/s3_manager.rb', line 155

def get_contents(path)
  contents = nil
  message = nil

  path = path.sub(%r{^/}, '')
  begin
    object = bucket.objects[path]
    contents = object.read
  rescue AWS::S3::Errors::NoSuchKey => error
    contents = ''
    message = FILE_DOES_NOT_EXIST
  end

  return contents, message
end

#rename_file(path, name) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/erp_tech_svcs/file_support/s3_manager.rb', line 96

def rename_file(path, name)
  result = false
  old_name = File.basename(path)
  path_pieces = path.split('/')
  path_pieces.delete(path_pieces.last)
  path_pieces.push(name)
  new_path = path_pieces.join('/')
  begin
    file = FileAsset.where(:name => ::File.basename(path)).where(:directory => ::File.dirname(path)).first
    acl = (file.is_secured? ? :private : :public_read) unless file.nil?
    options = (file.nil? ? {} : {:acl => acl})
    path = path.sub(%r{^/}, '')
    new_path = new_path.sub(%r{^/}, '')

    old_object = bucket.objects[path]
    if new_object = old_object.move_to(new_path, options)
      message = "#{old_name} was renamed to #{name} successfully"
      result = true
    else
      message = "Error renaming #{old_name}"
    end
  rescue AWS::S3::Errors::NoSuchKey => ex
    message = FILE_FOLDER_DOES_NOT_EXIST
  end

  return result, message
end

#rootObject



41
42
43
# File 'lib/erp_tech_svcs/file_support/s3_manager.rb', line 41

def root
  ''
end

#save_move(path, new_parent_path) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/erp_tech_svcs/file_support/s3_manager.rb', line 73

def save_move(path, new_parent_path)
  result = false
  unless self.exists? path
    message = FILE_DOES_NOT_EXIST
  else
    file = FileAsset.where(:name => ::File.basename(path)).where(:directory => ::File.dirname(path)).first
    acl = (file.is_secured? ? :private : :public_read) unless file.nil?
    options = (file.nil? ? {} : {:acl => acl})
    name = File.basename(path)
    path = path.sub(%r{^/}, '')
    new_path = File.join(new_parent_path, name).sub(%r{^/}, '')
    old_object = bucket.objects[path]
    if new_object = old_object.move_to(new_path, options)
      message = "#{name} was moved to #{new_path} successfully"
      result = true
    else
      message = "Error moving file #{path}"
    end
  end

  return result, message
end

#set_permissions(path, canned_acl = :public_read) ⇒ Object



124
125
126
127
# File 'lib/erp_tech_svcs/file_support/s3_manager.rb', line 124

def set_permissions(path, canned_acl=:public_read)
  path = path.sub(%r{^/}, '')
  bucket.objects[path].acl = canned_acl
end

#update_file(path, content) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/erp_tech_svcs/file_support/s3_manager.rb', line 45

def update_file(path, content)
  file = FileAsset.where(:name => ::File.basename(path)).where(:directory => ::File.dirname(path)).first
  acl = (file.is_secured? ? :private : :public_read) unless file.nil?
  options = (file.nil? ? {} : {:acl => acl, :content_type => file.content_type})
  path = path.sub(%r{^/}, '')
  bucket.objects[path].write(content, options)
end