I'm using Pinata's pinFileToIPFS() function that requires a ReadableStream to upload to Pinata's IPFS nodes. I have the bytearray of the file, for example of a PNG.
I want to convert this byteArray to readableStream and upload this on IPFS.
How can i convert that in typescript?
export async function putFileToIPFS(file:any): Promise<string>{
readableStream = ** CONVERT FILE TO READABLE **
let cid ;
try {
cid = await pinata.pinFileToIPFS(readableStream)
console.log(cid)
}
catch (error) { console.error(error);}
return cid['IpfsHash']
}
Thanks
export async function putFileToIPFS(file: ArrayBuffer) {
const readableStream = new ReadableBufferStream(file)
...
}
function ReadableBufferStream(ab: ArrayBuffer) {
return new ReadableStream({
start(controller) {
controller.enqueue(ab)
controller.close()
}
})
}
Alternatively, the "read size" could be controlled by setting a chunk size with multiple enqueues. This could potentially increase/decrease the number of HTTP requests sent by pinFileToIPFS(). subarray() maintains the memory footprint by reusing the underlying ArrayBuffer.
function ReadableBufferStream(ab: ArrayBuffer, chunkSize = 64*1024) { // 64 KiB
return new ReadableStream({
start(controller) {
const bytes = new Uint8Array(ab)
for (let readIndex = 0; readIndex < bytes.byteLength;) {
controller.enqueue(bytes.subarray(readIndex, readIndex += chunkSize))
}
controller.close()
}
})
}