aqua-ipfs/example/index.ts

94 lines
3.4 KiB
TypeScript

/*
* Copyright 2020 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { put, get_from, set_timeout } from '@fluencelabs/aqua-ipfs-ts';
import {createClient} from "@fluencelabs/fluence";
import { Node, testNet} from "@fluencelabs/fluence-network-environment";
const { create, urlSource } = require('ipfs-http-client');
const all = require('it-all');
const uint8ArrayConcat = require('uint8arrays/concat')
let local: Node[] = [
{
peerId: "12D3KooWHBG9oaVx4i3vi6c1rSBUm7MLBmyGmmbHoZ23pmjDCnvK",
multiaddr: "/ip4/127.0.0.1/tcp/9990/ws/p2p/12D3KooWHBG9oaVx4i3vi6c1rSBUm7MLBmyGmmbHoZ23pmjDCnvK"
},
{
peerId: "12D3KooWRABanQHUn28dxavN9ZS1zZghqoZVAYtFpoN7FdtoGTFv",
multiaddr: "/ip4/127.0.0.1/tcp/9991/ws/p2p/12D3KooWRABanQHUn28dxavN9ZS1zZghqoZVAYtFpoN7FdtoGTFv"
},
{
peerId: "12D3KooWFpQ7LHxcC9FEBUh3k4nSCC12jBhijJv3gJbi7wsNYzJ5",
multiaddr: "/ip4/127.0.0.1/tcp/9992/ws/p2p/12D3KooWFpQ7LHxcC9FEBUh3k4nSCC12jBhijJv3gJbi7wsNYzJ5"
},
];
async function main(environment: Node[]) {
// setLogLevel('DEBUG');
const fluence = await createClient(environment[1]);
console.log("📗 created a fluence client %s with relay %s", fluence.selfPeerId, fluence.relayPeerId);
let ipfsAddr = 'https://stage.fluence.dev:15001';
let ipfsMultiaddr = '/ip4/134.209.186.43/tcp/5001/p2p/12D3KooWEhCqQ9NBnmtSfNeXSNfhgccmH86xodkCUxZNEXab6pkw';
const ipfs = create(ipfsAddr);
console.log("📗 created ipfs client");
await ipfs.id();
console.log("📗 connected to ipfs");
let source = urlSource('https://images.adsttc.com/media/images/5ecd/d4ac/b357/65c6/7300/009d/large_jpg/02C.jpg?1590547607');
const file = await ipfs.add(source);
console.log("📗 uploaded file:", file);
let files = await ipfs.get(file.cid);
for await (const file of files) {
const content = uint8ArrayConcat(await all(file.content));
console.log("📗 downloaded file of length ", content.length);
}
// default IPFS timeout is 1 sec, set to 10 secs to retrieve file from remote node
await set_timeout(fluence, environment[2].peerId, 10);
console.log("📘 file hash: ", file.cid);
let getResult = await get_from(fluence, environment[2].peerId, file.cid.toString(), ipfsMultiaddr, { ttl: 10000 });
console.log("📘 Ipfs.get", getResult);
let putResult = await put(fluence, environment[2].peerId, getResult.path, { ttl: 10000 });
console.log("📘 Ipfs.put", putResult);
return;
}
let args = process.argv.slice(2);
var environment: Node[];
if (args.length >= 1 && args[0] == "local") {
environment = local;
console.log("📘 Will connect to local nodes");
} else {
environment = testNet;
console.log("📘 Will connect to testNet");
}
main(environment)
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});