]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/assets/player/shared/p2p-media-loader/redundancy-url-manager.ts
Don't conflict with alt + num web browser hotkey
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / shared / p2p-media-loader / redundancy-url-manager.ts
CommitLineData
da332417 1import { basename, dirname } from 'path'
42b40636 2import { logger } from '@root-helpers/logger'
da332417
C
3
4class RedundancyUrlManager {
5
da332417
C
6 constructor (private baseUrls: string[] = []) {
7 // empty
8 }
9
10 removeBySegmentUrl (segmentUrl: string) {
42b40636 11 logger.info(`Removing redundancy of segment URL ${segmentUrl}.`)
da332417
C
12
13 const baseUrl = dirname(segmentUrl)
14
15 this.baseUrls = this.baseUrls.filter(u => u !== baseUrl && u !== baseUrl + '/')
16 }
17
da332417 18 buildUrl (url: string) {
da332417
C
19 const max = this.baseUrls.length + 1
20 const i = this.getRandomInt(max)
21
22 if (i === max - 1) return url
23
24 const newBaseUrl = this.baseUrls[i]
25 const slashPart = newBaseUrl.endsWith('/') ? '' : '/'
26
b82df0a3 27 return newBaseUrl + slashPart + basename(url)
da332417
C
28 }
29
30 countBaseUrls () {
31 return this.baseUrls.length
32 }
33
34 private getRandomInt (max: number) {
35 return Math.floor(Math.random() * Math.floor(max))
36 }
37}
38
39// ---------------------------------------------------------------------------
40
41export {
42 RedundancyUrlManager
43}