Commit inicial

This commit is contained in:
Luiz F Picolo 2023-06-18 22:22:42 -04:00
commit 632473f060
6 changed files with 23371 additions and 0 deletions

1
file.txt Normal file
View File

@ -0,0 +1 @@
Meu arquivo no IPFS

23263
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

18
package.json Normal file
View File

@ -0,0 +1,18 @@
{
"name": "ipfs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "module",
"dependencies": {
"@helia/unixfs": "^1.3.0",
"helia": "^1.3.2",
"ipfs": "^0.66.1"
}
}

1
retrievedFile.txt Normal file
View File

@ -0,0 +1 @@
Meu arquivo no IPFS

37
retrive.js Normal file
View File

@ -0,0 +1,37 @@
import { create } from 'ipfs';
import { writeFileSync } from 'fs';
class IPFSFileManager {
constructor(cid) {
this.cid = cid;
this.node = null;
}
async initIPFS() {
this.node = await create();
console.log('IPFS node is ready');
}
async retrieveFileFromIPFS() {
try {
await this.initIPFS();
const chunks = [];
for await (const chunk of this.node.cat(this.cid)) {
chunks.push(chunk);
}
const fileData = Buffer.concat(chunks);
// Save the file data to a local file
const filePath = `./retrievedFile.txt`;
writeFileSync(filePath, fileData);
console.log('File retrieved from IPFS and saved as:', filePath);
} catch (error) {
console.error('Failed to retrieve the file from IPFS', error);
}
}
}
const cid = 'QmQzHxipWRHw8xmR8nwbiYasxQzKMh7rdwYwDxpyuoS9jX'; // Replace with the CID of the file you want to retrieve
const ipfsFileManager = new IPFSFileManager(cid);
ipfsFileManager.retrieveFileFromIPFS();

51
send.js Normal file
View File

@ -0,0 +1,51 @@
import { create } from 'ipfs';
import { readFile as _readFile } from 'fs';
class IPFSFileManager {
constructor(filePath) {
this.filePath = filePath;
this.node = null;
}
async initIPFS() {
this.node = await create();
console.log('IPFS node is ready');
}
readFile() {
return new Promise((resolve, reject) => {
_readFile(this.filePath, (error, data) => {
if (error) {
reject(error);
} else {
resolve(data);
}
});
});
}
async addFileToIPFS() {
try {
await this.initIPFS();
const fileData = await this.readFile();
const results = await this.node.add({ content: fileData });
const cid = results.cid.toString();
this.closeIPFS();
console.log('File added to IPFS with CID:', cid);
} catch (error) {
console.error('Failed to add the file to IPFS', error);
}
}
async closeIPFS() {
if (this.node) {
await this.node.stop();
console.log('IPFS node connection closed');
}
}
}
const filePath = './file.txt';
const ipfsFileManager = new IPFSFileManager(filePath);
ipfsFileManager.addFileToIPFS();
//ipfsFileManager.closeIPFS();