Class: MogileFS::HTTPFile

Inherits:
StringIO
  • Object
show all
Defined in:
lib/mogilefs/httpfile.rb

Overview

HTTPFile wraps up the new file operations for storing files onto an HTTP storage node.

You really don’t want to create an HTTPFile by hand. Instead you want to create a new file using MogileFS::MogileFS.new_file.

WARNING! HTTP mode is completely untested as I cannot make it work on FreeBSD. Please send patches/tests if you find bugs. – TODO dup’d content in MogileFS::NFSFile

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mg, fid, path, devid, klass, key, dests, content_length) ⇒ HTTPFile

Creates a new HTTPFile with MogileFS-specific data. Use MogileFS::MogileFS#new_file instead of this method.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mogilefs/httpfile.rb', line 57

def initialize(mg, fid, path, devid, klass, key, dests, content_length)
  super ''
  @mg = mg
  @fid = fid
  @path = path
  @devid = devid
  @klass = klass
  @key = key

  @dests = dests.map { |(_,u)| URI.parse u }
  @tried = {}

  @socket = nil
end

Instance Attribute Details

#classObject (readonly)

The class of this file.



35
36
37
# File 'lib/mogilefs/httpfile.rb', line 35

def class
  @class
end

#keyObject (readonly)

The key for this file. This key won’t represent a real file until you’ve called #close.



30
31
32
# File 'lib/mogilefs/httpfile.rb', line 30

def key
  @key
end

#pathObject (readonly)

The path this file will be stored to.



24
25
26
# File 'lib/mogilefs/httpfile.rb', line 24

def path
  @path
end

Class Method Details

.open(*args) ⇒ Object

Works like File.open. Use MogileFS::MogileFS#new_file instead of this method.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mogilefs/httpfile.rb', line 41

def self.open(*args)
  fp = new(*args)

  return fp unless block_given?

  begin
    yield fp
  ensure
    fp.close
  end
end

Instance Method Details

#closeObject

Closes the file handle and marks it as closed in MogileFS.



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/mogilefs/httpfile.rb', line 75

def close
  connect_client
  
  resp = @client.put(@path.request_uri, :body => string)    
  raise "HTTP response status from upload: #{resp.http_status}" unless resp.http_status.to_i == 200
    
  @mg.backend.create_close(:fid => @fid, :devid => @devid,
                           :domain => @mg.domain, :key => @key,
                           :path => @path, :size => length)
  return nil
end