]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/core-utils/common/url.ts
Support reinjecting token in private m3u8 playlist
[github/Chocobozzz/PeerTube.git] / shared / core-utils / common / url.ts
CommitLineData
15a7eafb
C
1import { Video, VideoPlaylist } from '../../models'
2import { secondsToTime } from './date'
3
3545e72c
C
4function addQueryParams (url: string, params: { [ id: string ]: string }) {
5 const objUrl = new URL(url)
6
7 for (const key of Object.keys(params)) {
8 objUrl.searchParams.append(key, params[key])
9 }
10
11 return objUrl.toString()
12}
13
71e3e879
C
14function removeQueryParams (url: string) {
15 const objUrl = new URL(url)
16
17 objUrl.searchParams.forEach((_v, k) => objUrl.searchParams.delete(k))
18
19 return objUrl.toString()
20}
21
15a7eafb
C
22function buildPlaylistLink (playlist: Pick<VideoPlaylist, 'shortUUID'>, base?: string) {
23 return (base ?? window.location.origin) + buildPlaylistWatchPath(playlist)
24}
25
26function buildPlaylistWatchPath (playlist: Pick<VideoPlaylist, 'shortUUID'>) {
27 return '/w/p/' + playlist.shortUUID
28}
29
30function buildVideoWatchPath (video: Pick<Video, 'shortUUID'>) {
31 return '/w/' + video.shortUUID
32}
33
34function buildVideoLink (video: Pick<Video, 'shortUUID'>, base?: string) {
35 return (base ?? window.location.origin) + buildVideoWatchPath(video)
36}
37
38function buildPlaylistEmbedPath (playlist: Pick<VideoPlaylist, 'uuid'>) {
39 return '/video-playlists/embed/' + playlist.uuid
40}
41
42function buildPlaylistEmbedLink (playlist: Pick<VideoPlaylist, 'uuid'>, base?: string) {
43 return (base ?? window.location.origin) + buildPlaylistEmbedPath(playlist)
44}
45
46function buildVideoEmbedPath (video: Pick<Video, 'uuid'>) {
47 return '/videos/embed/' + video.uuid
48}
49
50function buildVideoEmbedLink (video: Pick<Video, 'uuid'>, base?: string) {
51 return (base ?? window.location.origin) + buildVideoEmbedPath(video)
52}
53
54function decorateVideoLink (options: {
55 url: string
56
57 startTime?: number
58 stopTime?: number
59
60 subtitle?: string
61
62 loop?: boolean
63 autoplay?: boolean
64 muted?: boolean
65
66 // Embed options
67 title?: boolean
68 warningTitle?: boolean
60f013e1 69
15a7eafb 70 controls?: boolean
60f013e1
C
71 controlBar?: boolean
72
15a7eafb 73 peertubeLink?: boolean
85302118 74 p2p?: boolean
15a7eafb
C
75}) {
76 const { url } = options
77
bdb1dfc1 78 const params = new URLSearchParams()
15a7eafb
C
79
80 if (options.startTime !== undefined && options.startTime !== null) {
81 const startTimeInt = Math.floor(options.startTime)
82 params.set('start', secondsToTime(startTimeInt))
83 }
84
85 if (options.stopTime) {
86 const stopTimeInt = Math.floor(options.stopTime)
87 params.set('stop', secondsToTime(stopTimeInt))
88 }
89
90 if (options.subtitle) params.set('subtitle', options.subtitle)
91
92 if (options.loop === true) params.set('loop', '1')
93 if (options.autoplay === true) params.set('autoplay', '1')
94 if (options.muted === true) params.set('muted', '1')
95 if (options.title === false) params.set('title', '0')
96 if (options.warningTitle === false) params.set('warningTitle', '0')
60f013e1 97
15a7eafb 98 if (options.controls === false) params.set('controls', '0')
60f013e1
C
99 if (options.controlBar === false) params.set('controlBar', '0')
100
15a7eafb 101 if (options.peertubeLink === false) params.set('peertubeLink', '0')
85302118 102 if (options.p2p !== undefined) params.set('p2p', options.p2p ? '1' : '0')
15a7eafb
C
103
104 return buildUrl(url, params)
105}
106
107function decoratePlaylistLink (options: {
108 url: string
109
110 playlistPosition?: number
111}) {
112 const { url } = options
113
bdb1dfc1 114 const params = new URLSearchParams()
15a7eafb
C
115
116 if (options.playlistPosition) params.set('playlistPosition', '' + options.playlistPosition)
117
118 return buildUrl(url, params)
119}
120
121// ---------------------------------------------------------------------------
122
123export {
3545e72c 124 addQueryParams,
71e3e879 125 removeQueryParams,
3545e72c 126
15a7eafb
C
127 buildPlaylistLink,
128 buildVideoLink,
129
130 buildVideoWatchPath,
131 buildPlaylistWatchPath,
132
133 buildPlaylistEmbedPath,
134 buildVideoEmbedPath,
135
136 buildPlaylistEmbedLink,
137 buildVideoEmbedLink,
138
139 decorateVideoLink,
140 decoratePlaylistLink
141}
142
143function buildUrl (url: string, params: URLSearchParams) {
144 let hasParams = false
145 params.forEach(() => { hasParams = true })
146
147 if (hasParams) return url + '?' + params.toString()
148
149 return url
150}