forked from aerx-dev/aerx-mvp-interface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusePinata.js
68 lines (58 loc) · 2.58 KB
/
usePinata.js
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
// custom hook for IPFS
import { useEffect, useState } from "react";
import { pinFileToIPFS, unpinPinata } from "../lib/ipfsPinata";
import { upload } from "../lib/crust";
// Change this for using crust or not. If you use crust we will unpin the file from pinata
const deployCrust = false;
// pass the file or state you want to upload. It will upload the file and retrun the response.
export default function usePinata(file, toast) {
const [ipfsData, setIpfsData] = useState({
fileUrl: null,
fileSize: null,
urlSha256: null,
});
var shajs = require('sha.js')
useEffect(() => {
async function fileUpload() {
const filename = file.name
var parts = filename.split(".");
const fileType = parts[parts.length - 1];
// TODO assert that it is a image file
console.log("fileType to upload: ", fileType); //ex: zip, rar, jpg, svg etc.
pinFileToIPFS(file, filename)
.then((res) => {
// Createthe url and get the sha256 base64 hash of the url
const _fileUrl = "https://ipfs.io/ipfs/" + res.data.IpfsHash
const _urlHash = new shajs.sha256().update(_fileUrl).digest('base64')
setIpfsData((prevIpfs) => {
return {
...prevIpfs,
fileUrl: _fileUrl,
fileSize: res.data.PinSize,
urlSha256: _urlHash,
};
});
toast("success", "File deployed to IPFS! CID: " + res.data.IpfsHash, "ipfsSccss");
return [res.data.IpfsHash, res.data.PinSize]
})
.then(([fileCid, filePinSize]) => {
if (deployCrust) {
upload(fileCid, filePinSize)
.then((crustScs) => {
if (crustScs === true) {
toast("success", "File deployed to Crust!", "crustSccss");
unpinPinata(fileCid)
}
})
.catch((error) => console.log(error))
}
})
.catch((e) => {
toast("error", "Couldn't pin file to Pinata!", "ipfsError");
console.log("Couldn't pin file to Pinata ", e);
})
}
{ file && fileUpload() }
}, [file]);
return ipfsData;
}