Skip to content

Commit 1e39585

Browse files
committed
Quick send_file method. Maybe it works?
1 parent eeb6b5b commit 1e39585

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

plugins/connection/httpapi.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@
169169
- name: ansible_persistent_log_messages
170170
"""
171171

172+
import os
172173
from io import BytesIO
173174

174175
from ansible.errors import AnsibleConnectionFailure
@@ -337,3 +338,40 @@ def send(self, path, data, **kwargs):
337338
response_buffer.seek(0)
338339

339340
return response, response_buffer
341+
342+
def send_file(self, path, filename, chunk_size=-1, retries=0, **kwargs):
343+
start = 0
344+
tries = 0
345+
total_size = os.stat(filename).st_size
346+
with open(filename, "rb") as fileobj:
347+
while True:
348+
if retries > tries:
349+
raise ConnectionError(
350+
"Failed to upload file too many times."
351+
)
352+
353+
file_slice = fileobj.read(chunk_size)
354+
if not file_slice:
355+
break
356+
357+
slice_size = len(file_slice)
358+
end = start + slice_size
359+
headers = {
360+
"Content-Range": "{0}-{1}/{2}".format(
361+
start, end - 1, total_size
362+
),
363+
"Content-Type": kwargs.pop(
364+
"Content-Type", "application/octet-stream"
365+
),
366+
}
367+
try:
368+
response, response_data = self.send(
369+
path, file_slice, headers=headers, **kwargs
370+
)
371+
except HTTPError:
372+
# Try that again
373+
start = 0
374+
fileobj.seek(0)
375+
tries += 1
376+
377+
return True

0 commit comments

Comments
 (0)