utils.ts 5.0 KB

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