Skip to content

Commit

Permalink
resumable upload support
Browse files Browse the repository at this point in the history
  • Loading branch information
lohanidamodar committed Apr 13, 2022
1 parent 73f66c2 commit d1a7874
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion appwrite.gemspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Gem::Specification.new do |spec|

spec.name = 'appwrite'
spec.version = '4.0.0'
spec.version = '4.1.0'
spec.license = 'BSD-3-Clause'
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'
spec.author = 'Appwrite Team'
Expand Down
22 changes: 17 additions & 5 deletions lib/appwrite/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize
@chunk_size = 5*1024*1024
@headers = {
'user-agent' => RUBY_PLATFORM + ':ruby-' + RUBY_VERSION,
'x-sdk-version' => 'appwrite:ruby:4.0.0',
'x-sdk-version' => 'appwrite:ruby:4.1.0',
'X-Appwrite-Response-Format' => '0.13.0'
}
@endpoint = 'https://HOSTNAME/v1'
Expand Down Expand Up @@ -129,6 +129,7 @@ def chunked_upload(
headers:,
params:,
param_name: '',
id_param_name: nil,
on_progress: nil,
response_type: nil
)
Expand All @@ -147,11 +148,22 @@ def chunked_upload(
)
end

input = ::File.open(file_path)
offset = 0
id_param_name = id_param_name.to_sym if id_param_name
if id_param_name&.empty? == false && params[id_param_name] != "unique()"
# Make a request to check if a file already exists
current = call(
method: "GET",
path: "#{path}/#{params[id_param_name]}",
headers: headers,
params: {}
)
chunks_uploaded = current['chunksUploaded'].to_i
offset = [size, (chunks_uploaded * @chunk_size)].min
end

while offset < size
slice = input.read(@chunk_size)
slice = IO.read(file_path, @chunk_size, offset)

params[param_name] = File.new(file_path, slice)
headers['content-range'] = "bytes #{offset}-#{[offset + @chunk_size - 1, size].min}/#{size}"
Expand All @@ -173,8 +185,8 @@ def chunked_upload(
id: result['$id'],
progress: ([offset, size].min).to_f/size.to_f * 100.0,
size_uploaded: [offset, size].min,
chunks_total: result['chunks_total'],
chunks_uploaded: result['chunks_uploaded']
chunks_total: result['chunksTotal'],
chunks_uploaded: result['chunksUploaded']
}) unless on_progress.nil?
end

Expand Down
4 changes: 3 additions & 1 deletion lib/appwrite/services/functions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def create(function_id:, name:, execute:, runtime:, vars: nil, events: nil, sche
)
end

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

id_param_name = nil
param_name = 'code'

@client.chunked_upload(
path: path,
headers: headers,
params: params,
param_name: param_name,
id_param_name: id_param_name,
on_progress: on_progress,
response_type: Models::Deployment
)
Expand Down
2 changes: 2 additions & 0 deletions lib/appwrite/services/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,15 @@ def create_file(bucket_id:, file_id:, file:, read: nil, write: nil, on_progress:
"content-type": 'multipart/form-data',
}

id_param_name = "fileId"
param_name = 'file'

@client.chunked_upload(
path: path,
headers: headers,
params: params,
param_name: param_name,
id_param_name: id_param_name,
on_progress: on_progress,
response_type: Models::File
)
Expand Down

0 comments on commit d1a7874

Please sign in to comment.