Skip to content

Commit 6d33beb

Browse files
committed
cleaned up multipart file streaming a lot, cleaner Client.add_* helper methods, and added compress option to file downloads.
1 parent d1a8490 commit 6d33beb

File tree

6 files changed

+191
-138
lines changed

6 files changed

+191
-138
lines changed

ipfsApi/client.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from __future__ import absolute_import
55

66
from . import http
7+
from . import multipart
78
from . import utils
89
from .commands import Command, ArgCommand, FileCommand, DownloadCommand
910

@@ -502,7 +503,14 @@ def add_str(self, string, **kwargs):
502503
>> ipfs_client.add_str('Mary had a little lamb')
503504
u'QmZfF6C9j4VtoCsTp4KSrhYH47QMd3DNXVZBKaxJdhaPab'
504505
"""
505-
return self.add(utils.make_string_buffer(string), **kwargs)
506+
chunk_size = kwargs.pop('chunk_size',
507+
multipart.default_chunk_size)
508+
body, headers = multipart.stream_text(string,
509+
chunk_size=chunk_size)
510+
return self._client.request('/add',
511+
data=body,
512+
headers=headers,
513+
**kwargs)
506514

507515
@utils.return_field('Hash')
508516
def add_json(self, json_obj, **kwargs):
@@ -512,7 +520,14 @@ def add_json(self, json_obj, **kwargs):
512520
>> ipfs_client.add_json({'one': 1, 'two': 2, 'three': 3})
513521
u'QmVz9g7m5u3oHiNKHj2CJX1dbG1gtismRS3g9NaPBBLbob'
514522
"""
515-
return self.add(utils.make_json_buffer(json_obj), **kwargs)
523+
chunk_size = kwargs.pop('chunk_size',
524+
multipart.default_chunk_size)
525+
body, headers = multipart.stream_text(utils.encode_json(json_obj),
526+
chunk_size=chunk_size)
527+
return self._client.request('/add',
528+
data=body,
529+
headers=headers,
530+
**kwargs)
516531

517532
def get_json(self, multihash, **kwargs):
518533
"""
@@ -531,7 +546,14 @@ def add_pyobj(self, py_obj, **kwargs):
531546
>> c.add_pyobj([0, 1.0, 2j, '3', 4e5])
532547
u'QmdCWFLDXqgdWQY9kVubbEHBbkieKd3uo7MtCm7nTZZE9K'
533548
"""
534-
return self.add(utils.make_pyobj_buffer(py_obj), **kwargs)
549+
chunk_size = kwargs.pop('chunk_size',
550+
multipart.default_chunk_size)
551+
body, headers = multipart.stream_text(utils.encode_pyobj(py_obj),
552+
chunk_size=chunk_size)
553+
return self._client.request('/add',
554+
data=body,
555+
headers=headers,
556+
**kwargs)
535557

536558
def get_pyobj(self, multihash, **kwargs):
537559
"""

ipfsApi/commands.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44

55
from . import multipart
6+
from .multipart import default_chunk_size
67
from .exceptions import InvalidArguments, FileCommandException
78

89
import six
@@ -37,6 +38,12 @@ def __init__(self, path, accept_multiple=True):
3738
self.accept_multiple = accept_multiple
3839

3940
def request(self, client, f, **kwargs):
41+
"""
42+
Takes either a file object, a filename, an iterable of filenames, an
43+
iterable of file objects, or a homogenous iterable of file objects and
44+
filenames. Can only take one directory at a time, which will be
45+
traversed (optionally recursive).
46+
"""
4047
if kwargs.pop('recursive', False):
4148
return self.directory(client, f, recursive=True, **kwargs)
4249
if isinstance(f, (list, tuple)):
@@ -46,25 +53,28 @@ def request(self, client, f, **kwargs):
4653
else:
4754
return self.single(client, f, **kwargs)
4855

49-
def single(self, client, _file, **kwargs):
56+
def single(self, client, _file, chunk_size=default_chunk_size, **kwargs):
5057
"""
5158
Adds a single file-like object to IPFS.
5259
"""
53-
body, headers = multipart.stream_file(_file)
60+
body, headers = multipart.stream_file(_file,
61+
chunk_size=chunk_size)
5462
return client.request(self.path, data=body, headers=headers, **kwargs)
5563

56-
def multiple(self, client, files, **kwargs):
64+
def multiple(self, client, files, chunk_size=default_chunk_size, **kwargs):
5765
"""
5866
Adds multiple file-like objects as a multipart request to IPFS.
5967
"""
6068
if not self.accept_multiple:
6169
raise FileCommandException(
6270
"[%s] does not accept multiple files." % self.path)
63-
body, headers = multipart.stream_files(files)
71+
body, headers = multipart.stream_files(files,
72+
chunk_size=chunk_size)
6473
return client.request(self.path, data=body, headers=headers, **kwargs)
6574

6675
def directory(self, client, dirname,
67-
fnpattern='*', recursive=False, **kwargs):
76+
fnpattern='*', recursive=False,
77+
chunk_size=default_chunk_size, **kwargs):
6878
"""
6979
Loads a directory recursively into IPFS, files are matched against the
7080
given pattern.
@@ -74,7 +84,8 @@ def directory(self, client, dirname,
7484
"[%s] does not accept multiple files." % self.path)
7585
body, headers = multipart.stream_directory(dirname,
7686
fnpattern=fnpattern,
77-
recursive=recursive)
87+
recursive=recursive,
88+
chunk_size=chunk_size)
7889
return client.request(self.path, data=body, headers=headers, **kwargs)
7990

8091

ipfsApi/http.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,20 @@ def request(self, path,
9494
return ret
9595

9696
@pass_defaults
97-
def download(self, path, filepath=None, args=[], opts={}, **kwargs):
97+
def download(self, path, filepath=None,
98+
args=[], opts={},
99+
compress=True, **kwargs):
98100
"""
99101
Downloads a file or files from IPFS into the current working
100102
directory, or the directory given by :filepath:.
101103
"""
102104
url = self.base + path
105+
wd = filepath or '.'
103106

104107
params = []
105108
params.append(('stream-channels', 'true'))
106-
params.append(('archive', 'true'))
107-
params.append(('compress', 'true'))
109+
if compress:
110+
params.append(('compress', 'true'))
108111

109112
for opt in opts.items():
110113
params.append(opt)
@@ -122,9 +125,10 @@ def download(self, path, filepath=None, args=[], opts={}, **kwargs):
122125

123126
res.raise_for_status()
124127

125-
# try to stream download as a gzipped tar file stream
126-
wd = filepath or '.'
127-
with tarfile.open(fileobj=res.raw, mode='r|gz') as tf:
128+
# try to stream download as a tar file stream
129+
mode = 'r|gz' if compress else 'r|'
130+
131+
with tarfile.open(fileobj=res.raw, mode=mode) as tf:
128132
tf.extractall(path=wd)
129133

130134
@contextlib.contextmanager

0 commit comments

Comments
 (0)