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