Class: AssetCloud::FileSystemBucket

Inherits:
Bucket
  • Object
show all
Defined in:
lib/asset_cloud/buckets/file_system_bucket.rb

Instance Attribute Summary

Attributes inherited from Bucket

#cloud, #name

Instance Method Summary collapse

Methods inherited from Bucket

#initialize, #versioned?

Constructor Details

This class inherits a constructor from AssetCloud::Bucket

Instance Method Details

#delete(key) ⇒ Object



22
23
24
25
# File 'lib/asset_cloud/buckets/file_system_bucket.rb', line 22

def delete(key)      
  File.delete(path_for(key))
rescue Errno::ENOENT
end

#ls(key = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/asset_cloud/buckets/file_system_bucket.rb', line 5

def ls(key = nil)
  objects = []
  base_path = File.join(path_for(key), '*')

  Dir.glob(base_path).each do |f| 
    next unless File.file?(f)
    objects.push cloud[relative_path_for(f)]
  end                                  
  objects
end

#read(key) ⇒ Object



16
17
18
19
20
# File 'lib/asset_cloud/buckets/file_system_bucket.rb', line 16

def read(key)                  
  File.read(path_for(key))
rescue Errno::ENOENT => e
  raise AssetCloud::AssetNotFoundError, key
end

#stat(key) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/asset_cloud/buckets/file_system_bucket.rb', line 48

def stat(key)           
  begin 
    stat = File.stat(path_for(key))
    .new(true, stat.size, stat.ctime, stat.mtime)
  rescue Errno::ENOENT => e   
    .new(false)
  end
end

#write(key, data) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/asset_cloud/buckets/file_system_bucket.rb', line 27

def write(key, data)            
  full_path = path_for(key)   
  
  retried = false            

  begin   
    File.open(full_path, "wb+") { |fp| fp << data }      
    true
  rescue Errno::ENOENT => e 
    if retried == false 
      directory = File.dirname(full_path)
      FileUtils.mkdir_p(File.dirname(full_path))                  
      retried = true
      retry 
    else
      raise
    end 
    false
  end
end