@@ -15,16 +15,17 @@ def initialize
1515 'x-sdk-name' => 'Ruby' ,
1616 'x-sdk-platform' => 'server' ,
1717 'x-sdk-language' => 'ruby' ,
18- 'x-sdk-version' => '13.0.0' ,
19- 'x-appwrite-response-format ' => '1.6.0'
18+ 'x-sdk-version' => '13.0.0' ,
19+ 'X-Appwrite-Response-Format ' => '1.6.0'
2020 }
2121 @endpoint = 'https://cloud.appwrite.io/v1'
2222 end
2323
2424 # Set Project
2525 #
2626 # Your project ID
27- # # @param [String] value The value to set for the Project header
27+ #
28+ # @param [String] value The value to set for the Project header
2829 #
2930 # @return [self]
3031 def set_project ( value )
@@ -36,7 +37,8 @@ def set_project(value)
3637 # Set Key
3738 #
3839 # Your secret API key
39- # # @param [String] value The value to set for the Key header
40+ #
41+ # @param [String] value The value to set for the Key header
4042 #
4143 # @return [self]
4244 def set_key ( value )
@@ -48,7 +50,8 @@ def set_key(value)
4850 # Set JWT
4951 #
5052 # Your secret JSON Web Token
51- # # @param [String] value The value to set for the JWT header
53+ #
54+ # @param [String] value The value to set for the JWT header
5255 #
5356 # @return [self]
5457 def set_jwt ( value )
@@ -71,7 +74,8 @@ def set_locale(value)
7174 # Set Session
7275 #
7376 # The user session to authenticate with
74- # # @param [String] value The value to set for the Session header
77+ #
78+ # @param [String] value The value to set for the Session header
7579 #
7680 # @return [self]
7781 def set_session ( value )
@@ -83,7 +87,8 @@ def set_session(value)
8387 # Set ForwardedUserAgent
8488 #
8589 # The user agent string of the client that made the request
86- # # @param [String] value The value to set for the ForwardedUserAgent header
90+ #
91+ # @param [String] value The value to set for the ForwardedUserAgent header
8792 #
8893 # @return [self]
8994 def set_forwarded_user_agent ( value )
@@ -114,6 +119,7 @@ def set_self_signed(self_signed = true)
114119 self
115120 end
116121
122+
117123 # Add Header
118124 #
119125 # @param [String] key The key for the header to add
@@ -156,9 +162,20 @@ def chunked_upload(
156162 on_progress : nil ,
157163 response_type : nil
158164 )
159- payload = params [ param_name . to_sym ]
165+ input_file = params [ param_name . to_sym ]
166+
167+ case input_file . source_type
168+ when 'path'
169+ size = ::File . size ( input_file . path )
170+ when 'string'
171+ size = input_file . data . bytesize
172+ end
160173
161- if payload . size < @chunk_size
174+ if size < @chunk_size
175+ if input_file . source_type == 'path'
176+ input_file . data = IO . read ( input_file . path )
177+ end
178+ params [ param_name . to_sym ] = input_file
162179 return call (
163180 method : 'POST' ,
164181 path : path ,
@@ -182,13 +199,21 @@ def chunked_upload(
182199 offset = chunks_uploaded * @chunk_size
183200 end
184201
185- while offset < payload . size
186- params [ param_name . to_sym ] = Payload . from_binary (
187- payload . to_binary ( offset , [ @chunk_size , payload . size - offset ] . min ) ,
188- filename : payload . filename
202+ while offset < size
203+ case input_file . source_type
204+ when 'path'
205+ string = IO . read ( input_file . path , @chunk_size , offset )
206+ when 'string'
207+ string = input_file . data . byteslice ( offset , [ @chunk_size , size - offset ] . min )
208+ end
209+
210+ params [ param_name . to_sym ] = InputFile ::from_string (
211+ string ,
212+ filename : input_file . filename ,
213+ mime_type : input_file . mime_type
189214 )
190215
191- headers [ 'content-range' ] = "bytes #{ offset } -#{ [ offset + @chunk_size - 1 , payload . size - 1 ] . min } /#{ payload . size } "
216+ headers [ 'content-range' ] = "bytes #{ offset } -#{ [ offset + @chunk_size - 1 , size - 1 ] . min } /#{ size } "
192217
193218 result = call (
194219 method : 'POST' ,
@@ -205,8 +230,8 @@ def chunked_upload(
205230
206231 on_progress . call ( {
207232 id : result [ '$id' ] ,
208- progress : ( [ offset , payload . size ] . min ) . to_f /payload . size . to_f * 100.0 ,
209- size_uploaded : [ offset , payload . size ] . min ,
233+ progress : ( [ offset , size ] . min ) . to_f /size . to_f * 100.0 ,
234+ size_uploaded : [ offset , size ] . min ,
210235 chunks_total : result [ 'chunksTotal' ] ,
211236 chunks_uploaded : result [ 'chunksUploaded' ]
212237 } ) unless on_progress . nil?
@@ -233,27 +258,18 @@ def fetch(
233258 @http . use_ssl = !@self_signed
234259 payload = ''
235260
236- headers = @headers . merge ( headers . transform_keys ( & :to_s ) )
261+ headers = @headers . merge ( headers )
237262
238263 params . compact!
239264
265+ @boundary = "----A30#3ad1"
240266 if method != "GET"
241- case headers [ 'content-type' ]
267+ case headers [ : 'content-type']
242268 when 'application/json'
243269 payload = params . to_json
244270 when 'multipart/form-data'
245- multipart = MultipartBuilder . new ( )
246-
247- params . each do |name , value |
248- if value . is_a? ( Payload )
249- multipart . add ( name , value . to_s , filename : value . filename )
250- else
251- multipart . add ( name , value )
252- end
253- end
254-
255- headers [ 'content-type' ] = multipart . content_type
256- payload = multipart . body
271+ payload = encode_form_data ( params ) + "--#{ @boundary } --\r \n "
272+ headers [ :'content-type' ] = "multipart/form-data; boundary=#{ @boundary } "
257273 else
258274 payload = encode ( params )
259275 end
@@ -283,7 +299,7 @@ def fetch(
283299 return fetch ( method , uri , headers , { } , response_type , limit - 1 )
284300 end
285301
286- if response . content_type . start_with? ( 'application/json' )
302+ if response . content_type == 'application/json'
287303 begin
288304 result = JSON . parse ( response . body )
289305 rescue JSON ::ParserError => e
@@ -294,34 +310,49 @@ def fetch(
294310 raise Appwrite ::Exception . new ( result [ 'message' ] , result [ 'status' ] , result [ 'type' ] , result )
295311 end
296312
297- if response_type . respond_to? ( "from" )
298- return response_type . from ( map : result )
313+ unless response_type . respond_to? ( "from" )
314+ return result
299315 end
300316
301- return result
317+ return response_type . from ( map : result )
302318 end
303319
304320 if response . code . to_i >= 400
305321 raise Appwrite ::Exception . new ( response . body , response . code , response )
306322 end
307323
308- if response . content_type . start_with? ( 'multipart/form-data' )
309- multipart = MultipartParser . new ( response . body , response [ 'content-type' ] )
310- result = multipart . to_hash
311-
312- if response_type . respond_to? ( "from" )
313- return response_type . from ( map : result )
314- end
315-
316- return result
317- end
318-
319- if response . class . body_permitted?
320- return response . body
324+ if response . respond_to? ( "body_permitted?" )
325+ return response . body if response . body_permitted?
321326 end
322327
323328 return response
324329 end
330+
331+ def encode_form_data ( value , key = nil )
332+ case value
333+ when Hash
334+ value . map { |k , v | encode_form_data ( v , k ) } . join
335+ when Array
336+ value . map { |v | encode_form_data ( v , "#{ key } []" ) } . join
337+ when nil
338+ ''
339+ else
340+ post_body = [ ]
341+ if value . instance_of? InputFile
342+ post_body << "--#{ @boundary } "
343+ post_body << "Content-Disposition: form-data; name=\" #{ key } \" ; filename=\" #{ value . filename } \" "
344+ post_body << "Content-Type: #{ value . mime_type } "
345+ post_body << ""
346+ post_body << value . data
347+ else
348+ post_body << "--#{ @boundary } "
349+ post_body << "Content-Disposition: form-data; name=\" #{ key } \" "
350+ post_body << ""
351+ post_body << value . to_s
352+ end
353+ post_body . join ( "\r \n " ) + "\r \n "
354+ end
355+ end
325356
326357 def encode ( value , key = nil )
327358 case value
0 commit comments