1 import { basename, dirname } from 'path'
2 import { logger } from '@root-helpers/logger'
4 class RedundancyUrlManager {
6 constructor (private baseUrls: string[] = []) {
10 removeBySegmentUrl (segmentUrl: string) {
11 logger.info(`Removing redundancy of segment URL ${segmentUrl}.`)
13 const baseUrl = dirname(segmentUrl)
15 this.baseUrls = this.baseUrls.filter(u => u !== baseUrl && u !== baseUrl + '/')
18 buildUrl (url: string) {
19 const max = this.baseUrls.length + 1
20 const i = this.getRandomInt(max)
22 if (i === max - 1) return url
24 const newBaseUrl = this.baseUrls[i]
25 const slashPart = newBaseUrl.endsWith('/') ? '' : '/'
27 return newBaseUrl + slashPart + basename(url)
31 return this.baseUrls.length
34 private getRandomInt (max: number) {
35 return Math.floor(Math.random() * Math.floor(max))
39 // ---------------------------------------------------------------------------