diff --git a/retrive.js b/IPFSFileRetriever.js similarity index 70% rename from retrive.js rename to IPFSFileRetriever.js index 1937490..4861fc7 100644 --- a/retrive.js +++ b/IPFSFileRetriever.js @@ -1,17 +1,13 @@ -import { create } from 'ipfs'; import { writeFile } from 'fs/promises'; import { fileTypeFromBuffer } from 'file-type'; +import IPFSManager from './IPFSManager.js'; -class IPFSFileManager { +export default class IPFSFileRetriever extends IPFSManager { constructor() { + super(); this.node = null; } - async initIPFS() { - this.node = await create(); - console.log('IPFS node is ready'); - } - async retrieveFileFromIPFS(cid, filepath) { try { await this.initIPFS(); @@ -23,7 +19,7 @@ class IPFSFileManager { const fileData = Buffer.concat(chunks); // 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); // 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 { return fileTypeFromBuffer(fileData); } catch (error) { @@ -69,16 +65,4 @@ class IPFSFileManager { // Return the corresponding extension if available, otherwise fallback to '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); +} \ No newline at end of file diff --git a/IPFSFileUploader.js b/IPFSFileUploader.js new file mode 100644 index 0000000..b16ca60 --- /dev/null +++ b/IPFSFileUploader.js @@ -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}`); + } + } +} \ No newline at end of file diff --git a/IPFSManager.js b/IPFSManager.js new file mode 100644 index 0000000..88952e0 --- /dev/null +++ b/IPFSManager.js @@ -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'); + } + } +} \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..37bcc04 --- /dev/null +++ b/index.js @@ -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'); + diff --git a/send.js b/send.js deleted file mode 100644 index 653a46c..0000000 --- a/send.js +++ /dev/null @@ -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);