簡體   English   中英

IPFS。 如何從 Unit8Array 獲取 MIME 類型?

[英]IPFS. How to get MIME type from Unit8Array?

如何從 Unit8Array 獲取 MIME 類型?

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)

我可以嘗試通過從response獲取的映射header獲取類型:

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
}

但我想做得更容易。

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"}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM