utils.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import { VideoFile } from '@shared/models'
  2. function toTitleCase (str: string) {
  3. return str.charAt(0).toUpperCase() + str.slice(1)
  4. }
  5. function isWebRTCDisabled () {
  6. return !!((window as any).RTCPeerConnection || (window as any).mozRTCPeerConnection || (window as any).webkitRTCPeerConnection) === false
  7. }
  8. function isIOS () {
  9. if (/iPad|iPhone|iPod/.test(navigator.platform)) {
  10. return true
  11. }
  12. // Detect iPad Desktop mode
  13. return !!(navigator.maxTouchPoints &&
  14. navigator.maxTouchPoints > 2 &&
  15. /MacIntel/.test(navigator.platform))
  16. }
  17. function isSafari () {
  18. return /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
  19. }
  20. // https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts
  21. // Don't import all Angular stuff, just copy the code with shame
  22. const dictionaryBytes: Array<{max: number, type: string}> = [
  23. { max: 1024, type: 'B' },
  24. { max: 1048576, type: 'KB' },
  25. { max: 1073741824, type: 'MB' },
  26. { max: 1.0995116e12, type: 'GB' }
  27. ]
  28. function bytes (value: number) {
  29. const format = dictionaryBytes.find(d => value < d.max) || dictionaryBytes[dictionaryBytes.length - 1]
  30. const calc = Math.floor(value / (format.max / 1024)).toString()
  31. return [ calc, format.type ]
  32. }
  33. function isMobile () {
  34. return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent)
  35. }
  36. function buildVideoLink (options: {
  37. baseUrl?: string,
  38. startTime?: number,
  39. stopTime?: number,
  40. subtitle?: string,
  41. loop?: boolean,
  42. autoplay?: boolean,
  43. muted?: boolean,
  44. // Embed options
  45. title?: boolean,
  46. warningTitle?: boolean,
  47. controls?: boolean
  48. } = {}) {
  49. const { baseUrl } = options
  50. const url = baseUrl
  51. ? baseUrl
  52. : window.location.origin + window.location.pathname.replace('/embed/', '/watch/')
  53. const params = new URLSearchParams(window.location.search)
  54. // Remove these unused parameters when we are on a playlist page
  55. params.delete('videoId')
  56. params.delete('resume')
  57. if (options.startTime) {
  58. const startTimeInt = Math.floor(options.startTime)
  59. params.set('start', secondsToTime(startTimeInt))
  60. }
  61. if (options.stopTime) {
  62. const stopTimeInt = Math.floor(options.stopTime)
  63. params.set('stop', secondsToTime(stopTimeInt))
  64. }
  65. if (options.subtitle) params.set('subtitle', options.subtitle)
  66. if (options.loop === true) params.set('loop', '1')
  67. if (options.autoplay === true) params.set('autoplay', '1')
  68. if (options.muted === true) params.set('muted', '1')
  69. if (options.title === false) params.set('title', '0')
  70. if (options.warningTitle === false) params.set('warningTitle', '0')
  71. if (options.controls === false) params.set('controls', '0')
  72. let hasParams = false
  73. params.forEach(() => hasParams = true)
  74. if (hasParams) return url + '?' + params.toString()
  75. return url
  76. }
  77. function timeToInt (time: number | string) {
  78. if (!time) return 0
  79. if (typeof time === 'number') return time
  80. const reg = /^((\d+)[h:])?((\d+)[m:])?((\d+)s?)?$/
  81. const matches = time.match(reg)
  82. if (!matches) return 0
  83. const hours = parseInt(matches[2] || '0', 10)
  84. const minutes = parseInt(matches[4] || '0', 10)
  85. const seconds = parseInt(matches[6] || '0', 10)
  86. return hours * 3600 + minutes * 60 + seconds
  87. }
  88. function secondsToTime (seconds: number, full = false, symbol?: string) {
  89. let time = ''
  90. const hourSymbol = (symbol || 'h')
  91. const minuteSymbol = (symbol || 'm')
  92. const secondsSymbol = full ? '' : 's'
  93. const hours = Math.floor(seconds / 3600)
  94. if (hours >= 1) time = hours + hourSymbol
  95. else if (full) time = '0' + hourSymbol
  96. seconds %= 3600
  97. const minutes = Math.floor(seconds / 60)
  98. if (minutes >= 1 && minutes < 10 && full) time += '0' + minutes + minuteSymbol
  99. else if (minutes >= 1) time += minutes + minuteSymbol
  100. else if (full) time += '00' + minuteSymbol
  101. seconds %= 60
  102. if (seconds >= 1 && seconds < 10 && full) time += '0' + seconds + secondsSymbol
  103. else if (seconds >= 1) time += seconds + secondsSymbol
  104. else if (full) time += '00'
  105. return time
  106. }
  107. function buildVideoEmbed (embedUrl: string) {
  108. return '<iframe width="560" height="315" ' +
  109. 'sandbox="allow-same-origin allow-scripts allow-popups" ' +
  110. 'src="' + embedUrl + '" ' +
  111. 'frameborder="0" allowfullscreen>' +
  112. '</iframe>'
  113. }
  114. function copyToClipboard (text: string) {
  115. const el = document.createElement('textarea')
  116. el.value = text
  117. el.setAttribute('readonly', '')
  118. el.style.position = 'absolute'
  119. el.style.left = '-9999px'
  120. document.body.appendChild(el)
  121. el.select()
  122. document.execCommand('copy')
  123. document.body.removeChild(el)
  124. }
  125. function videoFileMaxByResolution (files: VideoFile[]) {
  126. let max = files[0]
  127. for (let i = 1; i < files.length; i++) {
  128. const file = files[i]
  129. if (max.resolution.id < file.resolution.id) max = file
  130. }
  131. return max
  132. }
  133. function videoFileMinByResolution (files: VideoFile[]) {
  134. let min = files[0]
  135. for (let i = 1; i < files.length; i++) {
  136. const file = files[i]
  137. if (min.resolution.id > file.resolution.id) min = file
  138. }
  139. return min
  140. }
  141. function getRtcConfig () {
  142. return {
  143. iceServers: [
  144. {
  145. urls: 'stun:stun.stunprotocol.org'
  146. },
  147. {
  148. urls: 'stun:stun.framasoft.org'
  149. }
  150. ]
  151. }
  152. }
  153. // ---------------------------------------------------------------------------
  154. export {
  155. getRtcConfig,
  156. toTitleCase,
  157. timeToInt,
  158. secondsToTime,
  159. isWebRTCDisabled,
  160. buildVideoLink,
  161. buildVideoEmbed,
  162. videoFileMaxByResolution,
  163. videoFileMinByResolution,
  164. copyToClipboard,
  165. isMobile,
  166. bytes,
  167. isIOS,
  168. isSafari
  169. }