Created
November 17, 2022 01:09
-
-
Save heidyhb89/4798b504b22929c912e43ce143240650 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const buffer = await getWnft(args, { size: IMAGE_SIZE }); // returns Buffer | |
// we are calling the function below with `fileData: buffer` | |
static async uploadFileToIPFS({ fileData, metadata = {} }) { | |
const pinataOptions = { | |
cidVersion: PINATA_CID_VERSION, | |
wrapWithDirectory: false, | |
}; | |
const pinataMetadata = { | |
keyvalues: metadata, | |
}; | |
const data = new FormData(); | |
data.append("pinataOptions", JSON.stringify(pinataOptions)); | |
data.append("pinataMetadata", JSON.stringify(pinataMetadata)); | |
data.append("file", fileData); | |
const config = { | |
method: "post", | |
url: `${this.BASE_URL}/${this.ENDPOINTS.PIN_FILE}`, | |
headers: { | |
Authorization: `Bearer ${PINATA_JWT}`, | |
...data.getHeaders(), | |
}, | |
maxBodyLength: "Infinity", | |
data, | |
}; | |
try { | |
const response = await axios(config); | |
const responseData = response.data; | |
const cid = responseData.IpfsHash; | |
logger.info(`Successfully pinned file to IPFS, CID = ${cid}.`); | |
return { success: true, cid }; | |
} catch (e) { | |
logger.info(`Error pinning file to IPFS. Error ${e}`); | |
} | |
return { success: false, cid: null }; | |
} | |
/* | |
{ | |
method: 'post', | |
url: 'https://api.pinata.cloud/pinning/pinFileToIPFS', | |
headers: { | |
Authorization: 'Bearer <token>', | |
'content-type': 'multipart/form-data; boundary=--------------------------767515994124970525600149' | |
}, | |
maxBodyLength: 'Infinity', | |
data: FormData { | |
_overheadLength: 368, | |
_valueLength: 97308, | |
_valuesToMeasure: [], | |
writable: false, | |
readable: true, | |
dataSize: 0, | |
maxDataSize: 2097152, | |
pauseStreams: true, | |
_released: false, | |
_streams: [ | |
'----------------------------767515994124970525600149\r\n' + | |
'Content-Disposition: form-data; name="pinataOptions"\r\n' + | |
'\r\n', | |
'{"cidVersion":"1","wrapWithDirectory":false}', | |
[Function: bound ], | |
'----------------------------767515994124970525600149\r\n' + | |
'Content-Disposition: form-data; name="pinataMetadata"\r\n' + | |
'\r\n', | |
'{"keyvalues":{}}', | |
[Function: bound ], | |
'----------------------------767515994124970525600149\r\n' + | |
'Content-Disposition: form-data; name="file"\r\n' + | |
'Content-Type: application/octet-stream\r\n' + | |
'\r\n', | |
<Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 08 00 00 00 08 00 08 06 00 00 00 b2 a7 d3 30 00 01 7b a7 49 44 41 54 78 9c ec dd 31 8c 9d d5 99 ... 97198 more bytes>, | |
[Function: bound ] | |
], | |
_currentStream: null, | |
_insideLoop: false, | |
_pendingNext: false, | |
_boundary: '--------------------------767515994124970525600149' | |
} | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment