-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.js
More file actions
133 lines (112 loc) · 3.99 KB
/
utils.js
File metadata and controls
133 lines (112 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import Web3 from "web3";
import FileManagementContract from "./FileManagement.json";
/**
* Converts a file size in bytes to a human-readable string.
* @param {number} bytes - The size of the file in bytes.
* @returns {string} - The human-readable file size string.
*/
export function formatFileSize(bytes) {
if (bytes < 1024) {
return bytes + " B";
} else if (bytes < 1024 * 1024) {
return (bytes / 1024).toFixed(2) + " KB";
} else if (bytes < 1024 * 1024 * 1024) {
return (bytes / (1024 * 1024)).toFixed(2) + " MB";
} else {
return (bytes / (1024 * 1024 * 1024)).toFixed(2) + " GB";
}
}
/**
* Returns the MIME type of a file.
* @param {File} file - The file.
* @returns {string} - The MIME type of the file.
*/
export function getMimeType(file) {
return file.type || "application/octet-stream";
}
/**
* Removes a file from the IPFS network and blockchain.
* @param {string} hash - The IPFS hash of the file to remove.
* @param {Object} contract - The contract instance.
* @param {string} account - The user's account address.
* @param {Function} onConfirmation - The callback function to invoke after the transaction has been confirmed.
*/
export async function removeFile(hash, contract, account, onConfirmation) {
try {
// Call the contract's removeFile function to remove the file from the blockchain.
const tx = await contract.methods.removeFile(hash).send({ from: account });
// Invoke the callback function with the transaction hash.
onConfirmation(tx.transactionHash);
} catch (error) {
console.error(`Failed to remove file from blockchain: ${error.message}`);
throw error;
}
}
/*export function addFile(file, contract, account) {
return new Promise((resolve, reject) => {
const reader = new window.FileReader();
reader.readAsArrayBuffer(file);
reader.onload = async () => {
const buffer = Buffer.from(reader.result);
try {
await contract.methods.addFile(file.name, buffer.toString('hex')).send({ from: account });
resolve();
} catch (error) {
reject(error);
}
};
});
} */
export const addFile = async (selectedFile, contract, account) => {
const fileReader = new window.FileReader();
fileReader.readAsArrayBuffer(selectedFile);
return new Promise((resolve, reject) => {
fileReader.onload = async () => {
try {
const buffer = await Buffer.from(fileReader.result);
const data = await contract.methods.addFile(selectedFile.name, buffer).encodeABI();
const result = await contract.methods.sendTransaction({ from: account, data: data });
resolve(result);
} catch (error) {
reject(error);
}
};
fileReader.onerror = (error) => {
reject(error);
};
});
};
/**
* Returns an instance of the FileManagement contract.
* @returns {Promise<Contract>} - An instance of the FileManagement contract.
*/
export async function getContractInstance(web3) {
// const provider = new Web3.providers.HttpProvider('http://127.0.0.1:7545'); // Use ganache's default port
// const web3 = new Web3(provider);
try {
// Get the network ID
const networkId = await web3.eth.net.getId();
console.log("Network ID:", networkId); // Debug log
// Get the - network
const deployedNetwork = FileManagementContract.networks[networkId];
console.log("Deployed Network:", deployedNetwork); // Debug log
// Check if the deployedNetwork is defined before creating the contract instance
if (deployedNetwork) {
return new web3.eth.Contract(
FileManagementContract.abi,
deployedNetwork.address
);
} else {
throw new Error(`The FileManagement contract is not deployed on the current network (ID: ${networkId}).`);
}
} catch (error) {
throw new Error(`Could not get contract instance: ${error}`);
}
}
/**
* Returns an instance of the Web3 object.
* @returns {Web3} - The Web3 object.
*/
export function getWeb3() {
return new Web3(Web3.givenProvider || "http://localhost:8545");
}