Skip to content

Commit 63412a2

Browse files
committed
Fix ruby input file
1 parent 7cb7733 commit 63412a2

File tree

6 files changed

+51
-25
lines changed

6 files changed

+51
-25
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,15 @@ module {{ spec.title | caseUcfirst }}
109109
case input_file.source_type
110110
when 'path'
111111
size = ::File.size(input_file.path)
112-
when 'bytes'
112+
when 'string'
113113
size = input_file.data.bytesize
114114
end
115115

116116
if size < @chunk_size
117117
if input_file.source_type == 'path'
118118
input_file.data = IO.read(input_file.path)
119119
end
120-
params[param_name] = input_file
120+
params[param_name.to_sym] = input_file
121121
return call(
122122
method: 'POST',
123123
path: path,
@@ -141,16 +141,20 @@ module {{ spec.title | caseUcfirst }}
141141
offset = [size, (chunks_uploaded * @chunk_size)].min
142142
end
143143

144-
cached_input = input_file
145-
146144
while offset < size
147145
case input_file.source_type
148146
when 'path'
149-
input_file.data = IO.read(input_file.path, @chunk_size, offset)
150-
when 'bytes'
151-
input_file.data = cached_input.data.slice(offset, @chunk_size)
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)
152150
end
153-
params[param_name] = input_file
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+
)
157+
154158
headers['content-range'] = "bytes #{offset}-#{[offset + @chunk_size - 1, size].min}/#{size}"
155159

156160
result = call(

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,28 @@ module {{spec.title | caseUcfirst}}
66
attr_accessor :filename
77
attr_accessor :mime_type
88
attr_accessor :source_type
9-
109
attr_accessor :data
1110

12-
def self.fromPath(path)
11+
def self.from_path(path)
1312
instance = InputFile.new
1413
instance.path = path
1514
instance.filename = ::File.basename(path)
16-
instance.mime_type = MIME::Types.type_for(path)
15+
instance.mime_type = MIME::Types.type_for(path).first.content_type
1716
instance.source_type = 'path'
1817
instance
1918
end
2019

21-
def self.fromBytes(bytes, filename: nil, mime_type: nil)
20+
def self.from_string(string, filename: nil, mime_type: nil)
2221
instance = InputFile.new
23-
instance.data = bytes
22+
instance.data = string
2423
instance.filename = filename
2524
instance.mime_type = mime_type
26-
instance.source_type = 'bytes'
25+
instance.source_type = 'string'
2726
instance
2827
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
2932
end
3033
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: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class Ruby30Test extends Base
1919
...Base::LARGE_FILE_RESPONSES,
2020
...Base::LARGE_FILE_RESPONSES,
2121
...Base::LARGE_FILE_RESPONSES,
22-
...Base::LARGE_FILE_RESPONSES,
2322
...Base::EXCEPTION_RESPONSES
2423
];
2524
}

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: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,35 @@
4949
response = general.redirect()
5050
puts response["result"]
5151

52-
response = general.upload(x: 'string', y: 123, z:['string in array'], file: Appwrite::InputFile.fromPath('./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: Appwrite::InputFile.fromPath('./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
5765

58-
data = IO.read('./tests/resources/file.png')
59-
response = general.upload(x: 'string', y: 123, z:['string in array'], file: Appwrite::InputFile.fromBytes(data, filename:'file.png', mime_type: 'image/png'))
60-
puts response.result
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
6173

62-
data = IO.read('./tests/resources/large_file.mp4')
63-
response = general.upload(x: 'string', y: 123, z:['string in array'], file: Appwrite::InputFile.fromBytes(data, filename:'large_file.mp4', mime_type: 'video/mp4'))
64-
puts response.result
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
6581

6682
begin
6783
general.error400()

0 commit comments

Comments
 (0)