Class: Thumbo::Mogilefs

Inherits:
AbstractStorage show all
Defined in:
lib/thumbo/storages/mogilefs.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Mogilefs

Returns a new instance of Mogilefs.



14
15
16
17
18
19
# File 'lib/thumbo/storages/mogilefs.rb', line 14

def initialize opts = {}
  @klass  = opts[:klass]  || 'thumbo'
  @domain = opts[:domain] || 'thumbo'
  @hosts  = opts[:hosts]  || ['127.0.0.1:6001']
  @timeout_time = opts[:timeout_time] || 2
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



13
14
15
# File 'lib/thumbo/storages/mogilefs.rb', line 13

def client
  @client
end

#domainObject

Returns the value of attribute domain.



12
13
14
# File 'lib/thumbo/storages/mogilefs.rb', line 12

def domain
  @domain
end

#hostsObject

Returns the value of attribute hosts.



12
13
14
# File 'lib/thumbo/storages/mogilefs.rb', line 12

def hosts
  @hosts
end

#klassObject

Returns the value of attribute klass.



12
13
14
# File 'lib/thumbo/storages/mogilefs.rb', line 12

def klass
  @klass
end

#timeout_timeObject

Returns the value of attribute timeout_time.



12
13
14
# File 'lib/thumbo/storages/mogilefs.rb', line 12

def timeout_time
  @timeout_time
end

Instance Method Details

#delete(filename) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/thumbo/storages/mogilefs.rb', line 41

def delete filename
  client.delete(filename)

rescue MogileFS::Backend::UnknownKeyError
  raise_file_not_found(filename)

end

#exist?(filename) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
# File 'lib/thumbo/storages/mogilefs.rb', line 57

def exist? filename
  target = paths(filename)
  target[ rand(target.size) ]

rescue Thumbo::FileNotFound
  false
end

#paths(filename) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/thumbo/storages/mogilefs.rb', line 49

def paths filename
  client.get_paths(filename)

rescue MogileFS::Backend::UnknownKeyError
  raise_file_not_found(filename)

end

#read(filename) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/thumbo/storages/mogilefs.rb', line 21

def read filename
  Timeout.timeout(timeout_time){
    paths(filename).each{ |path|
      begin
        return client.get_file_data(filename)
      rescue SystemCallError
        next
      end
    }
  }
end

#write(filename, blob) ⇒ Object



33
34
35
# File 'lib/thumbo/storages/mogilefs.rb', line 33

def write filename, blob
  client.store_content(filename, klass, blob)
end

#write_file(filename, file) ⇒ Object



37
38
39
# File 'lib/thumbo/storages/mogilefs.rb', line 37

def write_file filename, file
  client.store_file(filename, klass, file)
end