backblaze 0.2.0 → 0.2.1.pre.beta

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 58cfca96218c6f265a44d93fdeaf373a51431b7f
4
- data.tar.gz: 12856ac81e879a92feb8177a17a1d15b83df8504
3
+ metadata.gz: 1ee1b40ead4140aa561944ed21649e6f18bedd92
4
+ data.tar.gz: 3ce3761e93d95621c2f91b00314b76d31d97514b
5
5
  SHA512:
6
- metadata.gz: 02635c9ba73cbadd3610481e70bc92cd93b42a8fefa4005aaed57e3683b5c60d6a45ef641f074156a341924d515f59dab6b29fe8e25b6cc0c1f250b92dae04cb
7
- data.tar.gz: 1d1a3dcc23ada161c464dff4f93864062ffee887400403629ff7753faab660e964b288c39e164137eccf4902a9b6d7ac2bcc24b5860ba5f71901f39ae8b170e1
6
+ metadata.gz: a81b1a91b36f177ca9a9d5db6fd2ef1746dba735dc452ebc6a140f8af516967a7ec41a2cf805878c430fcae4419a6bec2d433d0df86f596eb405fae6695f3a72
7
+ data.tar.gz: ed2809cd93d183382c6b81b32f8d2b1d19025f1f8be947aac4c4f1cdbe62cebf84771f30c798235c63a324f5d588567dc77a7f761d38f5c29377c1c8fa85b2f3
@@ -27,4 +27,6 @@ Gem::Specification.new do |spec|
27
27
  spec.add_development_dependency "webmock"
28
28
 
29
29
  spec.add_dependency "httparty"
30
+
31
+ spec.required_ruby_version = '>= 2.1.0'
30
32
  end
@@ -2,6 +2,9 @@ require "backblaze/b2/base"
2
2
  require "backblaze/b2/bucket"
3
3
  require "backblaze/b2/file"
4
4
  require "backblaze/b2/file_version"
5
+ require 'net/http'
6
+ require 'tempfile'
7
+ require 'digest/sha1'
5
8
 
6
9
  module Backblaze::B2
7
10
  class << self
@@ -40,7 +40,7 @@ module Backblaze::B2
40
40
  files.compact
41
41
  end
42
42
 
43
- def file_list(limit:, retreived:, first_file:, start_field:, bucket_id:, file_name: nil)
43
+ def file_list(limit:, retreived:, first_file:, start_field:, bucket_id:, file_name: nil, first: true)
44
44
  params = {'bucketId'.freeze => bucket_id}
45
45
  if limit == -1
46
46
  params['maxFileCount'.freeze] = 1000
@@ -51,19 +51,29 @@ module Backblaze::B2
51
51
  else
52
52
  return []
53
53
  end
54
- unless first_file.nil?
54
+ if first_file.nil?
55
+ if file_name && start_field == 'startFileId' && first
56
+ params['startFileName'] = file_name
57
+ end
58
+ else
55
59
  params[start_field] = first_file
56
60
  end
57
61
 
62
+ p params
63
+
58
64
  response = post("/b2_list_file_#{start_field == 'startFileName' ? 'names' : 'versions'}", body: params.to_json)
59
65
 
66
+ raise Backblaze::FileError.new(response) unless response.code == 200
67
+
60
68
  files = response['files'.freeze]
61
69
  halt = false
62
70
  files.map! do |f|
63
71
  if halt
72
+ p f
64
73
  nil
65
74
  else
66
75
  ret = Hash[f.map{|k,v| [Backblaze::Utils.underscore(k).to_sym, v]}]
76
+ p ret
67
77
  if file_name && file_name != ret[:file_name]
68
78
  halt = true
69
79
  end
@@ -85,7 +95,8 @@ module Backblaze::B2
85
95
  limit: limit,
86
96
  retreived: retreived,
87
97
  start_field: start_field,
88
- bucket_id: bucket_id
98
+ bucket_id: bucket_id,
99
+ first: false
89
100
  )
90
101
  else
91
102
  files
@@ -105,6 +105,10 @@ module Backblaze::B2
105
105
  files
106
106
  end
107
107
 
108
+ def upload_url
109
+ self.class.upload_url(bucket_id: bucket_id)
110
+ end
111
+
108
112
  class << self
109
113
  ##
110
114
  # Create a bucket
@@ -128,6 +132,12 @@ module Backblaze::B2
128
132
  new(params)
129
133
  end
130
134
 
135
+ def upload_url(bucket_id:)
136
+ response = post('/b2_get_upload_url', body: {bucketId: bucket_id}.to_json)
137
+ raise Backblaze::BucketError.new(response) unless response.code / 100 == 2
138
+ {url: response['uploadUrl'], token: response['authorizationToken']}
139
+ end
140
+
131
141
  ##
132
142
  # List buckets for account
133
143
  # @return [Array<Backblaze::Bucket>] buckets for this account
@@ -12,8 +12,88 @@ module Backblaze::B2
12
12
  end
13
13
  end
14
14
 
15
- def [](version)
16
- versions[version]
15
+ class << self
16
+ def create(data:, bucket:, name: nil, base_name: '', content_type: 'b2/x-auto', info: {})
17
+ raise ArgumentError.new('data must not be nil') if data.nil?
18
+
19
+ case bucket
20
+ when String
21
+ upload_url = Bucket.upload_url(bucket_id: bucket)
22
+ when Bucket
23
+ upload_url = bucket.upload_url
24
+ else
25
+ raise ArgumentError.new('You must pass a bucket')
26
+ end
27
+
28
+ close_file = false
29
+ case data
30
+ when String
31
+ data.force_encoding('ASCII-8BIT')
32
+ raise ArgumentError.new('Must provide a file name for data') if name.nil?
33
+ when ::File, Tempfile
34
+ data.binmode
35
+ data.rewind
36
+ if name.nil?
37
+ raise ArgumentError.new('Must provide a file name with Tempfiles') if data.is_a? Tempfile
38
+ name = ::File.basename(data)
39
+ end
40
+ else
41
+ raise ArgumentError.new('Must provide a file name with streams') if name.nil?
42
+ if data.respond_to?(:read)
43
+ close_file = true
44
+ temp = Tempfile.new(name)
45
+ temp.binmode
46
+ IO.copy_stream(data, temp)
47
+ data = temp
48
+ data.rewind
49
+ else
50
+ raise ArgumentError.new('Unsuitable data type. Please read the docs.')
51
+ end
52
+ end
53
+
54
+ uri = URI(upload_url[:url])
55
+ req = Net::HTTP::Post.new(uri)
56
+
57
+ req.add_field("Authorization", upload_url[:token])
58
+ req.add_field("X-Bz-File-Name", "#{base_name}/#{name}".tr_s('/', '/').sub(/\A\//, ''))
59
+ req.add_field("Content-Type", content_type)
60
+ req.add_field("Content-Length", data.size)
61
+
62
+ digest = Digest::SHA1.new
63
+ if data.is_a? String
64
+ digest.update(data)
65
+ req.body = data
66
+ else
67
+ digest.file(data)
68
+ data.rewind
69
+ req.body_stream = data
70
+ end
71
+
72
+ req.add_field("X-Bz-Content-Sha1", digest)
73
+
74
+ info.first(10).map do |key, value|
75
+ req.add_field("X-Bz-Info-#{URI.encode(key)}", value)
76
+ end
77
+
78
+ http = Net::HTTP.new(req.uri.host, req.uri.port)
79
+ http.use_ssl = (req.uri.scheme == 'https')
80
+ res = http.start {|make| make.request(req)}
81
+
82
+ response = JSON.parse(res.body)
83
+
84
+ raise Backblaze::FileError.new(response) unless res.code.to_i == 200
85
+
86
+ params = {
87
+ file_name: response['fileName'],
88
+ bucket_id: response['bucketId'],
89
+ size: response['contentLength'],
90
+ file_id: response['fileId'],
91
+ upload_timestamp: Time.now.to_i * 1000,
92
+ action: 'upload'
93
+ }
94
+
95
+ File.new(params)
96
+ end
17
97
  end
18
98
 
19
99
  def file_name
@@ -29,6 +109,14 @@ module Backblaze::B2
29
109
  @versions
30
110
  end
31
111
 
112
+ def download_url(bucket:)
113
+ "#{Backblaze::B2.download_url}/file/#{bucket.is_a?(Bucket) ? bucket.name : bucket}/#{file_name}"
114
+ end
115
+
116
+ def file_id_download_url
117
+ latest.download_url
118
+ end
119
+
32
120
  def latest
33
121
  @versions.first
34
122
  end
@@ -20,6 +20,10 @@ module Backblaze::B2
20
20
  @get_info
21
21
  end
22
22
 
23
+ def download_url
24
+ "#{Backblaze::B2.download_url}#{Backblaze::B2.api_path}b2_download_file_by_id?fileId=#{file_id}"
25
+ end
26
+
23
27
  def destroy!
24
28
  response = post('/b2_delete_file_version', body: {fileName: file_name, fileId: file_id}.to_json)
25
29
  raise Backblaze::FileError.new(response) unless response.code == 200
@@ -1,3 +1,3 @@
1
1
  module Backblaze
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1-beta"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: backblaze
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1.pre.beta
5
5
  platform: ruby
6
6
  authors:
7
7
  - Winston Durand
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-21 00:00:00.000000000 Z
11
+ date: 2015-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -148,12 +148,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
148
148
  requirements:
149
149
  - - ">="
150
150
  - !ruby/object:Gem::Version
151
- version: '0'
151
+ version: 2.1.0
152
152
  required_rubygems_version: !ruby/object:Gem::Requirement
153
153
  requirements:
154
- - - ">="
154
+ - - ">"
155
155
  - !ruby/object:Gem::Version
156
- version: '0'
156
+ version: 1.3.1
157
157
  requirements: []
158
158
  rubyforge_project:
159
159
  rubygems_version: 2.4.5