fliq/ipfs-laravel

:package_description

v0.1.0 2022-08-04 13:55 UTC

This package is auto-updated.

Last update: 2024-10-30 01:54:01 UTC


README

Latest Version on Packagist Total Downloads Build Status StyleCI

Interact with IPFS in Laravel.

Installation

Via Composer

composer require fliq/ipfs-laravel

Usage

Basic usage

$cid = Ipfs::add('Hello, IPFS')->first()['Hash'];

Ipfs::get($cid); // Hello, IPFS

Configuration

To publish your config run:

php artisan vendor:publish --tag=ipfs.config

Adding Files

You can flexibly add new files to IPFS using the add() method. You can add files from a string, a resource or an array, arrays will be json encoded. Use array keys to specify files names.

The second argument is an array of options more information on the IPFS documentation. The wrap-with-directoryoption will create an extra CID for the directory.

$resource = fopen('path/to/file.mp3');

$results = Ipfs::add([
    'hello.txt' => 'Hello, IPFS!', 
    'file.mp3' => $resource,
    'meta.json' => [
        'name' => 'Hello',
        'properties' => [...],
    ],
], ['wrap-with-directory' => true]);

$cid = $results->last()['Hash'];

Ipfs::get("{$cid}/hello.txt"); // Hello, IPFS!

Retrieving files

You can retrieve files using the cat(), get(), and json() methods.

  • cat()
  • get()
    • returns a string
  • json()
    • returns a json decoded array
$stream = Ipfs::cat($cid);

$str = Ipfs::get($cid);

$array = Ipfs::json($cid);

Asynchronous requests

Use the async() method to make asynchronous requests.

Async requests return Promises

Ipfs::async()->add($file)->then(function(array $results) {
    $results[0]; // do something
});

// or do multiple requests.
$ipfs = Ipfs::async();

$promises = [
    $ipfs->get($cid1),
    $ipfs->get($cid2),
];

$results = GuzzleHttp\Promise\Utils::unwrap($promises);

Testing

$ composer test

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, please email author@email.com instead of using the issue tracker.

Credits

License

MIT. Please see the license file for more information.