Refactored code

This commit is contained in:
Luiz F Picolo 2023-06-20 18:30:55 -04:00
parent 59bda5d12b
commit 44b8f4a924
5 changed files with 65 additions and 68 deletions

View File

@ -1,17 +1,13 @@
import { create } from 'ipfs';
import { writeFile } from 'fs/promises'; import { writeFile } from 'fs/promises';
import { fileTypeFromBuffer } from 'file-type'; import { fileTypeFromBuffer } from 'file-type';
import IPFSManager from './IPFSManager.js';
class IPFSFileManager { export default class IPFSFileRetriever extends IPFSManager {
constructor() { constructor() {
super();
this.node = null; this.node = null;
} }
async initIPFS() {
this.node = await create();
console.log('IPFS node is ready');
}
async retrieveFileFromIPFS(cid, filepath) { async retrieveFileFromIPFS(cid, filepath) {
try { try {
await this.initIPFS(); await this.initIPFS();
@ -23,7 +19,7 @@ class IPFSFileManager {
const fileData = Buffer.concat(chunks); const fileData = Buffer.concat(chunks);
// Determine the file extension based on the MIME type // Determine the file extension based on the MIME type
const { mime } = await this.getfileTypeFromFile(fileData); const mime = await this.getFileTypeFromFile(fileData);
const fileExtension = this.getFileExtension(mime); const fileExtension = this.getFileExtension(mime);
// Save the file data to a local file with the appropriate extension // Save the file data to a local file with the appropriate extension
@ -47,7 +43,7 @@ class IPFSFileManager {
} }
} }
async getfileTypeFromFile(fileData) { async getFileTypeFromFile(fileData) {
try { try {
return fileTypeFromBuffer(fileData); return fileTypeFromBuffer(fileData);
} catch (error) { } catch (error) {
@ -69,16 +65,4 @@ class IPFSFileManager {
// Return the corresponding extension if available, otherwise fallback to 'dat' // Return the corresponding extension if available, otherwise fallback to 'dat'
return extensionMap[mimeType] || 'dat'; return extensionMap[mimeType] || 'dat';
} }
}
async closeIPFS() {
if (this.node) {
await this.node.stop();
console.log('IPFS node connection closed');
}
}
}
const cid = 'QmZ6h18zefo1DEEN5QC3WberbAFAkhvKmhEF6eJou6TC3h'; // Replace with the CID of the file you want to retrieve
const filepath = './retrievedFile'; // Specify the file path without the extension
const ipfsFileManager = new IPFSFileManager();
ipfsFileManager.retrieveFileFromIPFS(cid, filepath);

31
IPFSFileUploader.js Normal file
View File

@ -0,0 +1,31 @@
import { readFile } from 'fs/promises';
import IPFSManager from './IPFSManager.js';
export default class IPFSFileUploader extends IPFSManager {
constructor() {
super();
this.node = null;
}
async uploadFileToIPFS(filePath) {
try {
await this.initIPFS();
const fileData = await this.readFile(filePath);
const results = await this.node.add({ content: fileData });
const cid = results.cid.toString();
console.log('File uploaded to IPFS with CID:', cid);
} catch (error) {
console.error('Failed to upload the file to IPFS', error);
} finally {
this.closeIPFS();
}
}
async readFile(filePath) {
try {
return await readFile(filePath);
} catch (error) {
throw new Error(`Failed to read the file: ${error}`);
}
}
}

15
IPFSManager.js Normal file
View File

@ -0,0 +1,15 @@
import { create } from 'ipfs';
export default class IPFSManager {
async initIPFS() {
this.node = await create();
console.log('IPFS node is ready');
}
async closeIPFS() {
if (this.node) {
await this.node.stop();
console.log('IPFS node connection closed');
}
}
}

13
index.js Normal file
View File

@ -0,0 +1,13 @@
import IPFSFileUploader from './IPFSFileUploader.js';
import IPFSFileRetriever from './IPFSFileRetriever.js';
// const filePath = './file.dat';
// const ipfsFileUploader = new IPFSFileUploader();
// const cid = ipfsFileUploader.uploadFileToIPFS(filePath);
// console.log(cid)
const cid = "QmP5DGmXZBrnvL2mgEUTwtccbupfKhTbwDjFMtmWAj7CLN";
const ipfsFileRetriever = new IPFSFileRetriever();
ipfsFileRetriever.retrieveFileFromIPFS(cid, './data');

46
send.js
View File

@ -1,46 +0,0 @@
import { create } from 'ipfs';
import { readFile } from 'fs/promises';
class IPFSFileManager {
constructor() {
this.node = null;
}
async initIPFS() {
this.node = await create();
console.log('IPFS node is ready');
}
async addFileToIPFS(filePath) {
try {
await this.initIPFS();
const fileData = await this.readFile(filePath);
const results = await this.node.add({ content: fileData });
const cid = results.cid.toString();
console.log('File added to IPFS with CID:', cid);
} catch (error) {
console.error('Failed to add the file to IPFS', error);
} finally {
this.closeIPFS();
}
}
async readFile(filePath) {
try {
return await readFile(filePath);
} catch (error) {
throw new Error(`Failed to read the file: ${error}`);
}
}
async closeIPFS() {
if (this.node) {
await this.node.stop();
console.log('IPFS node connection closed');
}
}
}
const filePath = './image.jpg';
const ipfsFileManager = new IPFSFileManager();
ipfsFileManager.addFileToIPFS(filePath);