Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick send_file method. Maybe it works? #85

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 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,6 +338,45 @@ def send(self, path, data, **kwargs):

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
continue
start = end

return True

def transport_test(self, connect_timeout):
"""This method enables wait_for_connection to work.

Expand Down