]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/assets/player/utils.ts
Allow iframes to open links
[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 (options: {
31 baseUrl?: string,
32
33 startTime?: number,
34 stopTime?: number,
35
36 subtitle?: string,
37
38 loop?: boolean,
39 autoplay?: boolean,
40 muted?: boolean,
41
42 // Embed options
43 title?: boolean,
44 warningTitle?: boolean,
45 controls?: boolean
46 } = {}) {
47 const { baseUrl } = options
48
49 const url = baseUrl
50 ? baseUrl
51 : window.location.origin + window.location.pathname.replace('/embed/', '/watch/')
52
53 const params = new URLSearchParams(window.location.search)
54 // Remove this unused parameter when we are on a playlist page
55 params.delete('videoId')
56
57 if (options.startTime) {
58 const startTimeInt = Math.floor(options.startTime)
59 params.set('start', secondsToTime(startTimeInt))
60 }
61
62 if (options.stopTime) {
63 const stopTimeInt = Math.floor(options.stopTime)
64 params.set('stop', secondsToTime(stopTimeInt))
65 }
66
67 if (options.subtitle) params.set('subtitle', options.subtitle)
68
69 if (options.loop === true) params.set('loop', '1')
70 if (options.autoplay === true) params.set('autoplay', '1')
71 if (options.muted === true) params.set('muted', '1')
72 if (options.title === false) params.set('title', '0')
73 if (options.warningTitle === false) params.set('warningTitle', '0')
74 if (options.controls === false) params.set('controls', '0')
75
76 let hasParams = false
77 params.forEach(() => hasParams = true)
78
79 if (hasParams) return url + '?' + params.toString()
80
81 return url
82 }
83
84 function timeToInt (time: number | string) {
85 if (!time) return 0
86 if (typeof time === 'number') return time
87
88 const reg = /^((\d+)[h:])?((\d+)[m:])?((\d+)s?)?$/
89 const matches = time.match(reg)
90
91 if (!matches) return 0
92
93 const hours = parseInt(matches[2] || '0', 10)
94 const minutes = parseInt(matches[4] || '0', 10)
95 const seconds = parseInt(matches[6] || '0', 10)
96
97 return hours * 3600 + minutes * 60 + seconds
98 }
99
100 function secondsToTime (seconds: number, full = false, symbol?: string) {
101 let time = ''
102
103 const hourSymbol = (symbol || 'h')
104 const minuteSymbol = (symbol || 'm')
105 const secondsSymbol = full ? '' : 's'
106
107 const hours = Math.floor(seconds / 3600)
108 if (hours >= 1) time = hours + hourSymbol
109 else if (full) time = '0' + hourSymbol
110
111 seconds %= 3600
112 const minutes = Math.floor(seconds / 60)
113 if (minutes >= 1 && minutes < 10 && full) time += '0' + minutes + minuteSymbol
114 else if (minutes >= 1) time += minutes + minuteSymbol
115 else if (full) time += '00' + minuteSymbol
116
117 seconds %= 60
118 if (seconds >= 1 && seconds < 10 && full) time += '0' + seconds + secondsSymbol
119 else if (seconds >= 1) time += seconds + secondsSymbol
120 else if (full) time += '00'
121
122 return time
123 }
124
125 function buildVideoEmbed (embedUrl: string) {
126 return '<iframe width="560" height="315" ' +
127 'sandbox="allow-same-origin allow-scripts allow-popups" ' +
128 'src="' + embedUrl + '" ' +
129 'frameborder="0" allowfullscreen>' +
130 '</iframe>'
131 }
132
133 function copyToClipboard (text: string) {
134 const el = document.createElement('textarea')
135 el.value = text
136 el.setAttribute('readonly', '')
137 el.style.position = 'absolute'
138 el.style.left = '-9999px'
139 document.body.appendChild(el)
140 el.select()
141 document.execCommand('copy')
142 document.body.removeChild(el)
143 }
144
145 function videoFileMaxByResolution (files: VideoFile[]) {
146 let max = files[0]
147
148 for (let i = 1; i < files.length; i++) {
149 const file = files[i]
150 if (max.resolution.id < file.resolution.id) max = file
151 }
152
153 return max
154 }
155
156 function videoFileMinByResolution (files: VideoFile[]) {
157 let min = files[0]
158
159 for (let i = 1; i < files.length; i++) {
160 const file = files[i]
161 if (min.resolution.id > file.resolution.id) min = file
162 }
163
164 return min
165 }
166
167 function getRtcConfig () {
168 return {
169 iceServers: [
170 {
171 urls: 'stun:stun.stunprotocol.org'
172 },
173 {
174 urls: 'stun:stun.framasoft.org'
175 }
176 ]
177 }
178 }
179
180 // ---------------------------------------------------------------------------
181
182 export {
183 getRtcConfig,
184 toTitleCase,
185 timeToInt,
186 secondsToTime,
187 isWebRTCDisabled,
188 buildVideoLink,
189 buildVideoEmbed,
190 videoFileMaxByResolution,
191 videoFileMinByResolution,
192 copyToClipboard,
193 isMobile,
194 bytes
195 }