Exemplo n.º 1
0
def download_file(cid, output=""):
    """
    Pipline to download a file with the ipfs daemon
    :param cid: file cid
    :param output: destination folder
    :return:
    """
    # Let's reboot the daemon to clear stats and pending
    kill_subprocess()
    print("Subprocess killed")
    time.sleep(1)
    reset_ipfs()
    print("IPFS Daemon (re)started")
    time.sleep(4)

    # Clear Cache
    myUtils.post("repo/gc", json=False)
    print("Cache clear!")
    time.sleep(2)
    # delete downloaded file
    myUtils.rm(cid)
    print("File Deleted!")
    # Start download
    current_app.config["cid"] = cid
    command = "ipfs get -o {} {}".format(output, cid)
    process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
    current_app.config["process"] = process
    return "started"
Exemplo n.º 2
0
def get_peers():
    """
    Get the list of connected peers
    :return: list of peers' ID and IP addr
    """
    # in CLI: ipfs swarm peers

    peers = myUtils.post("swarm/peers")
    return [[d['Peer'], myUtils.filter_ip(d['Addr'])] for d in peers["Peers"]]
Exemplo n.º 3
0
def consult_ledger(peer_id):
    """
    Get info about a peer checking the ledger
    :param peer_id: Peer ID to check in the ledger
    :return: [dataSent, dataReceived] in Bytes
    """
    # in CLI: ipfs bitswap ledger <peerID>

    ledger = myUtils.post("bitswap/ledger?arg={}".format(peer_id))
    return [ledger["Sent"], ledger["Recv"]]
Exemplo n.º 4
0
def get_file_size(cid):
    """
    Get the file size given a cid
    :param cid: file cid
    :return: file size in Bytes
    """
    # in CLI: ipfs object stat <cid>
    stat = myUtils.post("object/stat?arg={}".format(cid))
    if "CumulativeSize" in stat:
        return int(stat["CumulativeSize"])-int(stat["BlockSize"])-int(stat["LinksSize"])
    else:
        return "Invalid Request"
Exemplo n.º 5
0
def get_speed():
    speeds = myUtils.post("stats/bw")
    return [speeds["RateIn"], speeds["RateOut"]]