-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
42 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import mime from 'mime'; | ||
|
||
const formats = [ | ||
[/\.(mp4|m4v|webm|flv|f4v|ogv)$/i, 'video'], | ||
[/\.(mp3|m4a|aac|wav|flac|ogg|opus)$/i, 'audio'], | ||
[/\.(html|htm|xml|pdf|odf|doc|docx|md|markdown|txt|epub|org)$/i, 'document'], | ||
[/\.(stl|obj|fbx|gcode)$/i, '3D-file'], | ||
]; | ||
|
||
export default function getMediaType(contentType, fileName) { | ||
const extName = mime.getExtension(contentType); | ||
const fileExt = extName ? `.${extName}` : null; | ||
const testString = fileName || fileExt; | ||
|
||
// Get mediaType from file extension | ||
if (testString) { | ||
const res = formats.reduce((ret, testpair) => { | ||
const [regex, mediaType] = testpair; | ||
|
||
return regex.test(ret) ? mediaType : ret; | ||
}, testString); | ||
|
||
if (res !== testString) return res; | ||
} | ||
|
||
// Get mediaType from contentType | ||
if (contentType) { | ||
return /^[^/]+/.exec(contentType)[0]; | ||
} | ||
|
||
return 'unknown'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters