Skip to content

Commit d1a7874

Browse files
committed
resumable upload support
1 parent 73f66c2 commit d1a7874

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

appwrite.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Gem::Specification.new do |spec|
22

33
spec.name = 'appwrite'
4-
spec.version = '4.0.0'
4+
spec.version = '4.1.0'
55
spec.license = 'BSD-3-Clause'
66
spec.summary = 'Appwrite is an open-source self-hosted backend server that abstract and simplify complex and repetitive development tasks behind a very simple REST API'
77
spec.author = 'Appwrite Team'

lib/appwrite/client.rb

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def initialize
1212
@chunk_size = 5*1024*1024
1313
@headers = {
1414
'user-agent' => RUBY_PLATFORM + ':ruby-' + RUBY_VERSION,
15-
'x-sdk-version' => 'appwrite:ruby:4.0.0',
15+
'x-sdk-version' => 'appwrite:ruby:4.1.0',
1616
'X-Appwrite-Response-Format' => '0.13.0'
1717
}
1818
@endpoint = 'https://HOSTNAME/v1'
@@ -129,6 +129,7 @@ def chunked_upload(
129129
headers:,
130130
params:,
131131
param_name: '',
132+
id_param_name: nil,
132133
on_progress: nil,
133134
response_type: nil
134135
)
@@ -147,11 +148,22 @@ def chunked_upload(
147148
)
148149
end
149150

150-
input = ::File.open(file_path)
151151
offset = 0
152+
id_param_name = id_param_name.to_sym if id_param_name
153+
if id_param_name&.empty? == false && params[id_param_name] != "unique()"
154+
# Make a request to check if a file already exists
155+
current = call(
156+
method: "GET",
157+
path: "#{path}/#{params[id_param_name]}",
158+
headers: headers,
159+
params: {}
160+
)
161+
chunks_uploaded = current['chunksUploaded'].to_i
162+
offset = [size, (chunks_uploaded * @chunk_size)].min
163+
end
152164

153165
while offset < size
154-
slice = input.read(@chunk_size)
166+
slice = IO.read(file_path, @chunk_size, offset)
155167

156168
params[param_name] = File.new(file_path, slice)
157169
headers['content-range'] = "bytes #{offset}-#{[offset + @chunk_size - 1, size].min}/#{size}"
@@ -173,8 +185,8 @@ def chunked_upload(
173185
id: result['$id'],
174186
progress: ([offset, size].min).to_f/size.to_f * 100.0,
175187
size_uploaded: [offset, size].min,
176-
chunks_total: result['chunks_total'],
177-
chunks_uploaded: result['chunks_uploaded']
188+
chunks_total: result['chunksTotal'],
189+
chunks_uploaded: result['chunksUploaded']
178190
}) unless on_progress.nil?
179191
end
180192

lib/appwrite/services/functions.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def create(function_id:, name:, execute:, runtime:, vars: nil, events: nil, sche
9696
)
9797
end
9898

99-
# Get a list of all runtimes that are currently active in your project.
99+
# Get a list of all runtimes that are currently active on your instance.
100100
#
101101
#
102102
# @return [RuntimeList]
@@ -314,13 +314,15 @@ def create_deployment(function_id:, entrypoint:, code:, activate:, on_progress:
314314
"content-type": 'multipart/form-data',
315315
}
316316

317+
id_param_name = nil
317318
param_name = 'code'
318319

319320
@client.chunked_upload(
320321
path: path,
321322
headers: headers,
322323
params: params,
323324
param_name: param_name,
325+
id_param_name: id_param_name,
324326
on_progress: on_progress,
325327
response_type: Models::Deployment
326328
)

lib/appwrite/services/storage.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,15 @@ def create_file(bucket_id:, file_id:, file:, read: nil, write: nil, on_progress:
303303
"content-type": 'multipart/form-data',
304304
}
305305

306+
id_param_name = "fileId"
306307
param_name = 'file'
307308

308309
@client.chunked_upload(
309310
path: path,
310311
headers: headers,
311312
params: params,
312313
param_name: param_name,
314+
id_param_name: id_param_name,
313315
on_progress: on_progress,
314316
response_type: Models::File
315317
)

0 commit comments

Comments
 (0)