]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/assets/player/utils.ts
Add to playlist dropdown
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / utils.ts
CommitLineData
6cca7360 1import { VideoFile } from '../../../../shared/models/videos'
e945b184 2
c6352f2c
C
3function toTitleCase (str: string) {
4 return str.charAt(0).toUpperCase() + str.slice(1)
5}
6
7// https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts
8// Don't import all Angular stuff, just copy the code with shame
9const dictionaryBytes: Array<{max: number, type: string}> = [
10 { max: 1024, type: 'B' },
11 { max: 1048576, type: 'KB' },
12 { max: 1073741824, type: 'MB' },
13 { max: 1.0995116e12, type: 'GB' }
14]
c199c427 15function bytes (value: number) {
c6352f2c
C
16 const format = dictionaryBytes.find(d => value < d.max) || dictionaryBytes[dictionaryBytes.length - 1]
17 const calc = Math.floor(value / (format.max / 1024)).toString()
18
19 return [ calc, format.type ]
20}
21
d1bd87e0
C
22function isMobile () {
23 return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent)
24}
25
11b8762f
C
26function buildVideoLink (time?: number, url?: string) {
27 if (!url) url = window.location.origin + window.location.pathname.replace('/embed/', '/watch/')
1f6824c9 28
960a11e8
C
29 if (time) {
30 const timeInt = Math.floor(time)
31
1f6824c9
C
32 const params = new URLSearchParams(window.location.search)
33 params.set('start', secondsToTime(timeInt))
34
11b8762f 35 return url + '?' + params.toString()
960a11e8
C
36 }
37
11b8762f 38 return url
1f6824c9
C
39}
40
41function timeToInt (time: number | string) {
3b019808 42 if (!time) return 0
1f6824c9
C
43 if (typeof time === 'number') return time
44
f0a39880 45 const reg = /^((\d+)[h:])?((\d+)[m:])?((\d+)s?)?$/
1f6824c9
C
46 const matches = time.match(reg)
47
48 if (!matches) return 0
49
50 const hours = parseInt(matches[2] || '0', 10)
51 const minutes = parseInt(matches[4] || '0', 10)
52 const seconds = parseInt(matches[6] || '0', 10)
53
54 return hours * 3600 + minutes * 60 + seconds
55}
56
f0a39880 57function secondsToTime (seconds: number, full = false, symbol?: string) {
1f6824c9
C
58 let time = ''
59
f0a39880
C
60 const hourSymbol = (symbol || 'h')
61 const minuteSymbol = (symbol || 'm')
62 const secondsSymbol = full ? '' : 's'
63
1f6824c9 64 let hours = Math.floor(seconds / 3600)
f0a39880
C
65 if (hours >= 1) time = hours + hourSymbol
66 else if (full) time = '0' + hourSymbol
1f6824c9
C
67
68 seconds %= 3600
69 let minutes = Math.floor(seconds / 60)
f0a39880
C
70 if (minutes >= 1 && minutes < 10 && full) time += '0' + minutes + minuteSymbol
71 else if (minutes >= 1) time += minutes + minuteSymbol
72 else if (full) time += '00' + minuteSymbol
1f6824c9
C
73
74 seconds %= 60
f0a39880
C
75 if (seconds >= 1 && seconds < 10 && full) time += '0' + seconds + secondsSymbol
76 else if (seconds >= 1) time += seconds + secondsSymbol
77 else if (full) time += '00'
1f6824c9
C
78
79 return time
960a11e8
C
80}
81
82function buildVideoEmbed (embedUrl: string) {
83 return '<iframe width="560" height="315" ' +
77540346 84 'sandbox="allow-same-origin allow-scripts" ' +
960a11e8
C
85 'src="' + embedUrl + '" ' +
86 'frameborder="0" allowfullscreen>' +
87 '</iframe>'
88}
89
90function copyToClipboard (text: string) {
91 const el = document.createElement('textarea')
92 el.value = text
93 el.setAttribute('readonly', '')
94 el.style.position = 'absolute'
95 el.style.left = '-9999px'
96 document.body.appendChild(el)
97 el.select()
98 document.execCommand('copy')
99 document.body.removeChild(el)
100}
101
6cca7360
C
102function videoFileMaxByResolution (files: VideoFile[]) {
103 let max = files[0]
104
105 for (let i = 1; i < files.length; i++) {
106 const file = files[i]
107 if (max.resolution.id < file.resolution.id) max = file
108 }
109
110 return max
111}
112
113function videoFileMinByResolution (files: VideoFile[]) {
114 let min = files[0]
115
116 for (let i = 1; i < files.length; i++) {
117 const file = files[i]
118 if (min.resolution.id > file.resolution.id) min = file
119 }
120
121 return min
122}
123
09209296
C
124function getRtcConfig () {
125 return {
126 iceServers: [
127 {
128 urls: 'stun:stun.stunprotocol.org'
129 },
130 {
131 urls: 'stun:stun.framasoft.org'
132 }
133 ]
134 }
135}
136
7b3a99d5
C
137// ---------------------------------------------------------------------------
138
c6352f2c 139export {
09209296 140 getRtcConfig,
c6352f2c 141 toTitleCase,
1f6824c9 142 timeToInt,
f0a39880 143 secondsToTime,
960a11e8 144 buildVideoLink,
960a11e8 145 buildVideoEmbed,
6cca7360
C
146 videoFileMaxByResolution,
147 videoFileMinByResolution,
960a11e8 148 copyToClipboard,
d1bd87e0 149 isMobile,
c6352f2c
C
150 bytes
151}