*Need to refactor and make mime parsing its own separate step, and then request the stream a second time
This commit is contained in:
parent
f9f074e73f
commit
665a7e7ee1
|
@ -125,17 +125,56 @@ export default class IpfsProvider extends BaseProvider {
|
|||
reqStream.close();
|
||||
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 streaming = (async function () {
|
||||
let bufferRead = 0;
|
||||
const fileTypeBufferLength = 4100;
|
||||
const mimeBuffer = [];
|
||||
let checkMime = false;
|
||||
|
||||
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 () {
|
||||
try {
|
||||
// @ts-ignore
|
||||
for await (const chunk of reader.iterable()) {
|
||||
|
|
Reference in New Issue