]>
Commit | Line | Data |
---|---|---|
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 = generateParams(window.location.search) | |
70 | ||
71 | if (options.startTime !== undefined && options.startTime !== null) { | |
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)) | |
79 | } | |
80 | ||
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') | |
89 | if (options.peertubeLink === false) params.set('peertubeLink', '0') | |
90 | ||
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) { | |
113 | let hasParams = false | |
114 | params.forEach(() => hasParams = true) | |
115 | ||
116 | if (hasParams) return url + '?' + params.toString() | |
117 | ||
118 | return url | |
119 | } | |
120 | ||
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 | ||
130 | function timeToInt (time: number | string) { | |
131 | if (!time) return 0 | |
132 | if (typeof time === 'number') return time | |
133 | ||
134 | const reg = /^((\d+)[h:])?((\d+)[m:])?((\d+)s?)?$/ | |
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 | ||
146 | function secondsToTime (seconds: number, full = false, symbol?: string) { | |
147 | let time = '' | |
148 | ||
149 | if (seconds === 0 && !full) return '0s' | |
150 | ||
151 | const hourSymbol = (symbol || 'h') | |
152 | const minuteSymbol = (symbol || 'm') | |
153 | const secondsSymbol = full ? '' : 's' | |
154 | ||
155 | const hours = Math.floor(seconds / 3600) | |
156 | if (hours >= 1) time = hours + hourSymbol | |
157 | else if (full) time = '0' + hourSymbol | |
158 | ||
159 | seconds %= 3600 | |
160 | const minutes = Math.floor(seconds / 60) | |
161 | if (minutes >= 1 && minutes < 10 && full) time += '0' + minutes + minuteSymbol | |
162 | else if (minutes >= 1) time += minutes + minuteSymbol | |
163 | else if (full) time += '00' + minuteSymbol | |
164 | ||
165 | seconds %= 60 | |
166 | if (seconds >= 1 && seconds < 10 && full) time += '0' + seconds + secondsSymbol | |
167 | else if (seconds >= 1) time += seconds + secondsSymbol | |
168 | else if (full) time += '00' | |
169 | ||
170 | return time | |
171 | } | |
172 | ||
173 | function buildVideoOrPlaylistEmbed (embedUrl: string) { | |
174 | return '<iframe width="560" height="315" ' + | |
175 | 'sandbox="allow-same-origin allow-scripts allow-popups" ' + | |
176 | 'src="' + embedUrl + '" ' + | |
177 | 'frameborder="0" allowfullscreen>' + | |
178 | '</iframe>' | |
179 | } | |
180 | ||
181 | function videoFileMaxByResolution (files: VideoFile[]) { | |
182 | let max = files[0] | |
183 | ||
184 | for (let i = 1; i < files.length; i++) { | |
185 | const file = files[i] | |
186 | if (max.resolution.id < file.resolution.id) max = file | |
187 | } | |
188 | ||
189 | return max | |
190 | } | |
191 | ||
192 | function videoFileMinByResolution (files: VideoFile[]) { | |
193 | let min = files[0] | |
194 | ||
195 | for (let i = 1; i < files.length; i++) { | |
196 | const file = files[i] | |
197 | if (min.resolution.id > file.resolution.id) min = file | |
198 | } | |
199 | ||
200 | return min | |
201 | } | |
202 | ||
203 | function getRtcConfig () { | |
204 | return { | |
205 | iceServers: [ | |
206 | { | |
207 | urls: 'stun:stun.stunprotocol.org' | |
208 | }, | |
209 | { | |
210 | urls: 'stun:stun.framasoft.org' | |
211 | } | |
212 | ] | |
213 | } | |
214 | } | |
215 | ||
216 | // --------------------------------------------------------------------------- | |
217 | ||
218 | export { | |
219 | getRtcConfig, | |
220 | toTitleCase, | |
221 | timeToInt, | |
222 | secondsToTime, | |
223 | isWebRTCDisabled, | |
224 | buildPlaylistLink, | |
225 | buildVideoLink, | |
226 | buildVideoOrPlaylistEmbed, | |
227 | videoFileMaxByResolution, | |
228 | videoFileMinByResolution, | |
229 | isMobile, | |
230 | bytes, | |
231 | isIOS, | |
232 | isSafari | |
233 | } |