Class: Backup::Connection::Everbox::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/backup/connection/everbox.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :consumer_key => 'GW4YtvBpPwHfY0rCSf2xeOqn7tT0YH2O4zftXCOM',
  :consumer_secret => 'xlKLpZLVSe0Gk6q4w05PsDpzjEbV8SyE71exgz1i',
  :oauth_site => 'http://account.everbox.com',
  :fs_site => 'http://fs.everbox.com',
  :chunk_size => 1024*1024*4
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Session

Returns a new instance of Session.



21
22
23
24
# File 'lib/backup/connection/everbox.rb', line 21

def initialize(opts={})
  @options = DEFAULT_OPTIONS.merge(opts || {})
  @consumer = OAuth::Consumer.new @options[:consumer_key], @options[:consumer_secret], :site => 'http://account.everbox.com'
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



20
21
22
# File 'lib/backup/connection/everbox.rb', line 20

def access_token
  @access_token
end

#authorizing_secretObject

Returns the value of attribute authorizing_secret.



20
21
22
# File 'lib/backup/connection/everbox.rb', line 20

def authorizing_secret
  @authorizing_secret
end

#authorizing_tokenObject

Returns the value of attribute authorizing_token.



20
21
22
# File 'lib/backup/connection/everbox.rb', line 20

def authorizing_token
  @authorizing_token
end

Instance Method Details

#authorize!Object



26
27
28
29
30
31
# File 'lib/backup/connection/everbox.rb', line 26

def authorize!
  @access_token = OAuth::AccessToken.from_hash(@consumer, {:oauth_token => authorizing_token, :oauth_token_secret => authorizing_secret})
  raise "@access_token.token is nil" if @access_token.token.nil?
  raise "@access_token.secret is nil" if @access_token.secret.nil?
  puts @access_token.inspect
end

#authorized?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/backup/connection/everbox.rb', line 33

def authorized?
  !!@access_token
end

#delete(remote_path, opts = {}) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/backup/connection/everbox.rb', line 87

def delete(remote_path, opts={})
  remote_path = find_real_remote_path(remote_path)
  data = {
    :paths => [remote_path],
  }
  response = access_token.post(fs(:delete), data.to_json, {'Content-Type' => 'text/plain'})
  case response.code
  when "200"
    true
  else
    nil
    #raise "delete failed: #{response}"
  end
end

#path_stat(real_remote_path) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/backup/connection/everbox.rb', line 37

def path_stat(real_remote_path)
  response = access_token.post(fs(:get), JSON.dump({:path => real_remote_path}), {'Content-Type' => 'text/plain'})
  return :not_exist if response.code.to_i == 404
  info = JSON.parse(response.body)
  return :not_exist if info["type"] & 0x8000 != 0
  return :file if info["type"] & 0x1 != 0
  return :dir if info["type"] & 0x2 != 0
  raise "unknown type: #{info["type"]}"
end

#upload(filename, remote_path, opts = {}) ⇒ Object

Raises:

  • (TypeError)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/backup/connection/everbox.rb', line 47

def upload(filename, remote_path, opts={})
  raise TypeError, "filename should be string, but #{filename.class}" unless filename.is_a?(String)
  remote_path = find_real_remote_path(remote_path)
  stat = path_stat(remote_path)
  if stat == :not_exist
    puts "remote dir not exist, try to create it"
    mkdir_p(remote_path)
  elsif stat == :file
    raise "remote path is a file, failed to backup"
  end

  basename = File.basename(filename)
  target_path = File.expand_path(basename, remote_path)
  keys = calc_digests(filename)
  params = {
    :path      => target_path,
    :keys      => keys,
    :chunkSize => 4*1024*1024,
    :fileSize  => File.open(filename).stat.size,
    :base      => ''
  }
  info = JSON.parse(access_token.post(fs(:prepare_put), JSON.dump(params), {'Content-Type' => 'text/plain' }).body)
  raise "prepare_put failed:\n#{info}" unless info["required"].is_a?(Array)
  File.open(filename) do |f|
    info["required"].each do |x|
      puts "uploading block ##{x["index"]}"
      f.seek(x["index"] * @options[:chunk_size])
      code, response = http_request x['url'], f.read(@options[:chunk_size]), :method => :put
      if code != 200
        raise code.to_s
      end
    end
  end


  ftime = (Time.now.to_i * 1000 * 1000 * 10).to_s
  params = params.merge :editTime => ftime, :mimeType => 'application/octet-stream'
  code, response = access_token.post(fs(:commit_put), params.to_json, {'Content-Type' => 'text/plain'})
end