]>
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 | ||
960a11e8 | 26 | function buildVideoLink (time?: number) { |
1f6824c9 C |
27 | const baseEmbedPath = window.location.pathname.replace('/embed/', '/watch/') |
28 | const baseEmbedURL = window.location.origin + baseEmbedPath | |
29 | ||
960a11e8 C |
30 | if (time) { |
31 | const timeInt = Math.floor(time) | |
32 | ||
1f6824c9 C |
33 | const params = new URLSearchParams(window.location.search) |
34 | params.set('start', secondsToTime(timeInt)) | |
35 | ||
36 | return baseEmbedURL + '?' + params.toString() | |
960a11e8 C |
37 | } |
38 | ||
1f6824c9 C |
39 | return baseEmbedURL |
40 | } | |
41 | ||
42 | function timeToInt (time: number | string) { | |
43 | if (typeof time === 'number') return time | |
44 | ||
45 | const reg = /^((\d+)h)?((\d+)m)?((\d+)s?)?$/ | |
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 | ||
57 | function secondsToTime (seconds: number) { | |
58 | let time = '' | |
59 | ||
60 | let hours = Math.floor(seconds / 3600) | |
61 | if (hours >= 1) time = hours + 'h' | |
62 | ||
63 | seconds %= 3600 | |
64 | let minutes = Math.floor(seconds / 60) | |
65 | if (minutes >= 1) time += minutes + 'm' | |
66 | ||
67 | seconds %= 60 | |
68 | if (seconds >= 1) time += seconds + 's' | |
69 | ||
70 | return time | |
960a11e8 C |
71 | } |
72 | ||
73 | function buildVideoEmbed (embedUrl: string) { | |
74 | return '<iframe width="560" height="315" ' + | |
77540346 | 75 | 'sandbox="allow-same-origin allow-scripts" ' + |
960a11e8 C |
76 | 'src="' + embedUrl + '" ' + |
77 | 'frameborder="0" allowfullscreen>' + | |
78 | '</iframe>' | |
79 | } | |
80 | ||
81 | function copyToClipboard (text: string) { | |
82 | const el = document.createElement('textarea') | |
83 | el.value = text | |
84 | el.setAttribute('readonly', '') | |
85 | el.style.position = 'absolute' | |
86 | el.style.left = '-9999px' | |
87 | document.body.appendChild(el) | |
88 | el.select() | |
89 | document.execCommand('copy') | |
90 | document.body.removeChild(el) | |
91 | } | |
92 | ||
6cca7360 C |
93 | function videoFileMaxByResolution (files: VideoFile[]) { |
94 | let max = files[0] | |
95 | ||
96 | for (let i = 1; i < files.length; i++) { | |
97 | const file = files[i] | |
98 | if (max.resolution.id < file.resolution.id) max = file | |
99 | } | |
100 | ||
101 | return max | |
102 | } | |
103 | ||
104 | function videoFileMinByResolution (files: VideoFile[]) { | |
105 | let min = files[0] | |
106 | ||
107 | for (let i = 1; i < files.length; i++) { | |
108 | const file = files[i] | |
109 | if (min.resolution.id > file.resolution.id) min = file | |
110 | } | |
111 | ||
112 | return min | |
113 | } | |
114 | ||
7b3a99d5 C |
115 | // --------------------------------------------------------------------------- |
116 | ||
c6352f2c C |
117 | export { |
118 | toTitleCase, | |
1f6824c9 | 119 | timeToInt, |
960a11e8 | 120 | buildVideoLink, |
960a11e8 | 121 | buildVideoEmbed, |
6cca7360 C |
122 | videoFileMaxByResolution, |
123 | videoFileMinByResolution, | |
960a11e8 | 124 | copyToClipboard, |
d1bd87e0 | 125 | isMobile, |
c6352f2c C |
126 | bytes |
127 | } |