ipfs-file-manager/IPFSFileUploader.js

31 lines
809 B
JavaScript
Raw Normal View History

2023-06-20 22:30:55 +00:00
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}`);
}
}
}