1 import { VideoFile } from '../../../../shared/models/videos'
3 function toTitleCase (str: string) {
4 return str.charAt(0).toUpperCase() + str.slice(1)
7 // https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts
8 // Don't import all Angular stuff, just copy the code with shame
9 const dictionaryBytes: Array<{max: number, type: string}> = [
10 { max: 1024, type: 'B' },
11 { max: 1048576, type: 'KB' },
12 { max: 1073741824, type: 'MB' },
13 { max: 1.0995116e12, type: 'GB' }
15 function bytes (value: number) {
16 const format = dictionaryBytes.find(d => value < d.max) || dictionaryBytes[dictionaryBytes.length - 1]
17 const calc = Math.floor(value / (format.max / 1024)).toString()
19 return [ calc, format.type ]
22 function isMobile () {
23 return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent)
26 function buildVideoLink (time?: number, url?: string) {
27 if (!url) url = window.location.origin + window.location.pathname.replace('/embed/', '/watch/')
30 const timeInt = Math.floor(time)
32 const params = new URLSearchParams(window.location.search)
33 params.set('start', secondsToTime(timeInt))
35 return url + '?' + params.toString()
41 function timeToInt (time: number | string) {
43 if (typeof time === 'number') return time
45 const reg = /^((\d+)[h:])?((\d+)[m:])?((\d+)s?)?$/
46 const matches = time.match(reg)
48 if (!matches) return 0
50 const hours = parseInt(matches[2] || '0', 10)
51 const minutes = parseInt(matches[4] || '0', 10)
52 const seconds = parseInt(matches[6] || '0', 10)
54 return hours * 3600 + minutes * 60 + seconds
57 function secondsToTime (seconds: number, full = false, symbol?: string) {
60 const hourSymbol = (symbol || 'h')
61 const minuteSymbol = (symbol || 'm')
62 const secondsSymbol = full ? '' : 's'
64 const hours = Math.floor(seconds / 3600)
65 if (hours >= 1) time = hours + hourSymbol
66 else if (full) time = '0' + hourSymbol
69 const minutes = Math.floor(seconds / 60)
70 if (minutes >= 1 && minutes < 10 && full) time += '0' + minutes + minuteSymbol
71 else if (minutes >= 1) time += minutes + minuteSymbol
72 else if (full) time += '00' + minuteSymbol
75 if (seconds >= 1 && seconds < 10 && full) time += '0' + seconds + secondsSymbol
76 else if (seconds >= 1) time += seconds + secondsSymbol
77 else if (full) time += '00'
82 function buildVideoEmbed (embedUrl: string) {
83 return '<iframe width="560" height="315" ' +
84 'sandbox="allow-same-origin allow-scripts" ' +
85 'src="' + embedUrl + '" ' +
86 'frameborder="0" allowfullscreen>' +
90 function copyToClipboard (text: string) {
91 const el = document.createElement('textarea')
93 el.setAttribute('readonly', '')
94 el.style.position = 'absolute'
95 el.style.left = '-9999px'
96 document.body.appendChild(el)
98 document.execCommand('copy')
99 document.body.removeChild(el)
102 function videoFileMaxByResolution (files: VideoFile[]) {
105 for (let i = 1; i < files.length; i++) {
106 const file = files[i]
107 if (max.resolution.id < file.resolution.id) max = file
113 function videoFileMinByResolution (files: VideoFile[]) {
116 for (let i = 1; i < files.length; i++) {
117 const file = files[i]
118 if (min.resolution.id > file.resolution.id) min = file
124 function getRtcConfig () {
128 urls: 'stun:stun.stunprotocol.org'
131 urls: 'stun:stun.framasoft.org'
137 // ---------------------------------------------------------------------------
146 videoFileMaxByResolution,
147 videoFileMinByResolution,