diff --git a/src/app/utils/fetchUtil.ts b/src/app/utils/fetchUtil.ts index 8f563e2..9fa878e 100644 --- a/src/app/utils/fetchUtil.ts +++ b/src/app/utils/fetchUtil.ts @@ -61,14 +61,43 @@ export async function serverFetchBlob(...args: FetchParams) { const response = await serverFetch(...args); if (response.ok) { + const body = response.body + // console.log(body) + // console.log(body?.tee()[0].getReader()) + + + const reader = body?.getReader() + let finalUInt8Array = new Uint8Array() + let done = false + + // Read some more, and call this function again + while (!done) { + // Result objects contain two properties: + // done - true if the stream has already given you all its data. + // value - some data. Always undefined when done is true. + const read = await reader?.read() + + if (read?.done) { + done = true + } else { + // value for fetch streams is a Uint8Array + finalUInt8Array = new Uint8Array(read?.value.length!!) + finalUInt8Array.set(read?.value!!) + } + } + + // const bodyRead = await reader?.read() + // const bodyValue = bodyRead?.value + // const blob = await response.blob() // const blobText = await blob.text(); // const blobType = await blob.type; - const readBody = await response.body?.getReader().read() - const bodyValue = readBody!!.value!! - - console.log(bodyValue) - return {filename: response.headers.get("filename"), blobValue: bodyValue} as T; + + // console.log(bodyReader) + // console.log(finalUInt8Array) + // console.log(bodyValue) + + return { filename: response.headers.get("filename"), blobValue: finalUInt8Array } as T; } else { switch (response.status) { case 401: