All files / utils media.js

65.12% Statements 28/43
75.51% Branches 37/49
83.33% Functions 5/6
70% Lines 28/40
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 1381x   1x                                                 2x 2x                         4x 1x     3x                 21x   4x 1x     3x     3x       3x                 3x                   3x 3x     3x                     8x 1x     7x   7x                     10x 1x     9x     3x       2x       2x   2x    
import {escapeHTML} from './general';
 
export let typeChecks = [];
 
/**
 *
 * @param {String} url
 * @return {String}
 */
export function absolutizeUrl (url) {
 
	if (typeof url !== 'string') {
		throw new Error('`url` argument must be a string');
	}
 
	let el = document.createElement('div');
	el.innerHTML = `<a href="${escapeHTML(url)}">x</a>`;
	return el.firstChild.href;
}
 
/**
 * Get the format of a specific media, based on URL and additionally its mime type
 *
 * @param {String} url
 * @param {String} type
 * @return {String}
 */
export function formatType (url, type = '') {
	return (url && !type) ? getTypeFromFile(url) : getMimeFromType(type);
}
 
/**
 * Return the mime part of the type in case the attribute contains the codec
 * (`video/mp4; codecs="avc1.42E01E, mp4a.40.2"` becomes `video/mp4`)
 *
 * @see http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#the-source-element
 * @param {String} type
 * @return {String}
 */
export function getMimeFromType (type) {
 
	if (typeof type !== 'string') {
		throw new Error('`type` argument must be a string');
	}
 
	return (type && ~type.indexOf(';')) ? type.substr(0, type.indexOf(';')) : type;
}
 
/**
 * Get the type of media based on URL structure
 *
 * @param {String} url
 * @return {String}
 */
export function getTypeFromFile (url) {
 
	if (typeof url !== 'string') {
		throw new Error('`url` argument must be a string');
	}
 
	let type;
 
	// Validate `typeChecks` array
	Iif (!Array.isArray(typeChecks)) {
		throw new Error('`typeChecks` must be an array');
	}
 
	Iif (typeChecks.length) {
		for (let type of typeChecks) {
			if (typeof type !== 'function') {
				throw new Error('Element in array must be a function');
			}
		}
	}
 
	// do type checks first
	for (let check of typeChecks) {
		type = check(url);
 
		if (type !== undefined && type !== null) {
			return type;
		}
	}
 
	// the do standard extension check
	let
		ext = getExtension(url),
		normalizedExt = normalizeExtension(ext)
		;
 
	return (/(mp4|m4v|ogg|ogv|webm|webmv|flv|wmv|mpeg|mov)/gi.test(ext) ? 'video' : 'audio') + '/' + normalizedExt;
}
 
/**
 * Get media file extension from URL
 *
 * @param {String} url
 * @return {String}
 */
export function getExtension (url) {
 
	if (typeof url !== 'string') {
		throw new Error('`url` argument must be a string');
	}
 
	let baseUrl = url.split('?')[0];
 
	return ~baseUrl.indexOf('.') ? baseUrl.substring(baseUrl.lastIndexOf('.') + 1) : '';
}
 
/**
 * Get standard extension of a media file
 *
 * @param {String} extension
 * @return {String}
 */
export function normalizeExtension (extension) {
 
	if (typeof extension !== 'string') {
		throw new Error('`extension` argument must be a string');
	}
 
	switch (extension) {
		case 'mp4':
		case 'm4v':
			return 'mp4';
		case 'webm':
		case 'webma':
		case 'webmv':
			return 'webm';
		case 'ogg':
		case 'oga':
		case 'ogv':
			return 'ogg';
		default:
			return extension;
	}
}