2019-03-20 12:50:44 -07:00
|
|
|
import * as fs from 'fs';
|
2019-07-02 03:20:34 -07:00
|
|
|
import fileType = require('file-type');
|
2019-03-20 12:50:44 -07:00
|
|
|
import checkSvg from '../misc/check-svg';
|
|
|
|
|
|
|
|
export async function detectMine(path: string) {
|
2019-04-12 09:43:22 -07:00
|
|
|
return new Promise<[string, string | null]>((res, rej) => {
|
2019-03-20 12:50:44 -07:00
|
|
|
const readable = fs.createReadStream(path);
|
|
|
|
readable
|
|
|
|
.on('error', rej)
|
|
|
|
.once('data', (buffer: Buffer) => {
|
|
|
|
readable.destroy();
|
|
|
|
const type = fileType(buffer);
|
|
|
|
if (type) {
|
|
|
|
if (type.mime == 'application/xml' && checkSvg(path)) {
|
|
|
|
res(['image/svg+xml', 'svg']);
|
|
|
|
} else {
|
|
|
|
res([type.mime, type.ext]);
|
|
|
|
}
|
|
|
|
} else if (checkSvg(path)) {
|
|
|
|
res(['image/svg+xml', 'svg']);
|
|
|
|
} else {
|
|
|
|
// 種類が同定できなかったら application/octet-stream にする
|
|
|
|
res(['application/octet-stream', null]);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.on('end', () => {
|
|
|
|
// maybe 0 bytes
|
|
|
|
res(['application/octet-stream', null]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|