Merge pull request #7 from ProfessorDey/patch-1

Simplify Media Identification Function
This commit is contained in:
Sean Yesmunt 2018-04-25 12:40:13 -04:00 committed by GitHub
commit 93b3573ff4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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';
};