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