diff options
Diffstat (limited to 'src/misc/detect-mine.ts')
| -rw-r--r-- | src/misc/detect-mine.ts | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/misc/detect-mine.ts b/src/misc/detect-mine.ts new file mode 100644 index 0000000000..bbf49efc10 --- /dev/null +++ b/src/misc/detect-mine.ts @@ -0,0 +1,31 @@ +import * as fs from 'fs'; +import fileType from 'file-type'; +import checkSvg from '../misc/check-svg'; + +export async function detectMine(path: string) { + return new Promise<[string, string]>((res, rej) => { + 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]); + }); + }); +} |