alipan-sdk 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e4628bf443c49974992f06af3a266a6554f631efe8affb8e96fe4a0f1211c91e
4
- data.tar.gz: d3dc3210dc35128e261c25454dfa36f5fd16384ad2d280e226520b9e03f81ac7
3
+ metadata.gz: 48a3d90f95f3aec2c203b662aadf32aadf77877a399bebe105ca1283a703afa6
4
+ data.tar.gz: 295a90bea8c0add091d0bda3ec02cfe81787932d2558c06b7afbddc6f49976e2
5
5
  SHA512:
6
- metadata.gz: 9356ddf651b4f762cbcfe0c20c70dd3050f50307975647c0d21054a1b1b101047ebbc7d27f15c03c53abaa885bb251c9ea0a204dca95ca0e00da0149fa78189c
7
- data.tar.gz: 1ee6392d663d57d12490c4ae12f007ad2343cee00bf2bb34dd729d6131ea6468b369b26c758b69a74cc060a1bde8b4d91d6fdd1fafa0480686a570d61a8f9549
6
+ metadata.gz: 6f73f95422319ff361f1a29b218208f6f1634b3febd1321b8f5cb89af3a11a486808259f59d72e89e09e3af7ab245480ad6f29ce56644aa6c8f09151bb4419cd
7
+ data.tar.gz: 2e14c6f8c68b773c8cdaab666c960f023e5e377e4c8ed8dd41852d0f05efd9de5fe69850bb8e991bfe8d5aeb5055541a01239dccaf4be811fa975f9a6f03ed1f
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
- ## [Unreleased]
1
+ ## Change Log
2
2
 
3
3
  ## [0.1.0] - 2025-06-20
4
4
 
5
5
  - Initial release
6
+
7
+ ## [0.1.3] - 2025-07-08
8
+
9
+ - 添加获取文件、上传文件方法
data/README.md CHANGED
@@ -35,9 +35,15 @@ client = Alipan::Client.new({:access_token=>"xxx"})
35
35
  ### 获取drive
36
36
  drive = client.get_drive
37
37
 
38
- ### 获取object
38
+ ### 获取文件列表
39
39
  objects = drive.list_objects
40
40
 
41
+ ### 获取文件
42
+ objects = drive.get_object(key, opts = {}, &block)
43
+
44
+ ### 上传文件
45
+ objects = drive.put_object(key, opts = {}, &block)
46
+
41
47
  ## 更多
42
48
 
43
49
  更多文档请查看:
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Alipan
4
+ class Adapter
5
+
6
+ def initialize()
7
+ @producer = Fiber.new do
8
+ yield self if block_given?
9
+ nil
10
+ end
11
+ end
12
+
13
+ def read(length = nil, outbuf = nil)
14
+ chunk = @producer.resume
15
+ outbuf.replace(chunk) if outbuf && chunk
16
+ chunk
17
+ end
18
+
19
+ def write(chunk)
20
+ Fiber.yield chunk.to_s.force_encoding(Encoding::ASCII_8BIT)
21
+ end
22
+
23
+ alias << write
24
+
25
+ def closed?
26
+ false
27
+ end
28
+
29
+ def close
30
+ end
31
+ end
32
+ end
data/lib/alipan/drive.rb CHANGED
@@ -31,5 +31,20 @@ module Alipan
31
31
 
32
32
  obj
33
33
  end
34
+
35
+ def put_object(key, opts = {}, &block)
36
+ file = opts[:file]
37
+
38
+ if file
39
+ @protocol.put_object(resource_drive_id, key, opts) do |sw|
40
+ File.open(File.expand_path(file), 'rb') do |f|
41
+ sw << f.read(Protocol::STREAM_CHUNK_SIZE) until f.eof?
42
+ end
43
+ end
44
+ else
45
+ @protocol.put_object(resource_drive_id, key, opts, &block)
46
+ end
47
+ end
48
+
34
49
  end
35
50
  end
data/lib/alipan/http.rb CHANGED
@@ -66,14 +66,11 @@ module Alipan
66
66
  :open_timeout => @config.open_timeout || OPEN_TIMEOUT,
67
67
  :read_timeout => @config.read_timeout || READ_TIMEOUT
68
68
  )
69
- response = request.execute do |resp, &blk|
70
- if resp.code >= 300
71
- e = RuntimeError.new JSON.parse(resp.body)
72
- logger.error(e.to_s)
73
- raise e
74
- else
75
- resp.return!(&blk)
76
- end
69
+ begin
70
+ response = request.execute
71
+ rescue RestClient::ExceptionWithResponse => e
72
+ response = e.response
73
+ response = RestClient::Response.create(response.body, Net::HTTPResponse.new('1.1', 200, 'OK'), request)
77
74
  end
78
75
 
79
76
  unless response.is_a?(RestClient::Response)
@@ -88,3 +85,13 @@ module Alipan
88
85
  end
89
86
  end
90
87
  end
88
+
89
+ module RestClient
90
+ module Payload
91
+ class Base
92
+ def headers
93
+ ({'Content-Length' => size.to_s} if size) || {}
94
+ end
95
+ end
96
+ end
97
+ end
@@ -6,6 +6,8 @@ module Alipan
6
6
  class Protocol
7
7
  include Common::Logging
8
8
 
9
+ STREAM_CHUNK_SIZE = 16 * 1024
10
+
9
11
  def initialize(config)
10
12
  @config = config
11
13
  @http = HTTP.new(config)
@@ -85,6 +87,10 @@ module Alipan
85
87
  r = @http.post( {:sub_res => "/adrive/v1.0/openFile/get_by_path"}, {:body => payload.to_json})
86
88
  body = JSON.parse(r.body)
87
89
 
90
+ if body.fetch(:code.to_s, '') == 'NotFound.File'
91
+ return nil
92
+ end
93
+
88
94
  obj = Object.new(
89
95
  :drive_id => body.fetch(:drive_id.to_s),
90
96
  :file_id => body.fetch(:file_id.to_s),
@@ -122,5 +128,101 @@ module Alipan
122
128
 
123
129
  obj
124
130
  end
131
+
132
+ def put_object(drive_id, object_name, opts = {}, &block)
133
+ logger.debug("Begin put object, drive_id: #{drive_id}, object: "\
134
+ "#{object_name}, options: #{opts}")
135
+
136
+ obj = nil
137
+ need_dirs = Array.new
138
+ parent_file_id = 'root'
139
+ obj_dirname = File.dirname(object_name)
140
+ dirname = "#{obj_dirname}".start_with?("/") ? "#{obj_dirname}" : "/#{obj_dirname}"
141
+
142
+ until dirname == '/'
143
+ payload = {
144
+ :drive_id => drive_id,
145
+ :file_path => dirname
146
+ }
147
+
148
+ r = @http.post( {:sub_res => "/adrive/v1.0/openFile/get_by_path"}, {:body => payload.to_json})
149
+ body = JSON.parse(r.body)
150
+
151
+ if body.fetch(:code.to_s, '') == 'NotFound.File'
152
+ need_dirs.unshift File.basename(dirname)
153
+ else
154
+ if body.fetch(:type.to_s) != 'folder'
155
+ e = RuntimeError.new "File #{dirname} has already existed!"
156
+ logger.error(e.to_s)
157
+ raise e
158
+ else
159
+ parent_file_id = body.fetch(:file_id.to_s)
160
+ break
161
+ end
162
+ end
163
+
164
+ dirname = File.dirname(dirname)
165
+ end
166
+
167
+ need_dirs.each do |need_dir|
168
+ payload = {
169
+ :drive_id => drive_id,
170
+ :parent_file_id => parent_file_id,
171
+ :name => need_dir,
172
+ :type => 'folder',
173
+ :check_name_mode => 'refuse'
174
+ }
175
+
176
+ r = @http.post( {:sub_res => "/adrive/v1.0/openFile/create"}, {:body => payload.to_json})
177
+ body = JSON.parse(r.body)
178
+
179
+ if body.fetch(:exist.to_s) == true && body.fetch(:type.to_s) != 'folder'
180
+ e = RuntimeError.new "File #{dirname} has already existed!"
181
+ logger.error(e.to_s)
182
+ raise e
183
+ end
184
+
185
+ parent_file_id = body.fetch(:file_id.to_s)
186
+ end
187
+
188
+ payload = {
189
+ :drive_id => drive_id,
190
+ :parent_file_id => parent_file_id,
191
+ :name => File.basename(object_name),
192
+ :type => 'file',
193
+ :check_name_mode => 'refuse'
194
+ }
195
+
196
+ r = @http.post( {:sub_res => "/adrive/v1.0/openFile/create"}, {:body => payload.to_json})
197
+ body = JSON.parse(r.body)
198
+
199
+ file_id = body.fetch(:file_id.to_s)
200
+ upload_id = body.fetch(:upload_id.to_s)
201
+
202
+ body.fetch('part_info_list', Array.new).each do |part|
203
+ payload = Alipan::Adapter.new(&block)
204
+
205
+ @http.put( {:sub_res => part.fetch(:upload_url.to_s)}, { :headers => { 'Content-Type' => '', 'Transfer-Encoding' => 'chunked' }, :body => payload })
206
+
207
+ payload = {
208
+ :drive_id => drive_id,
209
+ :file_id => file_id,
210
+ :upload_id => upload_id
211
+ }
212
+
213
+ r = @http.post( {:sub_res => "/adrive/v1.0/openFile/complete"}, {:body => payload.to_json})
214
+ body = JSON.parse(r.body)
215
+
216
+ obj = Object.new(
217
+ :drive_id => body.fetch(:drive_id.to_s),
218
+ :file_id => body.fetch(:file_id.to_s),
219
+ :size => body.fetch(:size.to_s),
220
+ :parent_file_id => body.fetch(:parent_file_id.to_s),
221
+ :name => body.fetch(:name.to_s))
222
+ end
223
+
224
+ obj
225
+ end
226
+
125
227
  end
126
228
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Alipan
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
data/lib/alipan.rb CHANGED
@@ -8,3 +8,4 @@ require_relative "alipan/protocol"
8
8
  require_relative "alipan/iterator"
9
9
  require_relative "alipan/drive"
10
10
  require_relative "alipan/object"
11
+ require_relative "alipan/adapter"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alipan-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - freeshenls
@@ -36,6 +36,7 @@ files:
36
36
  - README.md
37
37
  - Rakefile
38
38
  - lib/alipan.rb
39
+ - lib/alipan/adapter.rb
39
40
  - lib/alipan/client.rb
40
41
  - lib/alipan/common.rb
41
42
  - lib/alipan/common/logging.rb