Merge pull request #7 from ProfessorDey/patch-1
Simplify Media Identification Function
This commit is contained in:
commit
93b3573ff4
1 changed files with 10 additions and 14 deletions
24
src/lbry.js
24
src/lbry.js
|
@ -114,20 +114,16 @@ Lbry.getMediaType = (contentType, fileName) => {
|
|||
if (contentType) {
|
||||
return /^[^/]+/.exec(contentType)[0];
|
||||
} else if (fileName) {
|
||||
const dotIndex = fileName.lastIndexOf('.');
|
||||
if (dotIndex === -1) {
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
const ext = fileName.substr(dotIndex + 1);
|
||||
if (/^mp4|m4v|webm|flv|f4v|ogv$/i.test(ext)) {
|
||||
return 'video';
|
||||
} else if (/^mp3|m4a|aac|wav|flac|ogg|opus$/i.test(ext)) {
|
||||
return 'audio';
|
||||
} else if (/^html|htm|xml|pdf|odf|doc|docx|md|markdown|txt|epub|org$/i.test(ext)) {
|
||||
return 'document';
|
||||
}
|
||||
return 'unknown';
|
||||
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']];
|
||||
const res = formats.reduce(function extensionMatch(ret, testpair) {
|
||||
switch (testpair[0].test(ret)) {
|
||||
case true: return testpair[1];
|
||||
default: return ret; }
|
||||
}, fileName);
|
||||
return res === fileName ? 'unknown' : res;
|
||||
}
|
||||
return 'unknown';
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue