*Need to refactor and make mime parsing its own separate step, and then request the stream a second time

This commit is contained in:
Derrick Hammer 2023-04-17 02:37:46 -04:00
parent f9f074e73f
commit 665a7e7ee1
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 47 additions and 8 deletions

View File

@ -125,17 +125,56 @@ export default class IpfsProvider extends BaseProvider {
reqStream.close(); reqStream.close();
return {}; return {};
} }
const streamWriter = reqStream.stream.writable.getWriter();
const reader = await this._client.cat(cid, { path: urlPath });
const provider = this; let reader = await this._client.cat(cid, { path: urlPath });
let bufferRead = 0;
const fileTypeBufferLength = 4100;
const mimeBuffer = [];
for await (const chunk of reader.iterable()) {
if (bufferRead < fileTypeBufferLength) {
if (chunk.length >= fileTypeBufferLength) {
mimeBuffer.push(chunk.slice(0, fileTypeBufferLength));
bufferRead += fileTypeBufferLength;
} else {
mimeBuffer.push(chunk);
bufferRead += chunk.length;
}
if (bufferRead >= fileTypeBufferLength) {
reader.abort();
break;
}
} else {
reader.abort();
break;
}
}
if (bufferRead >= fileTypeBufferLength) {
const mime = await fileTypeFromBuffer(
mimeBuffer.reduce((acc, val) => {
return new Uint8Array([...acc, ...val]);
}, new Uint8Array())
);
if (mime) {
this.setData(details, "contentType", mime.mime);
}
if (!mime) {
const ext = path.parse(urlPath).ext.replace(".", "");
if (extToMimes.has(ext)) {
this.setData(details, "contentType", extToMimes.get(ext));
}
}
}
reader = await this._client.cat(cid, { path: urlPath });
const streamWriter = reqStream.stream.writable.getWriter();
let streaming = (async function () { let streaming = (async function () {
let bufferRead = 0;
const fileTypeBufferLength = 4100;
const mimeBuffer = [];
let checkMime = false;
try { try {
// @ts-ignore // @ts-ignore
for await (const chunk of reader.iterable()) { for await (const chunk of reader.iterable()) {