-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipfs.ts
103 lines (97 loc) · 3.8 KB
/
ipfs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// import { createHeliaHTTP } from '@helia/http'
// import { strings } from '@helia/strings'
// import {delegatedHTTPRouting, httpGatewayRouting} from '@helia/routers'
// import { unixfs } from '@helia/unixfs'
// import { CID } from 'multiformats/cid'
// import {bitswap, trustlessGateway} from "@helia/block-brokers";
// const streamPipeline = promisify(stream.pipeline);
import * as fs from "fs";
import axios from "axios";
import FormData from 'form-data';
import { promisify } from 'util';
import * as stream from 'stream';
const finished = promisify(stream.finished);
export default class Ipfs {
gatewayUrl;
// helia;
constructor() {
}
async init(gatewayUrl) {
console.log('Ipfs gatewayUrl:', gatewayUrl);
this.gatewayUrl = gatewayUrl;
// const libp2p = await createLibp2p()
// this.helia = await createHeliaHTTP({
// start: true,
// blockBrokers: [
// bitswap(),
// trustlessGateway({allowLocal: true}),
// ],
// routers: [
// delegatedHTTPRouting('https://delegated-ipfs.dev'),
// httpGatewayRouting({gateways: [gatewayUrl]}),
// ]
// })
}
async saveFile(path: string) {
// Read the file
const fileStream = fs.createReadStream(path);
console.log('saveFile path', path, 'content.length:', fs.readFileSync(path, {encoding: 'utf8'}).length)
// Create a FormData object
const formData = new FormData();
formData.append('file', fileStream);
try {
const response = await axios.post(`${this.gatewayUrl}:5001/api/v0/add?cid-version=1`, formData, {
headers: formData.getHeaders(),
maxContentLength: 2e6,
timeout: 20000,
} as Object);
if (!response.data.Hash) {
throw new Error(`HTTP error! status: ${response.status}, response: ${JSON.stringify(response.data)}`);
}
return response.data.Hash; // The IPFS hash of the added file
} catch (error) {
console.error('Error adding file to IPFS:', error.message);
throw error;
}
// const {data: {result: blockNumber}} = await fetch(
// `http://127.0.0.1:5001/api/v0/add?${new URLSearchParams({'cid-version': 1} as any).toString()}`,
// {
// method: 'POST',
// body: JSON.stringify({jsonrpc: '2.0', id: 1, method: 'eth_blockNumber', params: []})
// }).then(r => r.json());
//
// const s = strings(this.helia)
// return s.add(fs.readFileSync(path, {encoding: 'utf8'})).then(cid => cid.toString());
}
async downloadFile(dirName, ipfsHash) {
const path = `${dirName}/${ipfsHash}.cjs`;
console.log('downloadFile path', path);
const writer = fs.createWriteStream(path);
return axios
.post(`${this.gatewayUrl}:5001/api/v0/cat?arg=${ipfsHash}`, {}, {
responseType: 'stream',
maxContentLength: 2e6,
timeout: 20000,
})
.then(async response => {
response.data.pipe(writer);
await finished(writer); //this is a Promise
return path;
})
.catch(e => {
console.error(ipfsHash, 'downloadFile', e.message);
return undefined;
});
// const heliaFs = unixfs(this.helia)
// const decoder = new TextDecoder()
// let text = ''
// for await (const chunk of heliaFs.cat(CID.parse(ipfsHash))) {
// text += decoder.decode(chunk, {
// stream: true
// })
// }
//
// fs.writeFileSync(path, text, 'utf8');
// return path;
}
}