Skip to content

Commit

Permalink
Quick send_file method. Maybe it works?
Browse files Browse the repository at this point in the history
  • Loading branch information
Qalthos committed Jul 13, 2020
1 parent eeb6b5b commit 1e39585
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions plugins/connection/httpapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
- name: ansible_persistent_log_messages
"""

import os
from io import BytesIO

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

return response, response_buffer

def send_file(self, path, filename, chunk_size=-1, retries=0, **kwargs):
start = 0
tries = 0
total_size = os.stat(filename).st_size
with open(filename, "rb") as fileobj:
while True:
if retries > tries:
raise ConnectionError(
"Failed to upload file too many times."
)

file_slice = fileobj.read(chunk_size)
if not file_slice:
break

slice_size = len(file_slice)
end = start + slice_size
headers = {
"Content-Range": "{0}-{1}/{2}".format(
start, end - 1, total_size
),
"Content-Type": kwargs.pop(
"Content-Type", "application/octet-stream"
),
}
try:
response, response_data = self.send(
path, file_slice, headers=headers, **kwargs
)
except HTTPError:
# Try that again
start = 0
fileobj.seek(0)
tries += 1

return True

0 comments on commit 1e39585

Please sign in to comment.