]>
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 | 45 | function buildVideoLink (options: { |
951b582f | 46 | baseUrl?: string |
1f6824c9 | 47 | |
951b582f C |
48 | startTime?: number |
49 | stopTime?: number | |
960a11e8 | 50 | |
951b582f | 51 | subtitle?: string |
1f6824c9 | 52 | |
951b582f C |
53 | loop?: boolean |
54 | autoplay?: boolean | |
55 | muted?: boolean | |
2f4c784a C |
56 | |
57 | // Embed options | |
951b582f C |
58 | title?: boolean |
59 | warningTitle?: boolean | |
2f4c784a | 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 | ||
951b582f | 69 | const params = generateParams(window.location.search) |
2f4c784a C |
70 | |
71 | if (options.startTime) { | |
72 | const startTimeInt = Math.floor(options.startTime) | |
73 | params.set('start', secondsToTime(startTimeInt)) | |
74 | } | |
75 | ||
76 | if (options.stopTime) { | |
77 | const stopTimeInt = Math.floor(options.stopTime) | |
78 | params.set('stop', secondsToTime(stopTimeInt)) | |
960a11e8 C |
79 | } |
80 | ||
2f4c784a C |
81 | if (options.subtitle) params.set('subtitle', options.subtitle) |
82 | ||
83 | if (options.loop === true) params.set('loop', '1') | |
84 | if (options.autoplay === true) params.set('autoplay', '1') | |
85 | if (options.muted === true) params.set('muted', '1') | |
86 | if (options.title === false) params.set('title', '0') | |
87 | if (options.warningTitle === false) params.set('warningTitle', '0') | |
88 | if (options.controls === false) params.set('controls', '0') | |
189ab8de | 89 | if (options.peertubeLink === false) params.set('peertubeLink', '0') |
2f4c784a | 90 | |
951b582f C |
91 | return buildUrl(url, params) |
92 | } | |
93 | ||
94 | function buildPlaylistLink (options: { | |
95 | baseUrl?: string | |
96 | ||
97 | playlistPosition: number | |
98 | }) { | |
99 | const { baseUrl } = options | |
100 | ||
101 | const url = baseUrl | |
102 | ? baseUrl | |
103 | : window.location.origin + window.location.pathname.replace('/video-playlists/embed/', '/videos/watch/playlist/') | |
104 | ||
105 | const params = generateParams(window.location.search) | |
106 | ||
107 | if (options.playlistPosition) params.set('playlistPosition', '' + options.playlistPosition) | |
108 | ||
109 | return buildUrl(url, params) | |
110 | } | |
111 | ||
112 | function buildUrl (url: string, params: URLSearchParams) { | |
2f4c784a C |
113 | let hasParams = false |
114 | params.forEach(() => hasParams = true) | |
115 | ||
116 | if (hasParams) return url + '?' + params.toString() | |
117 | ||
11b8762f | 118 | return url |
1f6824c9 C |
119 | } |
120 | ||
951b582f C |
121 | function generateParams (url: string) { |
122 | const params = new URLSearchParams(window.location.search) | |
123 | // Unused parameters in embed | |
124 | params.delete('videoId') | |
125 | params.delete('resume') | |
126 | ||
127 | return params | |
128 | } | |
129 | ||
1f6824c9 | 130 | function timeToInt (time: number | string) { |
3b019808 | 131 | if (!time) return 0 |
1f6824c9 C |
132 | if (typeof time === 'number') return time |
133 | ||
f0a39880 | 134 | const reg = /^((\d+)[h:])?((\d+)[m:])?((\d+)s?)?$/ |
1f6824c9 C |
135 | const matches = time.match(reg) |
136 | ||
137 | if (!matches) return 0 | |
138 | ||
139 | const hours = parseInt(matches[2] || '0', 10) | |
140 | const minutes = parseInt(matches[4] || '0', 10) | |
141 | const seconds = parseInt(matches[6] || '0', 10) | |
142 | ||
143 | return hours * 3600 + minutes * 60 + seconds | |
144 | } | |
145 | ||
f0a39880 | 146 | function secondsToTime (seconds: number, full = false, symbol?: string) { |
1f6824c9 C |
147 | let time = '' |
148 | ||
f0a39880 C |
149 | const hourSymbol = (symbol || 'h') |
150 | const minuteSymbol = (symbol || 'm') | |
151 | const secondsSymbol = full ? '' : 's' | |
152 | ||
c4710631 | 153 | const hours = Math.floor(seconds / 3600) |
f0a39880 C |
154 | if (hours >= 1) time = hours + hourSymbol |
155 | else if (full) time = '0' + hourSymbol | |
1f6824c9 C |
156 | |
157 | seconds %= 3600 | |
c4710631 | 158 | const minutes = Math.floor(seconds / 60) |
f0a39880 C |
159 | if (minutes >= 1 && minutes < 10 && full) time += '0' + minutes + minuteSymbol |
160 | else if (minutes >= 1) time += minutes + minuteSymbol | |
161 | else if (full) time += '00' + minuteSymbol | |
1f6824c9 C |
162 | |
163 | seconds %= 60 | |
f0a39880 C |
164 | if (seconds >= 1 && seconds < 10 && full) time += '0' + seconds + secondsSymbol |
165 | else if (seconds >= 1) time += seconds + secondsSymbol | |
166 | else if (full) time += '00' | |
1f6824c9 C |
167 | |
168 | return time | |
960a11e8 C |
169 | } |
170 | ||
951b582f | 171 | function buildVideoOrPlaylistEmbed (embedUrl: string) { |
960a11e8 | 172 | return '<iframe width="560" height="315" ' + |
f2aa2c3c | 173 | 'sandbox="allow-same-origin allow-scripts allow-popups" ' + |
960a11e8 C |
174 | 'src="' + embedUrl + '" ' + |
175 | 'frameborder="0" allowfullscreen>' + | |
176 | '</iframe>' | |
177 | } | |
178 | ||
179 | function copyToClipboard (text: string) { | |
180 | const el = document.createElement('textarea') | |
181 | el.value = text | |
182 | el.setAttribute('readonly', '') | |
183 | el.style.position = 'absolute' | |
184 | el.style.left = '-9999px' | |
185 | document.body.appendChild(el) | |
186 | el.select() | |
187 | document.execCommand('copy') | |
188 | document.body.removeChild(el) | |
189 | } | |
190 | ||
6cca7360 C |
191 | function videoFileMaxByResolution (files: VideoFile[]) { |
192 | let max = files[0] | |
193 | ||
194 | for (let i = 1; i < files.length; i++) { | |
195 | const file = files[i] | |
196 | if (max.resolution.id < file.resolution.id) max = file | |
197 | } | |
198 | ||
199 | return max | |
200 | } | |
201 | ||
202 | function videoFileMinByResolution (files: VideoFile[]) { | |
203 | let min = files[0] | |
204 | ||
205 | for (let i = 1; i < files.length; i++) { | |
206 | const file = files[i] | |
207 | if (min.resolution.id > file.resolution.id) min = file | |
208 | } | |
209 | ||
210 | return min | |
211 | } | |
212 | ||
09209296 C |
213 | function getRtcConfig () { |
214 | return { | |
215 | iceServers: [ | |
216 | { | |
217 | urls: 'stun:stun.stunprotocol.org' | |
218 | }, | |
219 | { | |
220 | urls: 'stun:stun.framasoft.org' | |
221 | } | |
222 | ] | |
223 | } | |
224 | } | |
225 | ||
7b3a99d5 C |
226 | // --------------------------------------------------------------------------- |
227 | ||
c6352f2c | 228 | export { |
09209296 | 229 | getRtcConfig, |
c6352f2c | 230 | toTitleCase, |
1f6824c9 | 231 | timeToInt, |
f0a39880 | 232 | secondsToTime, |
31b6ddf8 | 233 | isWebRTCDisabled, |
951b582f | 234 | buildPlaylistLink, |
960a11e8 | 235 | buildVideoLink, |
951b582f | 236 | buildVideoOrPlaylistEmbed, |
6cca7360 C |
237 | videoFileMaxByResolution, |
238 | videoFileMinByResolution, | |
960a11e8 | 239 | copyToClipboard, |
d1bd87e0 | 240 | isMobile, |
3e2bc4ea C |
241 | bytes, |
242 | isIOS, | |
243 | isSafari | |
c6352f2c | 244 | } |