diff --git a/src/aleph_client/commands/files.py b/src/aleph_client/commands/files.py index 6f91514d..3c94f85b 100644 --- a/src/aleph_client/commands/files.py +++ b/src/aleph_client/commands/files.py @@ -1,9 +1,12 @@ +import asyncio import logging from pathlib import Path from typing import Optional import typer -from aleph.sdk import AuthenticatedAlephHttpClient + +from aleph.sdk import AuthenticatedAlephHttpClient, AlephClient + from aleph.sdk.account import _load_account from aleph.sdk.conf import settings as sdk_settings from aleph.sdk.types import AccountFromPrivateKey, StorageEnum @@ -99,3 +102,36 @@ async def upload( ) logger.debug("Upload finished") typer.echo(f"{result.json(indent=4)}") + + +@app.command() +def download( + item_hash: str = typer.Argument(..., help="IPFS hash to pin on aleph.im"), + use_ipfs: Optional[bool] = typer.Option( + default=False, help="Download on IPFS client (100MB + file)" + ), + path: Optional[str] = typer.Option(None, help="Path of the file to download"), + debug: bool = False, +): + """Download a file on aleph.im.""" + + setup_logging(debug) + + # If no path given then path == item_hash + if path is None: + path = item_hash + + with AlephClient(api_server=sdk_settings.API_HOST) as client: + try: + typer.echo("Downloading file ...") + file_path = Path(path) + with file_path.open(mode="wb") as f: + if not use_ipfs: + output_buffer = client.download_file(item_hash) + else: + output_buffer = client.download_file_ipfs(item_hash) + f.write(output_buffer) + typer.secho(f"File: '{path}' downloaded", fg=typer.colors.GREEN) + + except Exception as e: + typer.secho(f"Error downloading file: {str(e)}", fg=typer.colors.RED) diff --git a/src/aleph_client/commands/utils.py b/src/aleph_client/commands/utils.py index 22d60b21..8483f96b 100644 --- a/src/aleph_client/commands/utils.py +++ b/src/aleph_client/commands/utils.py @@ -7,6 +7,8 @@ from pygments.formatters.terminal256 import Terminal256Formatter from pygments.lexers import JsonLexer from typer import echo +from datetime import datetime +from pathlib import Path def colorful_json(obj: str): @@ -122,3 +124,4 @@ def str_to_datetime(date: Optional[str]) -> Optional[datetime]: except ValueError: pass return datetime.fromisoformat(date) + diff --git a/test.py b/test.py deleted file mode 100644 index e69de29b..00000000 diff --git a/tests/unit/test_commands.py b/tests/unit/test_commands.py index 29b05039..ee91dd8d 100644 --- a/tests/unit/test_commands.py +++ b/tests/unit/test_commands.py @@ -1,11 +1,11 @@ import json from pathlib import Path from tempfile import NamedTemporaryFile - from aleph.sdk.chains.ethereum import ETHAccount from typer.testing import CliRunner from aleph_client.__main__ import app +import pytest runner = CliRunner() diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 0671bf24..2f4ad63b 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -15,4 +15,4 @@ def test_get_message_type_value(): assert get_message_type_value(AggregateMessage) == MessageType.aggregate assert get_message_type_value(StoreMessage) == MessageType.store assert get_message_type_value(ProgramMessage) == MessageType.program - assert get_message_type_value(ForgetMessage) == MessageType.forget + assert get_message_type_value(ForgetMessage) == MessageType.forget \ No newline at end of file