1 import { basename } from 'path'
2 import { logger, loggerTagsFactory } from '@server/helpers/logger'
3 import { buildSha256Segment } from '../hls'
5 const lTags = loggerTagsFactory('live')
7 class LiveSegmentShaStore {
9 private static instance: LiveSegmentShaStore
11 private readonly segmentsSha256 = new Map<string, Map<string, string>>()
13 private constructor () {
16 getSegmentsSha256 (videoUUID: string) {
17 return this.segmentsSha256.get(videoUUID)
20 async addSegmentSha (videoUUID: string, segmentPath: string) {
21 const segmentName = basename(segmentPath)
22 logger.debug('Adding live sha segment %s.', segmentPath, lTags(videoUUID))
24 const shaResult = await buildSha256Segment(segmentPath)
26 if (!this.segmentsSha256.has(videoUUID)) {
27 this.segmentsSha256.set(videoUUID, new Map())
30 const filesMap = this.segmentsSha256.get(videoUUID)
31 filesMap.set(segmentName, shaResult)
34 removeSegmentSha (videoUUID: string, segmentPath: string) {
35 const segmentName = basename(segmentPath)
37 logger.debug('Removing live sha segment %s.', segmentPath, lTags(videoUUID))
39 const filesMap = this.segmentsSha256.get(videoUUID)
41 logger.warn('Unknown files map to remove sha for %s.', videoUUID, lTags(videoUUID))
45 if (!filesMap.has(segmentName)) {
46 logger.warn('Unknown segment in files map for video %s and segment %s.', videoUUID, segmentPath, lTags(videoUUID))
50 filesMap.delete(segmentName)
53 cleanupShaSegments (videoUUID: string) {
54 this.segmentsSha256.delete(videoUUID)
57 static get Instance () {
58 return this.instance || (this.instance = new this())