aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/live/live-segment-sha-store.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-06-16 15:14:41 +0200
committerChocobozzz <me@florianbigard.com>2021-06-17 08:47:03 +0200
commit8ebf2a5d5d126e6ef9b89109124adf2a5e9e293d (patch)
treeeee3ef972943be629519c2dc97928878dbd2d732 /server/lib/live/live-segment-sha-store.ts
parentfd6584844b070cfc283902aba26f2853473e83d1 (diff)
downloadPeerTube-8ebf2a5d5d126e6ef9b89109124adf2a5e9e293d.tar.gz
PeerTube-8ebf2a5d5d126e6ef9b89109124adf2a5e9e293d.tar.zst
PeerTube-8ebf2a5d5d126e6ef9b89109124adf2a5e9e293d.zip
Refactor live manager
Diffstat (limited to 'server/lib/live/live-segment-sha-store.ts')
-rw-r--r--server/lib/live/live-segment-sha-store.ts64
1 files changed, 64 insertions, 0 deletions
diff --git a/server/lib/live/live-segment-sha-store.ts b/server/lib/live/live-segment-sha-store.ts
new file mode 100644
index 000000000..4af6f3ebf
--- /dev/null
+++ b/server/lib/live/live-segment-sha-store.ts
@@ -0,0 +1,64 @@
1import { basename } from 'path'
2import { logger, loggerTagsFactory } from '@server/helpers/logger'
3import { buildSha256Segment } from '../hls'
4
5const lTags = loggerTagsFactory('live')
6
7class 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
62export {
63 LiveSegmentShaStore
64}