aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/live/live-segment-sha-store.ts
blob: 4af6f3ebfdfaba48cc530e91ba007852690b762f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { basename } from 'path'
import { logger, loggerTagsFactory } from '@server/helpers/logger'
import { buildSha256Segment } from '../hls'

const lTags = loggerTagsFactory('live')

class LiveSegmentShaStore {

  private static instance: LiveSegmentShaStore

  private readonly segmentsSha256 = new Map<string, Map<string, string>>()

  private constructor () {
  }

  getSegmentsSha256 (videoUUID: string) {
    return this.segmentsSha256.get(videoUUID)
  }

  async addSegmentSha (videoUUID: string, segmentPath: string) {
    const segmentName = basename(segmentPath)
    logger.debug('Adding live sha segment %s.', segmentPath, lTags(videoUUID))

    const shaResult = await buildSha256Segment(segmentPath)

    if (!this.segmentsSha256.has(videoUUID)) {
      this.segmentsSha256.set(videoUUID, new Map())
    }

    const filesMap = this.segmentsSha256.get(videoUUID)
    filesMap.set(segmentName, shaResult)
  }

  removeSegmentSha (videoUUID: string, segmentPath: string) {
    const segmentName = basename(segmentPath)

    logger.debug('Removing live sha segment %s.', segmentPath, lTags(videoUUID))

    const filesMap = this.segmentsSha256.get(videoUUID)
    if (!filesMap) {
      logger.warn('Unknown files map to remove sha for %s.', videoUUID, lTags(videoUUID))
      return
    }

    if (!filesMap.has(segmentName)) {
      logger.warn('Unknown segment in files map for video %s and segment %s.', videoUUID, segmentPath, lTags(videoUUID))
      return
    }

    filesMap.delete(segmentName)
  }

  cleanupShaSegments (videoUUID: string) {
    this.segmentsSha256.delete(videoUUID)
  }

  static get Instance () {
    return this.instance || (this.instance = new this())
  }
}

export {
  LiveSegmentShaStore
}