Skip to content

Commit 53481c8

Browse files
Merge pull request #473 from appwrite/feat-python-input-file
Update python input file
2 parents 6b14c1f + 1db5dee commit 53481c8

File tree

7 files changed

+61
-17
lines changed

7 files changed

+61
-17
lines changed

src/SDK/Language/Python.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ public function getParamExample(array $param)
302302
$output .= '{}';
303303
break;
304304
case self::TYPE_FILE:
305-
$output .= "'file.png'";
305+
$output .= "InputFile.from_path('file.png')";
306306
break;
307307
}
308308
}
@@ -321,7 +321,7 @@ public function getParamExample(array $param)
321321
$output .= "'{$example}'";
322322
break;
323323
case self::TYPE_FILE:
324-
$output .= "'file.png'";
324+
$output .= "InputFile.from_path('file.png')";
325325
break;
326326
}
327327
}

templates/python/package/client.py.twig

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class Client:
6666
stringify = True
6767
for key in data.copy():
6868
if isinstance(data[key], InputFile):
69-
files[key] = (data[key].name, data[key].file)
69+
files[key] = (data[key].filename, data[key].data)
7070
del data[key]
7171
response = None
7272
try:
@@ -108,21 +108,27 @@ class Client:
108108
on_progress = None,
109109
upload_id = ''
110110
):
111-
file_path = str(params[param_name])
112-
file_name = os.path.basename(file_path)
113-
size = os.stat(file_path).st_size
111+
input_file = params[param_name]
112+
113+
if input_file.source_type == 'path':
114+
size = os.stat(input_file.path).st_size
115+
input = open(input_file.path, 'rb')
116+
elif input_file.source_type == 'bytes':
117+
size = len(input_file.data)
118+
input = input_file.data
114119

115120
if size < self._chunk_size:
116-
slice = open(file_path, 'rb').read()
117-
params[param_name] = InputFile(file_path, file_name, slice)
121+
if input_file.source_type == 'path':
122+
input_file.data = input.read()
123+
124+
params[param_name] = input_file
118125
return self.call(
119126
'post',
120127
path,
121128
headers,
122129
params
123130
)
124131

125-
input = open(file_path, 'rb')
126132
offset = 0
127133
counter = 0
128134

@@ -138,9 +144,16 @@ class Client:
138144
input.seek(offset)
139145

140146
while offset < size:
141-
slice = input.read(self._chunk_size) or input.read(size - offset)
147+
if input_file.source_type == 'path':
148+
input_file.data = input.read(self._chunk_size) or input.read(size - offset)
149+
elif input_file.source_type == 'bytes':
150+
if offset + self._chunk_size < size:
151+
end = offset + self._chunk_size
152+
else:
153+
end = size - offset
154+
input_file.data = input[offset:end]
142155

143-
params[param_name] = InputFile(file_path, file_name, slice)
156+
params[param_name] = input_file
144157
headers["content-range"] = f'bytes {offset}-{min((offset + self._chunk_size) - 1, size)}/{size}'
145158

146159
result = self.call(
Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
1+
import os
2+
import mimetypes
3+
14
class InputFile:
2-
def __init__(self, path, name, file):
3-
self.path = path
4-
self.name = name
5-
self.file = file
5+
@classmethod
6+
def from_path(cls, path):
7+
instance = cls()
8+
instance.path = path
9+
instance.filename = os.path.basename(path)
10+
instance.mime_type = mimetypes.guess_type(path)
11+
instance.source_type = 'path'
12+
return instance
13+
14+
@classmethod
15+
def from_bytes(cls, bytes, filename = None, mime_type = None):
16+
instance = cls()
17+
instance.data = bytes
18+
instance.filename = filename
19+
instance.mime_type = mime_type
20+
instance.source_type = 'bytes'
21+
return instance

tests/Python310Test.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class Python310Test extends Base
1919
...Base::BAR_RESPONSES,
2020
...Base::GENERAL_RESPONSES,
2121
...Base::LARGE_FILE_RESPONSES,
22+
...Base::LARGE_FILE_RESPONSES,
23+
...Base::LARGE_FILE_RESPONSES,
2224
...Base::EXCEPTION_RESPONSES,
2325
];
2426
}

tests/Python38Test.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class Python38Test extends Base
1919
...Base::BAR_RESPONSES,
2020
...Base::GENERAL_RESPONSES,
2121
...Base::LARGE_FILE_RESPONSES,
22+
...Base::LARGE_FILE_RESPONSES,
23+
...Base::LARGE_FILE_RESPONSES,
2224
...Base::EXCEPTION_RESPONSES,
2325
];
2426
}

tests/Python39Test.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class Python39Test extends Base
1919
...Base::BAR_RESPONSES,
2020
...Base::GENERAL_RESPONSES,
2121
...Base::LARGE_FILE_RESPONSES,
22+
...Base::LARGE_FILE_RESPONSES,
23+
...Base::LARGE_FILE_RESPONSES,
2224
...Base::EXCEPTION_RESPONSES,
2325
];
2426
}

tests/languages/python/tests.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from appwrite.services.bar import Bar
44
from appwrite.services.general import General
55
from appwrite.exception import AppwriteException
6+
from appwrite.input_file import InputFile
67
import os.path
78

89

@@ -55,10 +56,18 @@
5556
response = general.redirect()
5657
print(response['result'])
5758

58-
response = general.upload('string', 123, ['string in array'], './tests/resources/file.png')
59+
response = general.upload('string', 123, ['string in array'], InputFile.from_path('./tests/resources/file.png'))
5960
print(response['result'])
6061

61-
response = general.upload('string', 123, ['string in array'], './tests/resources/large_file.mp4')
62+
response = general.upload('string', 123, ['string in array'], InputFile.from_path('./tests/resources/large_file.mp4'))
63+
print(response['result'])
64+
65+
data = open('./tests/resources/file.png', 'rb').read()
66+
response = general.upload('string', 123, ['string in array'], InputFile.from_bytes(data, 'file.png', 'image/png'))
67+
print(response['result'])
68+
69+
data = open('./tests/resources/large_file.mp4', 'rb').read()
70+
response = general.upload('string', 123, ['string in array'], InputFile.from_bytes(data, 'large_file.mp4','video/mp4'))
6271
print(response['result'])
6372

6473
try:

0 commit comments

Comments
 (0)