Refactor IPFSFileManager code for file retrieval and saving
- Removed unused constructor parameter in IPFSFileManager class - Renamed class method to for clarity - Updated import statement for the library to use function instead of the default import - Added method to retrieve the file type from the buffer - Modified the code to determine the file extension based on the MIME type using method - Updated the file name in the saveFileToLocal method to include the appropriate file extension based on the MIME type
This commit is contained in:
parent
764f097326
commit
e60be91d49
38
retrive.js
38
retrive.js
@ -1,5 +1,6 @@
|
|||||||
import { create } from 'ipfs';
|
import { create } from 'ipfs';
|
||||||
import { writeFile } from 'fs/promises';
|
import { writeFile } from 'fs/promises';
|
||||||
|
import { fileTypeFromBuffer } from 'file-type';
|
||||||
|
|
||||||
class IPFSFileManager {
|
class IPFSFileManager {
|
||||||
constructor() {
|
constructor() {
|
||||||
@ -16,13 +17,17 @@ class IPFSFileManager {
|
|||||||
await this.initIPFS();
|
await this.initIPFS();
|
||||||
|
|
||||||
const chunks = [];
|
const chunks = [];
|
||||||
for await (const chunk of this.node.cat(this.cid)) {
|
for await (const chunk of this.node.cat(cid)) {
|
||||||
chunks.push(chunk);
|
chunks.push(chunk);
|
||||||
}
|
}
|
||||||
const fileData = Buffer.concat(chunks);
|
const fileData = Buffer.concat(chunks);
|
||||||
|
|
||||||
// Save the file data to a local file
|
// Determine the file extension based on the MIME type
|
||||||
const filePath = `./retrievedFile.pdf`;
|
const { mime } = await this.getfileTypeFromFile(fileData);
|
||||||
|
const fileExtension = this.getFileExtension(mime);
|
||||||
|
|
||||||
|
// Save the file data to a local file with the appropriate extension
|
||||||
|
const filePath = `${filepath}.${fileExtension}`;
|
||||||
await this.saveFileToLocal(filePath, fileData);
|
await this.saveFileToLocal(filePath, fileData);
|
||||||
|
|
||||||
this.closeIPFS();
|
this.closeIPFS();
|
||||||
@ -41,6 +46,28 @@ class IPFSFileManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getfileTypeFromFile(fileData) {
|
||||||
|
try {
|
||||||
|
return fileTypeFromBuffer(fileData);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to determine the file type', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getFileExtension(mimeType) {
|
||||||
|
// Map commonly used MIME types to file extensions
|
||||||
|
const extensionMap = {
|
||||||
|
'application/pdf': 'pdf',
|
||||||
|
'image/jpeg': 'jpg',
|
||||||
|
'image/png': 'png',
|
||||||
|
// Add more MIME types and extensions as needed
|
||||||
|
};
|
||||||
|
|
||||||
|
// Return the corresponding extension if available, otherwise fallback to 'dat'
|
||||||
|
return extensionMap[mimeType] || 'dat';
|
||||||
|
}
|
||||||
|
|
||||||
async closeIPFS() {
|
async closeIPFS() {
|
||||||
if (this.node) {
|
if (this.node) {
|
||||||
await this.node.stop();
|
await this.node.stop();
|
||||||
@ -50,5 +77,6 @@ class IPFSFileManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const cid = 'QmQgGxzWnzjCfouxRiozBiEG3wcsuJGWjtHjv3wurVbJ9s'; // Replace with the CID of the file you want to retrieve
|
const cid = 'QmQgGxzWnzjCfouxRiozBiEG3wcsuJGWjtHjv3wurVbJ9s'; // Replace with the CID of the file you want to retrieve
|
||||||
const ipfsFileManager = new IPFSFileManager(cid);
|
const filepath = './retrievedFile'; // Specify the file path without the extension
|
||||||
ipfsFileManager.retrieveFileFromIPFS();
|
const ipfsFileManager = new IPFSFileManager();
|
||||||
|
ipfsFileManager.retrieveFileFromIPFS(cid, filepath);
|
||||||
|
Loading…
Reference in New Issue
Block a user