1 import { writeJson } from 'fs-extra'
2 import { basename } from 'path'
3 import { mapToJSON } from '@server/helpers/core-utils'
4 import { logger, loggerTagsFactory } from '@server/helpers/logger'
5 import { MStreamingPlaylistVideo } from '@server/types/models'
6 import { buildSha256Segment } from '../hls'
7 import { storeHLSFileFromPath } from '../object-storage'
8 import PQueue from 'p-queue'
10 const lTags = loggerTagsFactory('live')
12 class LiveSegmentShaStore {
14 private readonly segmentsSha256 = new Map<string, string>()
16 private readonly videoUUID: string
17 private readonly sha256Path: string
18 private readonly streamingPlaylist: MStreamingPlaylistVideo
19 private readonly sendToObjectStorage: boolean
20 private readonly writeQueue = new PQueue({ concurrency: 1 })
22 constructor (options: {
25 streamingPlaylist: MStreamingPlaylistVideo
26 sendToObjectStorage: boolean
28 this.videoUUID = options.videoUUID
29 this.sha256Path = options.sha256Path
30 this.streamingPlaylist = options.streamingPlaylist
31 this.sendToObjectStorage = options.sendToObjectStorage
34 async addSegmentSha (segmentPath: string) {
35 logger.debug('Adding live sha segment %s.', segmentPath, lTags(this.videoUUID))
37 const shaResult = await buildSha256Segment(segmentPath)
39 const segmentName = basename(segmentPath)
40 this.segmentsSha256.set(segmentName, shaResult)
43 await this.writeToDisk()
45 logger.error('Cannot write sha segments to disk.', { err })
49 async removeSegmentSha (segmentPath: string) {
50 const segmentName = basename(segmentPath)
52 logger.debug('Removing live sha segment %s.', segmentPath, lTags(this.videoUUID))
54 if (!this.segmentsSha256.has(segmentName)) {
56 'Unknown segment in live segment hash store for video %s and segment %s.',
57 this.videoUUID, segmentPath, lTags(this.videoUUID)
62 this.segmentsSha256.delete(segmentName)
64 await this.writeToDisk()
67 private writeToDisk () {
68 return this.writeQueue.add(async () => {
69 await writeJson(this.sha256Path, mapToJSON(this.segmentsSha256))
71 if (this.sendToObjectStorage) {
72 const url = await storeHLSFileFromPath(this.streamingPlaylist, this.sha256Path)
74 if (this.streamingPlaylist.segmentsSha256Url !== url) {
75 this.streamingPlaylist.segmentsSha256Url = url
76 await this.streamingPlaylist.save()