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 = "    curl -s -X \"GET\" \\\\\n         -D - \\\\\n         -H \"X-Auth-Token: \#{token}\" \\\\\n         https://\#{url}\n    CMD\n\n    stream = IO::popen(command, \"r\")\n\n    headers = []\n\n    while line = stream.gets\n      break if line == \"\\r\\n\"\n      headers << line\n    end\n  elsif mode =~ /w/\n    make_path(::File.dirname(filename))\n    \n    command = <<-CMD\n    curl -X \"PUT\" \\\\\n         -T \#{\"-\"} \\\\\n         -H \"X-Auth-Token: \#{token}\" \\\\\n         -H \"Content-Type: text/plain\" \\\\\n         https://\#{url}\n    CMD\n\n    stream = IO::popen(command, \"w\")\n  end\n\n  if block_given?\n    yield stream\n    stream.close\n  else\n    stream\n  end\nend\n"

#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 = "  curl -X \"PUT\" \\\\\n       -T \#{path ? Shellwords.escape(path.to_s) : \"-\"} \\\\\n       -H \"X-Auth-Token: \#{token}\" \\\\\n       -H \"Content-Type: text/plain\" \\\\\n       https://\#{url}\n  CMD\n\n  if path\n    system(command)\n  else\n    IO::popen(command, \"w\") do |session|\n      case file\n      when ::File\n        while data = file.read(500_000)\n          session.write(data)\n        end\n      when Harbor::FileStore::File\n        file.read do |block|\n          session.write(block)\n        end\n      end\n    end\n  end\nend\n"

#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