allow limiting video-comments rss feeds to an account or video channel
[oweals/peertube.git] / client / src / assets / player / p2p-media-loader / redundancy-url-manager.ts
1 import { basename, dirname } from 'path'
2
3 class RedundancyUrlManager {
4
5   constructor (private baseUrls: string[] = []) {
6     // empty
7   }
8
9   removeBySegmentUrl (segmentUrl: string) {
10     console.log('Removing redundancy of segment URL %s.', segmentUrl)
11
12     const baseUrl = dirname(segmentUrl)
13
14     this.baseUrls = this.baseUrls.filter(u => u !== baseUrl && u !== baseUrl + '/')
15   }
16
17   buildUrl (url: string) {
18     const max = this.baseUrls.length + 1
19     const i = this.getRandomInt(max)
20
21     if (i === max - 1) return url
22
23     const newBaseUrl = this.baseUrls[i]
24     const slashPart = newBaseUrl.endsWith('/') ? '' : '/'
25
26     return newBaseUrl + slashPart + basename(url)
27   }
28
29   countBaseUrls () {
30     return this.baseUrls.length
31   }
32
33   private getRandomInt (max: number) {
34     return Math.floor(Math.random() * Math.floor(max))
35   }
36 }
37
38 // ---------------------------------------------------------------------------
39
40 export {
41   RedundancyUrlManager
42 }