How to get MIME type from Unit8Array?
import all from 'it-all'
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
const hash = 'QmRW5tvGURXTcWwch5hQyeaEVdmeU3RGjyBo8cLZhmhS9x'
const response = uint8ArrayConcat(await all(ipfs.cat(hash)))
console.log('response', response)
> Uint8Array(6746687)
I can try to take a type through the mapping header taken from response:
switch (header) {
case '89504e47':
type = 'image/png';
break;
case '47494638':
type = 'image/gif'
break
case 'ffd8ffe0':
case 'ffd8ffe1':
case 'ffd8ffe2':
case 'ffd8ffe3':
case 'ffd8ffe8':
type = 'image/jpeg'
break
default:
type = 'unknown'
break
}
But I want to do it easier.
import fileType from 'file-type'
import all from 'it-all'
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
const hash = 'QmRW5tvGURXTcWwch5hQyeaEVdmeU3RGjyBo8cLZhmhS9x'
const response = uint8ArrayConcat(await all(ipfs.cat(hash)))
const {ext, mime} = await fileType.fromBuffer(response)
> {ext: "mp3", mime: "audio/mpeg"}