]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/live/live-segment-sha-store.ts
Add ability to save replay of permanent lives
[github/Chocobozzz/PeerTube.git] / server / lib / live / live-segment-sha-store.ts
1 import { basename } from 'path'
2 import { logger, loggerTagsFactory } from '@server/helpers/logger'
3 import { buildSha256Segment } from '../hls'
4
5 const lTags = loggerTagsFactory('live')
6
7 class LiveSegmentShaStore {
8
9 private static instance: LiveSegmentShaStore
10
11 private readonly segmentsSha256 = new Map<string, Map<string, string>>()
12
13 private constructor () {
14 }
15
16 getSegmentsSha256 (videoUUID: string) {
17 return this.segmentsSha256.get(videoUUID)
18 }
19
20 async addSegmentSha (videoUUID: string, segmentPath: string) {
21 const segmentName = basename(segmentPath)
22 logger.debug('Adding live sha segment %s.', segmentPath, lTags(videoUUID))
23
24 const shaResult = await buildSha256Segment(segmentPath)
25
26 if (!this.segmentsSha256.has(videoUUID)) {
27 this.segmentsSha256.set(videoUUID, new Map())
28 }
29
30 const filesMap = this.segmentsSha256.get(videoUUID)
31 filesMap.set(segmentName, shaResult)
32 }
33
34 removeSegmentSha (videoUUID: string, segmentPath: string) {
35 const segmentName = basename(segmentPath)
36
37 logger.debug('Removing live sha segment %s.', segmentPath, lTags(videoUUID))
38
39 const filesMap = this.segmentsSha256.get(videoUUID)
40 if (!filesMap) {
41 logger.warn('Unknown files map to remove sha for %s.', videoUUID, lTags(videoUUID))
42 return
43 }
44
45 if (!filesMap.has(segmentName)) {
46 logger.warn('Unknown segment in files map for video %s and segment %s.', videoUUID, segmentPath, lTags(videoUUID))
47 return
48 }
49
50 filesMap.delete(segmentName)
51 }
52
53 cleanupShaSegments (videoUUID: string) {
54 this.segmentsSha256.delete(videoUUID)
55 }
56
57 static get Instance () {
58 return this.instance || (this.instance = new this())
59 }
60 }
61
62 export {
63 LiveSegmentShaStore
64 }