diff --git a/ipfs_client.py b/ipfs_client.py index 5079c64..85cba7c 100644 --- a/ipfs_client.py +++ b/ipfs_client.py @@ -10,7 +10,7 @@ import logging import os import re from pathlib import Path -from typing import Optional +from typing import Optional, Union import requests @@ -132,13 +132,13 @@ def add_string(content: str, pin: bool = True) -> Optional[str]: return add_bytes(content.encode('utf-8'), pin=pin) -def get_file(cid: str, dest_path: Path) -> bool: +def get_file(cid: str, dest_path: Union[Path, str]) -> bool: """ Retrieve a file from IPFS and save to destination. Args: cid: IPFS CID to retrieve - dest_path: Path to save the file + dest_path: Path to save the file (Path object or string) Returns: True on success, False on failure @@ -148,6 +148,10 @@ def get_file(cid: str, dest_path: Path) -> bool: if data is None: return False + # Ensure dest_path is a Path object + if isinstance(dest_path, str): + dest_path = Path(dest_path) + dest_path.parent.mkdir(parents=True, exist_ok=True) dest_path.write_bytes(data) logger.info(f"Retrieved from IPFS: {cid} -> {dest_path}")