Class: Harbor::FileStore::Mosso

Inherits:
Harbor::FileStore show all
Defined in:
lib/harbor/file_store/mosso.rb,
lib/harbor/file_store/mosso/private.rb

Direct Known Subclasses

Private

Defined Under Namespace

Classes: Private

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Harbor::FileStore

[], exists?, file_stores, get, #local?, register

Constructor Details

#initialize(username, api_key, container_name, options = {}) ⇒ Mosso

Returns a new instance of Mosso.



9
10
11
12
13
14
# File 'lib/harbor/file_store/mosso.rb', line 9

def initialize(username, api_key, container_name, options = {})
  @username = username
  @api_key = api_key
  @container_name = container_name
  @options = options
end

Instance Attribute Details

#containerObject

Returns the value of attribute container.



7
8
9
# File 'lib/harbor/file_store/mosso.rb', line 7

def container
  @container
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/harbor/file_store/mosso.rb', line 7

def options
  @options
end

Instance Method Details

#delete(filename) ⇒ Object



62
63
64
65
# File 'lib/harbor/file_store/mosso.rb', line 62

def delete(filename)
  filename = strip_leading_slash(filename)
  container.delete_object(filename)
end

#exists?(filename) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
# File 'lib/harbor/file_store/mosso.rb', line 67

def exists?(filename)
  filename = strip_leading_slash(filename)
  container.object_exists?(filename)
end

#get(path) ⇒ Object



16
17
18
19
# File 'lib/harbor/file_store/mosso.rb', line 16

def get(path)
  path = strip_leading_slash(path)
  Harbor::FileStore::File.new(self, path)
end

#open(filename, mode = "r", &block) ⇒ Object



72
73
74
75
76
77
78
79
80
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/harbor/file_store/mosso.rb', line 72

def open(filename, mode = "r", &block)
  filename = strip_leading_slash(filename)
  url = container.connection.storagehost + container.connection.storagepath + "/#{container.name}/#{filename}"
  token = container.connection.authtoken

  if mode == "r"
    command = <<-CMD
    curl -s -X "GET" \\
         -D - \\
         -H "X-Auth-Token: #{token}" \\
         https://#{url}
    CMD

    stream = IO::popen(command, "r")

    headers = []

    while line = stream.gets
      break if line == "\r\n"
      headers << line
    end
  elsif mode =~ /w/
    make_path(::File.dirname(filename))
    
    command = <<-CMD
    curl -X "PUT" \\
         -T #{"-"} \\
         -H "X-Auth-Token: #{token}" \\
         -H "Content-Type: text/plain" \\
         https://#{url}
    CMD

    stream = IO::popen(command, "w")
  end

  if block_given?
    yield stream
    stream.close
  else
    stream
  end
end

#put(filename, file) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/harbor/file_store/mosso.rb', line 21

def put(filename, file)
  filename = strip_leading_slash(filename)
  url = container.connection.storagehost + container.connection.storagepath + "/#{container.name}/#{filename}"
  token = container.connection.authtoken

  path = nil

  if file.is_a?(::File)
    path = file.path
  elsif file.is_a?(Harbor::FileStore::File) && file.store.local?
    path = file.store.path + file.path
  end
  
  make_path(::File.dirname(path))

  command = <<-CMD
  curl -X "PUT" \\
       -T #{path ? Shellwords.escape(path.to_s) : "-"} \\
       -H "X-Auth-Token: #{token}" \\
       -H "Content-Type: text/plain" \\
       https://#{url}
  CMD

  if path
    system(command)
  else
    IO::popen(command, "w") do |session|
      case file
      when ::File
        while data = file.read(500_000)
          session.write(data)
        end
      when Harbor::FileStore::File
        file.read do |block|
          session.write(block)
        end
      end
    end
  end
end

#size(filename) ⇒ Object



115
116
117
118
# File 'lib/harbor/file_store/mosso.rb', line 115

def size(filename)
  filename = strip_leading_slash(filename)
  container.object(filename).bytes.to_i
end