Refactor IPFSFileManager class in retrive.js

This commit is contained in:
Luiz F Picolo 2023-06-20 14:14:56 -04:00
parent 8289c1982b
commit 59bda5d12b

View File

@ -30,10 +30,11 @@ class IPFSFileManager {
const filePath = `${filepath}.${fileExtension}`; const filePath = `${filepath}.${fileExtension}`;
await this.saveFileToLocal(filePath, fileData); await this.saveFileToLocal(filePath, fileData);
this.closeIPFS();
console.log('File retrieved from IPFS and saved as:', filePath); console.log('File retrieved from IPFS and saved as:', filePath);
} catch (error) { } catch (error) {
console.error('Failed to retrieve the file from IPFS', error); console.error('Failed to retrieve the file from IPFS', error);
} finally {
this.closeIPFS();
} }
} }
@ -61,6 +62,7 @@ class IPFSFileManager {
'application/pdf': 'pdf', 'application/pdf': 'pdf',
'image/jpeg': 'jpg', 'image/jpeg': 'jpg',
'image/png': 'png', 'image/png': 'png',
'text/plain': 'txt',
// Add more MIME types and extensions as needed // Add more MIME types and extensions as needed
}; };
@ -76,7 +78,7 @@ class IPFSFileManager {
} }
} }
const cid = 'QmQgGxzWnzjCfouxRiozBiEG3wcsuJGWjtHjv3wurVbJ9s'; // Replace with the CID of the file you want to retrieve 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 filepath = './retrievedFile'; // Specify the file path without the extension
const ipfsFileManager = new IPFSFileManager(); const ipfsFileManager = new IPFSFileManager();
ipfsFileManager.retrieveFileFromIPFS(cid, filepath); ipfsFileManager.retrieveFileFromIPFS(cid, filepath);