使用 Github API 时如何知道 mime 类型

How to know mime type when using Github API

我使用 JS 库(https://github.com/mikedeboer/node-github) to call GitHub api: https://developer.github.com/v3/repos/contents/ 从 repo 获取内容。

当 return 类型为 'file' 时,我想知道它是 mime 类型。如果我将 return 内容写入硬盘上的文件,有很多方法可以判断 mime 类型。

我的问题是如何在不将其写入硬盘文件的情况下知道 mime 类型。

无法通过 Git API(出于性能原因,不会扫描所有文件以确定其 MIME)获得该信息

这意味着,因为您不想编写文件并使用例如 npm mime-type, that you will have to rely on the file name extension (with npm broofa/node-mime, or now npm mime 来分析它。
这不如分析流内容可靠,但它是一种可能的解决方法。

var mime = require('mime');

mime.lookup('/path/to/file.txt');         // => 'text/plain' 
mime.lookup('file.txt');                  // => 'text/plain'