Skip to content

Commit d8de31a

Browse files
committed
Allow specifying target directory in client.get(…)
1 parent 29df09e commit d8de31a

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

ipfshttpclient/client/files.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -366,18 +366,20 @@ def add(self, file: ty.Union[utils.path_t, int, io.IOBase], *files,
366366

367367

368368
@base.returns_no_item
369-
def get(self, cid, **kwargs):
370-
"""Downloads a file, or directory of files from IPFS.
371-
372-
Files are placed in the current working directory.
373-
369+
def get(self, cid, target: utils.path_t = ".", **kwargs):
370+
"""Downloads a file, or directory of files from IPFS
371+
374372
Parameters
375373
----------
376374
cid : Union[str, cid.CIDv0, cid.CIDv1]
377375
The path to the IPFS object(s) to be outputted
376+
target
377+
The directory to place the downloaded files in
378+
379+
Defaults to the current working directory.
378380
"""
379381
args = (str(cid),)
380-
return self._client.download('/get', args, **kwargs)
382+
return self._client.download('/get', target, args, **kwargs)
381383

382384

383385
def cat(self, cid, offset=0, length=-1, **kwargs):

ipfshttpclient/http.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,8 @@ def request(
519519

520520
@pass_defaults
521521
def download(
522-
self, path: str,
522+
self, path: str, target: utils.path_t = ".",
523523
args: ty.Sequence[str] = [], *,
524-
filepath: ty.Optional[str] = None,
525524
opts: ty.Mapping[str, str] = {},
526525
compress: bool = False,
527526
offline: bool = False,
@@ -547,12 +546,12 @@ def download(
547546
----------
548547
path
549548
The command path relative to the given base
550-
args
551-
Positional parameters to be sent along with the HTTP request
552-
filepath
553-
The local path where IPFS will store downloaded files
549+
target
550+
The local path where downloaded files should be stored at
554551
555552
Defaults to the current working directory.
553+
args
554+
Positional parameters to be sent along with the HTTP request
556555
opts
557556
Query string paramters to be sent along with the HTTP request
558557
compress
@@ -580,7 +579,6 @@ def download(
580579
Set this to :py:`math.inf` to disable timeouts entirely.
581580
"""
582581
url = self.base + path
583-
wd = filepath or '.'
584582

585583
params = []
586584
params.append(('stream-channels', 'true'))
@@ -602,4 +600,4 @@ def download(
602600
mode = 'r|gz' if compress else 'r|'
603601

604602
with tarfile.open(fileobj=res.raw, mode=mode) as tf:
605-
tf.extractall(path=wd)
603+
tf.extractall(path=utils.convert_path(target))

test/functional/test_files.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import shutil
33
import sys
4+
import tempfile
45

56
import pytest
67
import pytest_cid
@@ -406,6 +407,16 @@ def test_get_path(client, cleanup_pins):
406407
assert "fsdfgh" not in os.listdir(os.getcwd())
407408

408409

410+
@pytest.mark.dependency(depends=["test_add_recursive"])
411+
def test_get_path_with_target(client, cleanup_pins):
412+
test_hash = FAKE_DIR_HASH[0]["Hash"] + "/fsdfgh"
413+
414+
with tempfile.TemporaryDirectory() as dirpath:
415+
client.get(test_hash, dirpath)
416+
assert "fsdfgh" in os.listdir(dirpath)
417+
assert "fsdfgh" not in os.listdir(os.getcwd())
418+
419+
409420
@pytest.mark.dependency(depends=["test_add_recursive"])
410421
def test_cat_single_file_str(client, cleanup_pins):
411422
content = client.cat("QmQcCtMgLVwvMQGu6mvsRYLjwqrZJcYtH4mboM9urWW9vX")

0 commit comments

Comments
 (0)