Skip to content

Allow specifying destination directory and filename in get() #158

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

Closed
Closed
Show file tree
Hide file tree
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
13 changes: 10 additions & 3 deletions ipfshttpclient/client/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def file_ls(self, multihash, **kwargs):
return self._client.request('/file/ls', args, decoder='json', **kwargs)


def get(self, multihash, **kwargs):
def get(self, multihash, filepath=None, filename=None, **kwargs):
"""Downloads a file, or directory of files from IPFS.

Files are placed in the current working directory.
Expand All @@ -338,9 +338,16 @@ def get(self, multihash, **kwargs):
----------
multihash : str
The path to the IPFS object(s) to be outputted
filepath : str
The local directory where IPFS will store downloaded files

Defaults to the current working directory.
If the directory does not exist, it will be created.
filename : str
The filename to store the file as.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need two parameters here? Why not just have a path relative to the current working directory whose basename will be the filename of the object to download (meaning that the file at the path will be either a file or a directory depending on its actual type)?

"""
args = (multihash,)
return self._client.download('/get', args, **kwargs)
return self._client.download('/get', args, filepath=filepath, filename=filename, **kwargs)


def cat(self, multihash, offset=0, length=-1, **kwargs):
Expand Down Expand Up @@ -406,4 +413,4 @@ def ls(self, multihash, **kwargs):
dict : Directory information and contents
"""
args = (multihash,)
return self._client.request('/ls', args, decoder='json', **kwargs)
return self._client.request('/ls', args, decoder='json', **kwargs)
15 changes: 13 additions & 2 deletions ipfshttpclient/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import functools
import re
import tarfile
import shutil
from six.moves import http_client

import requests
Expand Down Expand Up @@ -247,7 +248,7 @@ def request(self, path,
files, headers, data)

@pass_defaults
def download(self, path, args=[], filepath=None, opts={},
def download(self, path, args=None, filepath=None, filename=None, opts={},
compress=True, **kwargs):
"""Makes a request to the IPFS daemon to download a file.

Expand All @@ -267,9 +268,12 @@ def download(self, path, args=[], filepath=None, opts={},
path : str
The REST command path to send
filepath : str
The local path where IPFS will store downloaded files
The local directory where IPFS will store downloaded files

Defaults to the current working directory.
If the directory does not exist, it will be created.
filename : str
The filename to store the file as.
args : list
Positional parameters to be sent along with the HTTP request
opts : dict
Expand All @@ -280,6 +284,8 @@ def download(self, path, args=[], filepath=None, opts={},
kwargs : dict
Additional arguments to pass to :mod:`requests`
"""
if not args:
args = []
url = self.base + path
wd = filepath or '.'

Expand Down Expand Up @@ -307,6 +313,11 @@ def download(self, path, args=[], filepath=None, opts={},
with tarfile.open(fileobj=res.raw, mode=mode) as tf:
tf.extractall(path=wd)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please iterate over the fsnodes of the received tar file using TarFile.next(), extracting each of them using them using the TarFile.extract() method rather then moving them around like this after the fact.


if type(filename) is str:
# Assume that arg 0 is multihash. Not great.
old_name = args[0]
shutil.move((wd + '/' + old_name), (wd + '/' + filename))

@contextlib.contextmanager
def session(self):
"""A context manager for this client's session.
Expand Down
28 changes: 27 additions & 1 deletion test/functional/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,32 @@ def test_get_file(client, cleanup_pins):
assert test_hash not in os.listdir(os.getcwd())


def test_get_file_filepath(client, cleanup_pins):
client.add(FAKE_FILE1_PATH)

test_hash = FAKE_DIR_HASH[1]["Hash"]
filepath = os.getcwd() + "/test_downloads"
try:
client.get(test_hash, filepath=filepath)
assert test_hash in os.listdir(filepath)
finally:
os.remove(filepath + "/" + test_hash)
assert test_hash not in os.listdir(filepath)


def test_get_file_filename(client, cleanup_pins):
client.add(FAKE_FILE1_PATH)

test_hash = FAKE_DIR_HASH[1]["Hash"]
filename = "foo"
try:
client.get(test_hash, filename=filename)
assert filename in os.listdir(os.getcwd())
finally:
os.remove(filename)
assert filename not in os.listdir(os.getcwd())


def test_get_dir(client, cleanup_pins):
client.add(FAKE_DIR_PATH, recursive=True)

Expand Down Expand Up @@ -267,4 +293,4 @@ def test_mfs_dir_make_fill_list_delete(client):
client.files.rm(TEST_MFS_DIRECTORY, recursive=True)

with pytest.raises(ipfshttpclient.exceptions.Error):
client.files.stat(TEST_MFS_DIRECTORY)
client.files.stat(TEST_MFS_DIRECTORY)