Skip to content
This repository has been archived by the owner on Jul 7, 2021. It is now read-only.

Commit

Permalink
work in progress; converted to use upload function similar to go api;…
Browse files Browse the repository at this point in the history
… tests do not pass yet
  • Loading branch information
xloem committed Oct 6, 2020
1 parent 6169399 commit 0a33678
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 53 deletions.
4 changes: 3 additions & 1 deletion siaskynet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class SkynetClient():
get_skykeys
)
from ._upload import (
upload_file, upload_file_request, upload_file_request_with_chunks,
upload, upload_request,
upload_file, upload_file_request,
upload_directory, upload_directory_request
)
# pylint: enable=import-outside-toplevel
Expand Down Expand Up @@ -58,6 +59,7 @@ def execute_request(self, method, opts, **kwargs):
kwargs["timeout"] = opts["timeout_seconds"]

try:
print(opts,'REQUEST',method,url,kwargs)
return requests.request(method, url, **kwargs)
except requests.exceptions.Timeout as err:
raise TimeoutError("Request timed out") from err
Expand Down
104 changes: 52 additions & 52 deletions siaskynet/_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,48 @@ def default_upload_options():

return obj

def upload(self, uploadData, opts):
"""Uploads the given generic data and returns the skylink."""

response = self.upload_request(uploadData, opts)
sia_url = utils.uri_skynet_prefix() + response.json()["skylink"]
response.close()
return sia_url

def upload_request(self, uploadData, _opts):
"""Uploads the given generic data and returns the response object."""

opts = default_upload_options()
opts.update(self.custom_opts)
opts.update(_opts)

if len(uploadData) == 1:
fieldname = opts['portal_file_fieldname']
filename = opts['custom_filename'] # this appears to be missing in the go source; maybe the server ignores it
else:
if not opts['custom_dirname']:
raise ValueError("custom_dirname must be set when uploading multiple files")
fieldname = opts['portal_directory_file_fieldname']
filename = opts['custom_dirname']

params = {
'filename': filename,
#'skykeyname': opts['skykey_name'],
#'skykeyid': opts['skyket_id'],
}

ftuples = []
for filename, data in uploadData.items():
ftuples.append((fieldname,
(filename, data)))

return self.execute_request(
"POST",
opts,
files=ftuples,
params=params
)


def upload_file(self, path, custom_opts=None):
"""Uploads file at path with the given options."""
Expand Down Expand Up @@ -48,46 +90,12 @@ def upload_file_request(self, path, custom_opts=None):
return None

with open(path, 'rb') as file_h:
filename = opts['custom_filename'] if opts['custom_filename'] \
else os.path.basename(file_h.name)
files = {opts['portal_file_fieldname']: (filename, file_h)}
if not opts['custom_filename']:
opts['custom_filename'] = file_h.name

return self.execute_request(
"POST",
opts,
files=files,
)


def upload_file_request_with_chunks(self, fname, iterator, custom_opts=None):
"""
Posts request to upload file of unknown size with chunks.
:param str filename: The name or path to give the uploaded file.
:param str iterator: An iterator or generator yielding data to upload.
:param dict custom_opts: Custom options. See upload_file.
:return: the full response
:rtype: dict
"""

opts = default_upload_options()
opts.update(self.custom_opts)
if custom_opts is not None:
opts.update(custom_opts)

filename = opts['custom_filename'] if opts['custom_filename'] else fname

params = {'filename': filename}
headers = {'Content-Type': 'application/octet-stream'}

return self.execute_request(
"POST",
opts,
data=iterator,
headers=headers,
params=params,
)
upload_data = {filename: file_h}

return self.upload_request(upload_data, opts)

def upload_directory(self, path, custom_opts=None):
"""Uploads directory at path with the given options."""
Expand All @@ -111,21 +119,13 @@ def upload_directory_request(self, path, custom_opts=None):
print("Given path is not a directory")
return None

ftuples = []
upload_data = {}
basepath = path if path == '/' else path + '/'
files = list(utils.walk_directory(path).keys())
for filepath in files:
for filepath in utils.walk_directory(path).keys():
assert filepath.startswith(basepath)
ftuples.append((opts['portal_directory_file_fieldname'],
(filepath[len(basepath):], open(filepath, 'rb'))))

dirname = opts['custom_dirname'] if opts['custom_dirname'] else path
upload_data[filepath[len(basepath):]] = open(filepath, 'rb')

params = {"filename": dirname}
if not opts['custom_dirname']:
opts['custom_dirname'] = path

return self.execute_request(
"POST",
opts,
files=ftuples,
params=params,
)
return self.upload_request(upload_data, opts)

0 comments on commit 0a33678

Please sign in to comment.