]>
Commit | Line | Data |
---|---|---|
583eb04b | 1 | import { VideoFile } from '@shared/models' |
e945b184 | 2 | |
c6352f2c C |
3 | function toTitleCase (str: string) { |
4 | return str.charAt(0).toUpperCase() + str.slice(1) | |
5 | } | |
6 | ||
31b6ddf8 C |
7 | function isWebRTCDisabled () { |
8 | return !!((window as any).RTCPeerConnection || (window as any).mozRTCPeerConnection || (window as any).webkitRTCPeerConnection) === false | |
9 | } | |
10 | ||
3e2bc4ea | 11 | function isIOS () { |
b6a8cfc5 C |
12 | if (/iPad|iPhone|iPod/.test(navigator.platform)) { |
13 | return true | |
14 | } | |
15 | ||
16 | // Detect iPad Desktop mode | |
b12ce2b8 | 17 | return !!(navigator.maxTouchPoints && |
b6a8cfc5 | 18 | navigator.maxTouchPoints > 2 && |
b12ce2b8 | 19 | /MacIntel/.test(navigator.platform)) |
3e2bc4ea C |
20 | } |
21 | ||
22 | function isSafari () { | |
23 | return /^((?!chrome|android).)*safari/i.test(navigator.userAgent) | |
24 | } | |
25 | ||
c6352f2c C |
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 | ] | |
c199c427 | 34 | function bytes (value: number) { |
c6352f2c C |
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 | ||
d1bd87e0 C |
41 | function isMobile () { |
42 | return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent) | |
43 | } | |
44 | ||
2f4c784a C |
45 | function buildVideoLink (options: { |
46 | baseUrl?: string, | |
1f6824c9 | 47 | |
2f4c784a C |
48 | startTime?: number, |
49 | stopTime?: number, | |
960a11e8 | 50 | |
2f4c784a | 51 | subtitle?: string, |
1f6824c9 | 52 | |
2f4c784a C |
53 | loop?: boolean, |
54 | autoplay?: boolean, | |
55 | muted?: boolean, | |
56 | ||
57 | // Embed options | |
58 | title?: boolean, | |
59 | warningTitle?: boolean, | |
60 | controls?: boolean | |
189ab8de | 61 | peertubeLink?: boolean |
2f4c784a C |
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) | |
52891376 | 70 | // Remove these unused parameters when we are on a playlist page |
3a1fed11 | 71 | params.delete('videoId') |
52891376 | 72 | params.delete('resume') |
2f4c784a C |
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)) | |
960a11e8 C |
82 | } |
83 | ||
2f4c784a C |
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') | |
189ab8de | 92 | if (options.peertubeLink === false) params.set('peertubeLink', '0') |
2f4c784a C |
93 | |
94 | let hasParams = false | |
95 | params.forEach(() => hasParams = true) | |
96 | ||
97 | if (hasParams) return url + '?' + params.toString() | |
98 | ||
11b8762f | 99 | return url |
1f6824c9 C |
100 | } |
101 | ||
102 | function timeToInt (time: number | string) { | |
3b019808 | 103 | if (!time) return 0 |
1f6824c9 C |
104 | if (typeof time === 'number') return time |
105 | ||
f0a39880 | 106 | const reg = /^((\d+)[h:])?((\d+)[m:])?((\d+)s?)?$/ |
1f6824c9 C |
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 | ||
f0a39880 | 118 | function secondsToTime (seconds: number, full = false, symbol?: string) { |
1f6824c9 C |
119 | let time = '' |
120 | ||
f0a39880 C |
121 | const hourSymbol = (symbol || 'h') |
122 | const minuteSymbol = (symbol || 'm') | |
123 | const secondsSymbol = full ? '' : 's' | |
124 | ||
c4710631 | 125 | const hours = Math.floor(seconds / 3600) |
f0a39880 C |
126 | if (hours >= 1) time = hours + hourSymbol |
127 | else if (full) time = '0' + hourSymbol | |
1f6824c9 C |
128 | |
129 | seconds %= 3600 | |
c4710631 | 130 | const minutes = Math.floor(seconds / 60) |
f0a39880 C |
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 | |
1f6824c9 C |
134 | |
135 | seconds %= 60 | |
f0a39880 C |
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' | |
1f6824c9 C |
139 | |
140 | return time | |
960a11e8 C |
141 | } |
142 | ||
143 | function buildVideoEmbed (embedUrl: string) { | |
144 | return '<iframe width="560" height="315" ' + | |
f2aa2c3c | 145 | 'sandbox="allow-same-origin allow-scripts allow-popups" ' + |
960a11e8 C |
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 | ||
6cca7360 C |
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 | ||
09209296 C |
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 | ||
7b3a99d5 C |
198 | // --------------------------------------------------------------------------- |
199 | ||
c6352f2c | 200 | export { |
09209296 | 201 | getRtcConfig, |
c6352f2c | 202 | toTitleCase, |
1f6824c9 | 203 | timeToInt, |
f0a39880 | 204 | secondsToTime, |
31b6ddf8 | 205 | isWebRTCDisabled, |
960a11e8 | 206 | buildVideoLink, |
960a11e8 | 207 | buildVideoEmbed, |
6cca7360 C |
208 | videoFileMaxByResolution, |
209 | videoFileMinByResolution, | |
960a11e8 | 210 | copyToClipboard, |
d1bd87e0 | 211 | isMobile, |
3e2bc4ea C |
212 | bytes, |
213 | isIOS, | |
214 | isSafari | |
c6352f2c | 215 | } |