Class: Shuck::FileStore

Inherits:
Object
  • Object
show all
Defined in:
lib/shuck/file_store.rb

Constant Summary collapse

SHUCK_METADATA_DIR =
".shuck_metadataFFF"

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ FileStore

Returns a new instance of FileStore.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/shuck/file_store.rb', line 13

def initialize(root)
  @root = root
  @buckets = []
  @bucket_hash = {}
  Dir[File.join(root,"*")].each do |bucket|
    bucket_name = File.basename(bucket)
    bucket_obj = Bucket.new(bucket_name,Time.now,[])
    @buckets << bucket_obj
    @bucket_hash[bucket_name] = bucket_obj
  end
end

Instance Method Details

#bucketsObject



44
45
46
# File 'lib/shuck/file_store.rb', line 44

def buckets
  @buckets
end

#copy_object(src_bucket, src_object, dst_bucket, dst_object) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/shuck/file_store.rb', line 81

def copy_object(src_bucket,src_object,dst_bucket,dst_object)
  src_root = File.join(@root,src_bucket,src_object,SHUCK_METADATA_DIR)
  src_obj = S3Object.new
   = File.join(src_root,"metadata")
   = YAML.parse(File.open(,'rb').read)
  src_content_filename = File.join(src_root,"content")

  dst_filename= File.join(@root,dst_bucket,dst_object)
  FileUtils.mkdir_p(dst_filename)

   = File.join(dst_filename,SHUCK_METADATA_DIR)
  FileUtils.mkdir_p()

  content = File.join(,"content")
   = File.join(,"metadata")

  File.open(content,'wb') do |f|
    File.open(src_content_filename,'rb') do |input|
      f << input.read
    end
  end

  File.open(,'w') do |f|
    File.open(,'r') do |input|
      f << input.read
    end
  end

  obj = S3Object.new
  obj.md5 = [:md5]
  obj.content_type = [:content_type]
  return obj
end

#create_bucket(bucket) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/shuck/file_store.rb', line 52

def create_bucket(bucket)
  FileUtils.mkdir_p(File.join(@root,bucket))
  bucket_obj = Bucket.new(bucket,Time.now,[])
  if !@bucket_hash[bucket]
    @buckets << bucket_obj
    @bucket_hash[bucket] = bucket_obj
  end
end

#get_bucket(bucket) ⇒ Object



48
49
50
# File 'lib/shuck/file_store.rb', line 48

def get_bucket(bucket)
  @bucket_hash[bucket]
end

#get_object(bucket, object, request) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/shuck/file_store.rb', line 61

def get_object(bucket,object, request)
  begin
    real_obj = S3Object.new
    obj_root = File.join(@root,bucket,object,SHUCK_METADATA_DIR)
     = YAML.parse(File.open(File.join(obj_root,"metadata"),'rb').read)
    real_obj.name = object
    real_obj.md5 = [:md5].value
    real_obj.content_type = [:content_type] ? [:content_type].value : "application/octet-stream"
    #real_obj.io = File.open(File.join(obj_root,"content"),'rb')
    real_obj.io = RateLimitableFile.open(File.join(obj_root,"content"),'rb')
    return real_obj
  rescue
    puts $!
    return nil
  end
end

#object_metadata(bucket, object) ⇒ Object



78
79
# File 'lib/shuck/file_store.rb', line 78

def (bucket,object)
end

#rate_limit=(rate_limit) ⇒ Object

Pass a rate limit in bytes per second



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/shuck/file_store.rb', line 26

def rate_limit=(rate_limit)
  if rate_limit.is_a?(String)
    if rate_limit =~ /^(\d+)$/
      RateLimitableFile.rate_limit = rate_limit.to_i
    elsif rate_limit =~ /^(.*)K$/
      RateLimitableFile.rate_limit = $1.to_f * 1000
    elsif rate_limit =~ /^(.*)M$/
      RateLimitableFile.rate_limit = $1.to_f * 1000000
    elsif rate_limit =~ /^(.*)G$/
      RateLimitableFile.rate_limit = $1.to_f * 1000000000
    else
      raise "Invalid Rate Limit Format: Valid values include (1000,10K,1.1M)"
    end
  else
    RateLimitableFile.rate_limit = nil
  end
end

#store_object(bucket, object, request) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/shuck/file_store.rb', line 115

def store_object(bucket,object,request)
  begin
    filename = File.join(@root,bucket,object)
    FileUtils.mkdir_p(filename)

     = File.join(filename,SHUCK_METADATA_DIR)
    FileUtils.mkdir_p()

    content = File.join(filename,SHUCK_METADATA_DIR,"content")
     = File.join(filename,SHUCK_METADATA_DIR,"metadata")

    md5 = Digest::MD5.new

    File.open(content,'wb') do |f|
      request.body do |chunk|
        f << chunk
        md5 << chunk
      end
    end

     = {}
    [:md5] = md5.hexdigest
    [:content_type] = request.header["content-type"].first

    File.open(,'w') do |f|
      f << YAML::dump()
    end
    obj = S3Object.new
    obj.md5 = [:md5]
    obj.content_type = [:content_type]
    return obj
  rescue
    puts $!
    $!.backtrace.each { |line| puts line }
    return nil
  end
end