-
-
Save stevedylandev/dd67d99b9ae6662a00b683d678a88869 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 { Readable } = require("stream"); | |
const buffer = await getWnft(args, { size: IMAGE_SIZE }); // returns Buffer | |
const fileData = Readable.from(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, { | |
filepath: "filename.png" | |
}); | |
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