Class: MogileFS::HTTPFile

Inherits:
StringIO
  • Object
show all
Includes:
Util
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

Constant Summary

Constants included from Util

Util::CHUNK_SIZE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#sysrwloop

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.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mogilefs/httpfile.rb', line 64

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

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

  @socket = nil
end

Instance Attribute Details

#bigfileObject

The bigfile name in case we have file > 256M



42
43
44
# File 'lib/mogilefs/httpfile.rb', line 42

def bigfile
  @bigfile
end

#classObject (readonly)

The class of this file.



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

def class
  @class
end

#keyObject (readonly)

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



32
33
34
# File 'lib/mogilefs/httpfile.rb', line 32

def key
  @key
end

#pathObject (readonly)

The path this file will be stored to.



26
27
28
# File 'lib/mogilefs/httpfile.rb', line 26

def path
  @path
end

Class Method Details

.open(*args) ⇒ Object

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



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mogilefs/httpfile.rb', line 48

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.



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
114
115
116
117
118
119
120
121
# File 'lib/mogilefs/httpfile.rb', line 83

def close
  connect_socket

  file_size = nil
  if @bigfile
    # Don't try to run out of memory
    fp = File.open(@bigfile)
    file_size = File.size(@bigfile)
    @socket.write "PUT #{@path.request_uri} HTTP/1.0\r\nContent-Length: #{file_size}\r\n\r\n"
    sysrwloop(fp, @socket)
    fp.close
  else
    @socket.write "PUT #{@path.request_uri} HTTP/1.0\r\nContent-Length: #{length}\r\n\r\n#{string}"
  end

  if connected? then
    line = @socket.gets
    raise 'Unable to read response line from server' if line.nil?

    if line =~ %r%^HTTP/\d+\.\d+\s+(\d+)% then
      status = Integer $1
      case status
      when 200..299 then # success!
      else
        raise "HTTP response status from upload: #{status}"
      end
    else
      raise "Response line not understood: #{line}"
    end

    @socket.close
  end

  @mg.backend.create_close(:fid => @fid, :devid => @devid,
                           :domain => @mg.domain, :key => @key,
                           :path => @path, :size => length)
  return file_size if @bigfile
  return nil
end