Skip to content

Commit 5e16084

Browse files
authored
Merge pull request #414 from appwrite/feat-resumable-upload-ruby
Feat resumable upload ruby
2 parents 22be754 + dd3588a commit 5e16084

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

templates/ruby/lib/container/client.rb.twig

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ module {{ spec.title | caseUcfirst }}
100100
headers:,
101101
params:,
102102
param_name: '',
103+
id_param_name: nil,
103104
on_progress: nil,
104105
response_type: nil
105106
)
@@ -118,11 +119,22 @@ module {{ spec.title | caseUcfirst }}
118119
)
119120
end
120121

121-
input = ::File.open(file_path)
122122
offset = 0
123+
id_param_name = id_param_name.to_sym if id_param_name
124+
if id_param_name&.empty? == false && params[id_param_name] != "unique()"
125+
# Make a request to check if a file already exists
126+
current = call(
127+
method: "GET",
128+
path: "#{path}/#{params[id_param_name]}",
129+
headers: headers,
130+
params: {}
131+
)
132+
chunks_uploaded = current['chunksUploaded'].to_i
133+
offset = [size, (chunks_uploaded * @chunk_size)].min
134+
end
123135

124136
while offset < size
125-
slice = input.read(@chunk_size)
137+
slice = IO.read(file_path, @chunk_size, offset)
126138

127139
params[param_name] = File.new(file_path, slice)
128140
headers['content-range'] = "bytes #{offset}-#{[offset + @chunk_size - 1, size].min}/#{size}"
@@ -144,8 +156,8 @@ module {{ spec.title | caseUcfirst }}
144156
id: result['$id'],
145157
progress: ([offset, size].min).to_f/size.to_f * 100.0,
146158
size_uploaded: [offset, size].min,
147-
chunks_total: result['chunks_total'],
148-
chunks_uploaded: result['chunks_uploaded']
159+
chunks_total: result['chunksTotal'],
160+
chunks_uploaded: result['chunksUploaded']
149161
}) unless on_progress.nil?
150162
end
151163

templates/ruby/lib/container/services/service.rb.twig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ module {{spec.title | caseUcfirst}}
4141
}
4242

4343
{% if 'multipart/form-data' in method.consumes %}
44+
id_param_name = {% if method.parameters.all | filter(p => p.isUploadID) | length > 0 %}{% for parameter in method.parameters.all | filter(parameter => parameter.isUploadID) %}"{{ parameter.name }}"{% endfor %}{% else %}nil{% endif %}
45+
4446
{% for parameter in method.parameters.all %}
4547
{% if parameter.type == 'file' %}
4648
param_name = '{{ parameter.name }}'
@@ -52,6 +54,7 @@ module {{spec.title | caseUcfirst}}
5254
headers: headers,
5355
params: params,
5456
param_name: param_name,
57+
id_param_name: id_param_name,
5558
on_progress: on_progress,
5659
{% if method.responseModel and method.responseModel != 'any' %}
5760
response_type: Models::{{method.responseModel | caseUcfirst}}

0 commit comments

Comments
 (0)