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