2
votes

I use JS lib (https://github.com/mikedeboer/node-github) to call GitHub api: https://developer.github.com/v3/repos/contents/ to get content from repo.

When the return type is 'file', I want to know it's mime-type. If I write the return content into a file on hard driver, there are lots of way to tell the mime-type.

My question is how to know the mime-type without writing it into a file on hard driver.

1

1 Answers

1
votes

That information is not available through the Git API (which for performance reason, does not scan all files to determine their MIME)

That means, since you don't want to write the file and analyse it with, for instance, npm mime-type, that you will have to rely on the file name extension (with npm broofa/node-mime, or now npm mime for instance).
That is not as reliable as analyzing the stream content, but it is a possible workaround.

var mime = require('mime');

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