1 | const getFileNameAndExtension = require('./getFileNameAndExtension')
|
2 | const mimeTypes = require('./mimeTypes')
|
3 |
|
4 | module.exports = function getFileType (file) {
|
5 | let fileExtension = file.name ? getFileNameAndExtension(file.name).extension : null
|
6 | fileExtension = fileExtension ? fileExtension.toLowerCase() : null
|
7 |
|
8 | if (file.type) {
|
9 | // if mime type is set in the file object already, use that
|
10 | return file.type
|
11 | } if (fileExtension && mimeTypes[fileExtension]) {
|
12 | // else, see if we can map extension to a mime type
|
13 | return mimeTypes[fileExtension]
|
14 | }
|
15 | // if all fails, fall back to a generic byte stream type
|
16 | return 'application/octet-stream'
|
17 | }
|