Skip to content

Commit 41f80d6

Browse files
Merge pull request #474 from appwrite/feat-ruby-input-file
Ruby input file
2 parents 0e514b6 + eb78d61 commit 41f80d6

File tree

10 files changed

+100
-36
lines changed

10 files changed

+100
-36
lines changed

src/SDK/Language/Ruby.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ public function getFiles()
147147
],
148148
[
149149
'scope' => 'default',
150-
'destination' => 'lib/{{ spec.title | caseDash }}/file.rb',
151-
'template' => 'ruby/lib/container/file.rb.twig',
150+
'destination' => 'lib/{{ spec.title | caseDash }}/input_file.rb',
151+
'template' => 'ruby/lib/container/input_file.rb.twig',
152152
'minify' => false,
153153
],
154154
[
@@ -281,7 +281,7 @@ public function getParamExample(array $param)
281281
$output .= '{}';
282282
break;
283283
case self::TYPE_FILE:
284-
$output .= "'dir/file.png'";
284+
$output .= "InputFile.fromPath('dir/file.png')";
285285
break;
286286
}
287287
}
@@ -302,7 +302,7 @@ public function getParamExample(array $param)
302302
$output .= "'{$example}'";
303303
break;
304304
case self::TYPE_FILE:
305-
$output .= "'file.png'";
305+
$output .= "InputFile.fromPath('dir/file.png')";
306306
break;
307307
}
308308
}

templates/ruby/docs/example.md.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ client
1212

1313
{{ service.name | caseSnake }} = {{spec.title | caseUcfirst}}::{{ service.name | caseUcfirst }}.new(client{% if service.globalParams | length %}{% for parameter in service.globalParams %}, {{ parameter | paramExample }}{% endfor %}{% endif %})
1414

15-
response = {{ service.name | caseSnake }}.{{ method.name | caseSnake }}({% for parameter in method.parameters.all | filter((param) => not param.isGlobal) %}{% if parameter.required %}{% if not loop.first %}, {% endif %}{{parameter.name|caseSnake}}: {% if parameter | paramExample == "File.new" %}{{spec.title | caseUcfirst}}::{{ parameter | paramExample }}{% else %}{{ parameter | paramExample }}{% endif %}{% endif %}{% endfor %})
15+
response = {{ service.name | caseSnake }}.{{ method.name | caseSnake }}({% for parameter in method.parameters.all | filter((param) => not param.isGlobal) %}{% if parameter.required %}{% if not loop.first %}, {% endif %}{{parameter.name|caseSnake}}: {% if parameter | paramExample == "InputFile.new" %}{{spec.title | caseUcfirst}}::{{ parameter | paramExample }}{% else %}{{ parameter | paramExample }}{% endif %}{% endif %}{% endfor %})
1616

1717
puts response.inspect

templates/ruby/lib/container.rb.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require 'mime/types'
66
require_relative '{{ spec.title | caseSnake }}/client'
77
require_relative '{{ spec.title | caseSnake }}/service'
88
require_relative '{{ spec.title | caseSnake }}/exception'
9-
require_relative '{{ spec.title | caseSnake }}/file'
9+
require_relative '{{ spec.title | caseSnake }}/input_file'
1010

1111
{% for defintion in spec.definitions %}
1212
require_relative '{{ spec.title | caseSnake }}/models/{{ defintion.name | caseSnake }}'

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,20 @@ module {{ spec.title | caseUcfirst }}
104104
on_progress: nil,
105105
response_type: nil
106106
)
107-
file_path = params[param_name.to_sym]
108-
size = ::File.size(file_path)
107+
input_file = params[param_name.to_sym]
108+
109+
case input_file.source_type
110+
when 'path'
111+
size = ::File.size(input_file.path)
112+
when 'string'
113+
size = input_file.data.bytesize
114+
end
109115

110116
if size < @chunk_size
111-
slice = ::File.read(file_path)
112-
params[param_name] = File.new(file_path, slice)
117+
if input_file.source_type == 'path'
118+
input_file.data = IO.read(input_file.path)
119+
end
120+
params[param_name.to_sym] = input_file
113121
return call(
114122
method: 'POST',
115123
path: path,
@@ -134,9 +142,19 @@ module {{ spec.title | caseUcfirst }}
134142
end
135143

136144
while offset < size
137-
slice = IO.read(file_path, @chunk_size, offset)
145+
case input_file.source_type
146+
when 'path'
147+
string = IO.read(input_file.path, @chunk_size, offset)
148+
when 'string'
149+
string = input_file.data.byteslice(offset, [@chunk_size, size - offset].min)
150+
end
151+
152+
params[param_name.to_sym] = InputFile::from_string(
153+
string,
154+
filename: input_file.filename,
155+
mime_type: input_file.mime_type
156+
)
138157

139-
params[param_name] = File.new(file_path, slice)
140158
headers['content-range'] = "bytes #{offset}-#{[offset + @chunk_size - 1, size].min}/#{size}"
141159

142160
result = call(
@@ -250,10 +268,10 @@ module {{ spec.title | caseUcfirst }}
250268
''
251269
else
252270
post_body = []
253-
if value.instance_of? File
254-
post_body << "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{value.name}\"\r\n"
271+
if value.instance_of? InputFile
272+
post_body << "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{value.filename}\"\r\n"
255273
post_body << "Content-Type: #{value.mime_type}\r\n\r\n"
256-
post_body << value.content
274+
post_body << value.data
257275
post_body << "\r\n--#{@boundary}--\r\n"
258276
else
259277
post_body << "Content-Disposition: form-data; name=\"#{key}\"\r\n\r\n"

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

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require 'mime/types'
2+
3+
module {{spec.title | caseUcfirst}}
4+
class InputFile
5+
attr_accessor :path
6+
attr_accessor :filename
7+
attr_accessor :mime_type
8+
attr_accessor :source_type
9+
attr_accessor :data
10+
11+
def self.from_path(path)
12+
instance = InputFile.new
13+
instance.path = path
14+
instance.filename = ::File.basename(path)
15+
instance.mime_type = MIME::Types.type_for(path).first.content_type
16+
instance.source_type = 'path'
17+
instance
18+
end
19+
20+
def self.from_string(string, filename: nil, mime_type: nil)
21+
instance = InputFile.new
22+
instance.data = string
23+
instance.filename = filename
24+
instance.mime_type = mime_type
25+
instance.source_type = 'string'
26+
instance
27+
end
28+
29+
def self.from_bytes(bytes, filename: nil, mime_type: nil)
30+
self.from_string(bytes.pack('C*'), filename: filename, mime_type: mime_type)
31+
end
32+
end
33+
end

tests/Ruby27Test.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class Ruby27Test extends Base
1717
...Base::BAR_RESPONSES,
1818
...Base::GENERAL_RESPONSES,
1919
...Base::LARGE_FILE_RESPONSES,
20+
...Base::LARGE_FILE_RESPONSES,
21+
...Base::LARGE_FILE_RESPONSES,
2022
...Base::EXCEPTION_RESPONSES
2123
];
2224
}

tests/Ruby30Test.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class Ruby30Test extends Base
1717
...Base::BAR_RESPONSES,
1818
...Base::GENERAL_RESPONSES,
1919
...Base::LARGE_FILE_RESPONSES,
20+
...Base::LARGE_FILE_RESPONSES,
21+
...Base::LARGE_FILE_RESPONSES,
2022
...Base::EXCEPTION_RESPONSES
2123
];
2224
}

tests/Ruby31Test.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class Ruby31Test extends Base
1717
...Base::BAR_RESPONSES,
1818
...Base::GENERAL_RESPONSES,
1919
...Base::LARGE_FILE_RESPONSES,
20+
...Base::LARGE_FILE_RESPONSES,
21+
...Base::LARGE_FILE_RESPONSES,
2022
...Base::EXCEPTION_RESPONSES
2123
];
2224
}

tests/languages/ruby/tests.rb

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,35 @@
4949
response = general.redirect()
5050
puts response["result"]
5151

52-
response = general.upload(x: 'string', y: 123, z:['string in array'], file: './tests/resources/file.png')
53-
puts response.result
52+
begin
53+
response = general.upload(x: 'string', y: 123, z:['string in array'], file: Appwrite::InputFile.from_path('./tests/resources/file.png'))
54+
puts response.result
55+
rescue => e
56+
puts e
57+
end
5458

55-
response = general.upload(x: 'string', y: 123, z:['string in array'], file: './tests/resources/large_file.mp4')
56-
puts response.result
59+
begin
60+
response = general.upload(x: 'string', y: 123, z:['string in array'], file: Appwrite::InputFile.from_path('./tests/resources/large_file.mp4'))
61+
puts response.result
62+
rescue => e
63+
puts e
64+
end
65+
66+
begin
67+
string = IO.read('./tests/resources/file.png')
68+
response = general.upload(x: 'string', y: 123, z:['string in array'], file: Appwrite::InputFile.from_string(string, filename:'file.png', mime_type: 'image/png'))
69+
puts response.result
70+
rescue => e
71+
puts e
72+
end
73+
74+
begin
75+
string = IO.read('./tests/resources/large_file.mp4')
76+
response = general.upload(x: 'string', y: 123, z:['string in array'], file: Appwrite::InputFile.from_string(string, filename:'large_file.mp4', mime_type: 'video/mp4'))
77+
puts response.result
78+
rescue => e
79+
puts e
80+
end
5781

5882
begin
5983
general.error400()

0 commit comments

Comments
 (0)