JAVASCRIPT   4

media.js

Guest on 20th July 2021 03:30:57 PM

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

Raw Paste


Login or Register to edit or fork this paste. It's free.