Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def is_available():
"""
Return whether the IPFS daemon is reachable or not
"""
global __is_available
if not isinstance(__is_available, bool):
try:
ipfshttpclient.connect()
except ipfshttpclient.exceptions.Error as error:
__is_available = False
# Make sure version incompatiblity is displayed to the user
if isinstance(error, ipfshttpclient.exceptions.VersionMismatch):
raise
else:
__is_available = True
return __is_available
def test_publish_repo_basic(self, mocker):
mocker.patch.object(git.Repo, 'clone_from')
mocker.patch.object(shutil, 'rmtree')
ipfs_client_mock = mocker.Mock(spec=ipfshttpclient.Client)
ipfs_client_mock.add.return_value = [{'Hash': 'some-hash'}]
mocker.patch.object(ipfshttpclient, 'connect')
ipfshttpclient.connect.return_value = ipfs_client_mock
repo: publishing.GenericRepo = factories.RepoFactory()
repo.publish_repo()
ipfs_client_mock.add.assert_called_once_with(mocker.ANY, recursive=True, pin=True)
ipfs_client_mock.pin.rm.assert_not_called()
assert repo.last_ipfs_addr == '/ipfs/some-hash/'
host = os.environ.get(ENV_NAME_IPFS_HOST)
port = os.environ.get(ENV_NAME_IPFS_PORT)
# Hack to allow cross-platform Docker to reference the Docker host's machine with $HOST_ADDR
if host and host.startswith('$'):
logger.info(f'Resolving host name from environment variable {host}')
host = os.environ[host[1:]]
if host == 'localhost':
host = '127.0.0.1'
if not multiaddr:
multiaddr = f'/ip4/{host}/tcp/{port}/http'
logger.info(f'Connecting and caching to IPFS host \'{multiaddr}\'')
self._ipfs = ipfshttpclient.connect(multiaddr)
return self._ipfs
def up():
try:
if "file" in request.files:
file = request.files["file"]
log.info("file name: {}".format(file.filename), {'app': 'dfile-up-req'})
if 'IPFS_API_URL' in app.config:
url = app.config['IPFS_API_URL'] + '/add'
res = upload(url, file)
else:
client = ipfshttpclient.connect(app.config['IPFS_CONNECT_URL'])
res = client.add(file)
print("res: {}".format(res))
log.info("upload res: {}".format(res), {'app': 'dfile-up-res'})
url = app.config['DOMAIN'] + '/' + str(res['Hash'])
return url
abort(400)
except Exception as e:
log.exception("Upload Error! exception:{}".format(str(e)))
return "Upload Error! \n", 503
def ipfs_add_file(self, file_path):
with ipfshttpclient.connect() as ipfs_client:
response = ipfs_client.add(file_path)
return response
def pin_manifest(manifest_written_file : Path = MANIFEST_BUILD_FILE, settings_directory : Path = getcwd()):
path.append(settings_directory)
import settings
ipfs_address = settings.ipfs_settings["address"]
with ipfshttpclient.connect(ipfs_address) as client:
pinned_asset = client.add(manifest_written_file, recursive=False)
print(f"Pinned asset URI is {pinned_asset['Hash']}.")
return pinned_asset
def create_ipfs_file(data):
client = ipfshttpclient.connect('/ip4/{0}/tcp/{1}'.format(config['ipfs']['host'], config['ipfs']['port']))
if not client:
return bad_request('IPFS server can not be reached')
dir_id = str(uuid.uuid4())
file_id = str(uuid.uuid4())
dir_path = './files/' + dir_id
os.mkdir(dir_path)
file_name = file_id + '.txt'
file_path = dir_path + '/' + file_name
with open(file_path, 'w') as f:
f.write(data)
with client:
ipfs_data = client.add(file_path)
def client(self):
if self._client is None:
url = parse_url(self.base_uri)
self._client = ipfshttpclient.connect(
f'/dns/{url.host}/tcp/{url.port}/{url.scheme}', session=True
)
return self._client