1 import { Video, VideoFile, VideoPlaylist } from '@shared/models'
2 import { escapeHTML } from '@shared/core-utils/renderer'
4 function toTitleCase (str: string) {
5 return str.charAt(0).toUpperCase() + str.slice(1)
8 function isWebRTCDisabled () {
9 return !!((window as any).RTCPeerConnection || (window as any).mozRTCPeerConnection || (window as any).webkitRTCPeerConnection) === false
13 if (/iPad|iPhone|iPod/.test(navigator.platform)) {
17 // Detect iPad Desktop mode
18 return !!(navigator.maxTouchPoints &&
19 navigator.maxTouchPoints > 2 &&
20 /MacIntel/.test(navigator.platform))
23 function isSafari () {
24 return /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
27 // https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts
28 // Don't import all Angular stuff, just copy the code with shame
29 const dictionaryBytes: Array<{max: number, type: string}> = [
30 { max: 1024, type: 'B' },
31 { max: 1048576, type: 'KB' },
32 { max: 1073741824, type: 'MB' },
33 { max: 1.0995116e12, type: 'GB' }
35 function bytes (value: number) {
36 const format = dictionaryBytes.find(d => value < d.max) || dictionaryBytes[dictionaryBytes.length - 1]
37 const calc = Math.floor(value / (format.max / 1024)).toString()
39 return [ calc, format.type ]
42 function isMobile () {
43 return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent)
46 function buildPlaylistLink (playlist: Pick<VideoPlaylist, 'shortUUID'>, base?: string) {
47 return (base ?? window.location.origin) + '/w/p/' + playlist.shortUUID
50 function buildVideoLink (video: Pick<Video, 'shortUUID'>, base?: string) {
51 return (base ?? window.location.origin) + '/w/' + video.shortUUID
54 function buildPlaylistEmbedLink (playlist: Pick<VideoPlaylist, 'uuid'>, base?: string) {
55 return (base ?? window.location.origin) + '/video-playlists/embed/' + playlist.uuid
58 function buildVideoEmbedLink (video: Pick<Video, 'uuid'>, base?: string) {
59 return (base ?? window.location.origin) + '/videos/embed/' + video.uuid
62 function decorateVideoLink (options: {
76 warningTitle?: boolean
78 peertubeLink?: boolean
80 const { url } = options
82 const params = generateParams(window.location.search)
84 if (options.startTime !== undefined && options.startTime !== null) {
85 const startTimeInt = Math.floor(options.startTime)
86 params.set('start', secondsToTime(startTimeInt))
89 if (options.stopTime) {
90 const stopTimeInt = Math.floor(options.stopTime)
91 params.set('stop', secondsToTime(stopTimeInt))
94 if (options.subtitle) params.set('subtitle', options.subtitle)
96 if (options.loop === true) params.set('loop', '1')
97 if (options.autoplay === true) params.set('autoplay', '1')
98 if (options.muted === true) params.set('muted', '1')
99 if (options.title === false) params.set('title', '0')
100 if (options.warningTitle === false) params.set('warningTitle', '0')
101 if (options.controls === false) params.set('controls', '0')
102 if (options.peertubeLink === false) params.set('peertubeLink', '0')
104 return buildUrl(url, params)
107 function decoratePlaylistLink (options: {
110 playlistPosition?: number
112 const { url } = options
114 const params = generateParams(window.location.search)
116 if (options.playlistPosition) params.set('playlistPosition', '' + options.playlistPosition)
118 return buildUrl(url, params)
121 function buildUrl (url: string, params: URLSearchParams) {
122 let hasParams = false
123 params.forEach(() => hasParams = true)
125 if (hasParams) return url + '?' + params.toString()
130 function generateParams (url: string) {
131 const params = new URLSearchParams(window.location.search)
132 // Unused parameters in embed
133 params.delete('videoId')
134 params.delete('resume')
139 function timeToInt (time: number | string) {
141 if (typeof time === 'number') return time
143 const reg = /^((\d+)[h:])?((\d+)[m:])?((\d+)s?)?$/
144 const matches = time.match(reg)
146 if (!matches) return 0
148 const hours = parseInt(matches[2] || '0', 10)
149 const minutes = parseInt(matches[4] || '0', 10)
150 const seconds = parseInt(matches[6] || '0', 10)
152 return hours * 3600 + minutes * 60 + seconds
155 function secondsToTime (seconds: number, full = false, symbol?: string) {
158 if (seconds === 0 && !full) return '0s'
160 const hourSymbol = (symbol || 'h')
161 const minuteSymbol = (symbol || 'm')
162 const secondsSymbol = full ? '' : 's'
164 const hours = Math.floor(seconds / 3600)
165 if (hours >= 1) time = hours + hourSymbol
166 else if (full) time = '0' + hourSymbol
169 const minutes = Math.floor(seconds / 60)
170 if (minutes >= 1 && minutes < 10 && full) time += '0' + minutes + minuteSymbol
171 else if (minutes >= 1) time += minutes + minuteSymbol
172 else if (full) time += '00' + minuteSymbol
175 if (seconds >= 1 && seconds < 10 && full) time += '0' + seconds + secondsSymbol
176 else if (seconds >= 1) time += seconds + secondsSymbol
177 else if (full) time += '00'
182 function buildVideoOrPlaylistEmbed (embedUrl: string, embedTitle: string) {
183 const title = escapeHTML(embedTitle)
184 return '<iframe width="560" height="315" ' +
185 'sandbox="allow-same-origin allow-scripts allow-popups" ' +
186 'title="' + title + '" ' +
187 'src="' + embedUrl + '" ' +
188 'frameborder="0" allowfullscreen>' +
192 function videoFileMaxByResolution (files: VideoFile[]) {
195 for (let i = 1; i < files.length; i++) {
196 const file = files[i]
197 if (max.resolution.id < file.resolution.id) max = file
203 function videoFileMinByResolution (files: VideoFile[]) {
206 for (let i = 1; i < files.length; i++) {
207 const file = files[i]
208 if (min.resolution.id > file.resolution.id) min = file
214 function getRtcConfig () {
218 urls: 'stun:stun.stunprotocol.org'
221 urls: 'stun:stun.framasoft.org'
227 // ---------------------------------------------------------------------------
239 decoratePlaylistLink,
240 buildPlaylistEmbedLink,
243 buildVideoOrPlaylistEmbed,
244 videoFileMaxByResolution,
245 videoFileMinByResolution,