]>
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 | ||
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 | |
9 | const 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 | ] | |
15 | function bytes (value) { | |
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 |
22 | function isMobile () { |
23 | return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent) | |
24 | } | |
25 | ||
11b8762f C |
26 | function 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 | ||
41 | function timeToInt (time: number | string) { | |
42 | if (typeof time === 'number') return time | |
43 | ||
44 | const reg = /^((\d+)h)?((\d+)m)?((\d+)s?)?$/ | |
45 | const matches = time.match(reg) | |
46 | ||
47 | if (!matches) return 0 | |
48 | ||
49 | const hours = parseInt(matches[2] || '0', 10) | |
50 | const minutes = parseInt(matches[4] || '0', 10) | |
51 | const seconds = parseInt(matches[6] || '0', 10) | |
52 | ||
53 | return hours * 3600 + minutes * 60 + seconds | |
54 | } | |
55 | ||
56 | function secondsToTime (seconds: number) { | |
57 | let time = '' | |
58 | ||
59 | let hours = Math.floor(seconds / 3600) | |
60 | if (hours >= 1) time = hours + 'h' | |
61 | ||
62 | seconds %= 3600 | |
63 | let minutes = Math.floor(seconds / 60) | |
64 | if (minutes >= 1) time += minutes + 'm' | |
65 | ||
66 | seconds %= 60 | |
67 | if (seconds >= 1) time += seconds + 's' | |
68 | ||
69 | return time | |
960a11e8 C |
70 | } |
71 | ||
72 | function buildVideoEmbed (embedUrl: string) { | |
73 | return '<iframe width="560" height="315" ' + | |
77540346 | 74 | 'sandbox="allow-same-origin allow-scripts" ' + |
960a11e8 C |
75 | 'src="' + embedUrl + '" ' + |
76 | 'frameborder="0" allowfullscreen>' + | |
77 | '</iframe>' | |
78 | } | |
79 | ||
80 | function copyToClipboard (text: string) { | |
81 | const el = document.createElement('textarea') | |
82 | el.value = text | |
83 | el.setAttribute('readonly', '') | |
84 | el.style.position = 'absolute' | |
85 | el.style.left = '-9999px' | |
86 | document.body.appendChild(el) | |
87 | el.select() | |
88 | document.execCommand('copy') | |
89 | document.body.removeChild(el) | |
90 | } | |
91 | ||
6cca7360 C |
92 | function videoFileMaxByResolution (files: VideoFile[]) { |
93 | let max = files[0] | |
94 | ||
95 | for (let i = 1; i < files.length; i++) { | |
96 | const file = files[i] | |
97 | if (max.resolution.id < file.resolution.id) max = file | |
98 | } | |
99 | ||
100 | return max | |
101 | } | |
102 | ||
103 | function videoFileMinByResolution (files: VideoFile[]) { | |
104 | let min = files[0] | |
105 | ||
106 | for (let i = 1; i < files.length; i++) { | |
107 | const file = files[i] | |
108 | if (min.resolution.id > file.resolution.id) min = file | |
109 | } | |
110 | ||
111 | return min | |
112 | } | |
113 | ||
7b3a99d5 C |
114 | // --------------------------------------------------------------------------- |
115 | ||
c6352f2c C |
116 | export { |
117 | toTitleCase, | |
1f6824c9 | 118 | timeToInt, |
960a11e8 | 119 | buildVideoLink, |
960a11e8 | 120 | buildVideoEmbed, |
6cca7360 C |
121 | videoFileMaxByResolution, |
122 | videoFileMinByResolution, | |
960a11e8 | 123 | copyToClipboard, |
d1bd87e0 | 124 | isMobile, |
c6352f2c C |
125 | bytes |
126 | } |