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