]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/live/live-segment-sha-store.ts
Use private ACL for private videos in s3
[github/Chocobozzz/PeerTube.git] / server / lib / live / live-segment-sha-store.ts
CommitLineData
cfd57d2c 1import { writeJson } from 'fs-extra'
8ebf2a5d 2import { basename } from 'path'
cfd57d2c 3import { mapToJSON } from '@server/helpers/core-utils'
8ebf2a5d 4import { logger, loggerTagsFactory } from '@server/helpers/logger'
cfd57d2c 5import { MStreamingPlaylistVideo } from '@server/types/models'
8ebf2a5d 6import { buildSha256Segment } from '../hls'
cfd57d2c 7import { storeHLSFileFromPath } from '../object-storage'
9ab330b9 8import PQueue from 'p-queue'
8ebf2a5d
C
9
10const lTags = loggerTagsFactory('live')
11
12class LiveSegmentShaStore {
13
cfd57d2c
C
14 private readonly segmentsSha256 = new Map<string, string>()
15
16 private readonly videoUUID: string
17 private readonly sha256Path: string
18 private readonly streamingPlaylist: MStreamingPlaylistVideo
19 private readonly sendToObjectStorage: boolean
9ab330b9 20 private readonly writeQueue = new PQueue({ concurrency: 1 })
cfd57d2c
C
21
22 constructor (options: {
23 videoUUID: string
24 sha256Path: string
25 streamingPlaylist: MStreamingPlaylistVideo
26 sendToObjectStorage: boolean
27 }) {
28 this.videoUUID = options.videoUUID
29 this.sha256Path = options.sha256Path
30 this.streamingPlaylist = options.streamingPlaylist
31 this.sendToObjectStorage = options.sendToObjectStorage
8ebf2a5d
C
32 }
33
cfd57d2c
C
34 async addSegmentSha (segmentPath: string) {
35 logger.debug('Adding live sha segment %s.', segmentPath, lTags(this.videoUUID))
8ebf2a5d
C
36
37 const shaResult = await buildSha256Segment(segmentPath)
38
cfd57d2c
C
39 const segmentName = basename(segmentPath)
40 this.segmentsSha256.set(segmentName, shaResult)
8ebf2a5d 41
9ab330b9
C
42 try {
43 await this.writeToDisk()
44 } catch (err) {
45 logger.error('Cannot write sha segments to disk.', { err })
46 }
8ebf2a5d
C
47 }
48
cfd57d2c 49 async removeSegmentSha (segmentPath: string) {
8ebf2a5d
C
50 const segmentName = basename(segmentPath)
51
cfd57d2c 52 logger.debug('Removing live sha segment %s.', segmentPath, lTags(this.videoUUID))
8ebf2a5d 53
cfd57d2c
C
54 if (!this.segmentsSha256.has(segmentName)) {
55 logger.warn('Unknown segment in files map for video %s and segment %s.', this.videoUUID, segmentPath, lTags(this.videoUUID))
8ebf2a5d
C
56 return
57 }
58
cfd57d2c 59 this.segmentsSha256.delete(segmentName)
8ebf2a5d 60
cfd57d2c 61 await this.writeToDisk()
8ebf2a5d
C
62 }
63
9ab330b9
C
64 private writeToDisk () {
65 return this.writeQueue.add(async () => {
66 await writeJson(this.sha256Path, mapToJSON(this.segmentsSha256))
8ebf2a5d 67
9ab330b9
C
68 if (this.sendToObjectStorage) {
69 const url = await storeHLSFileFromPath(this.streamingPlaylist, this.sha256Path)
cfd57d2c 70
9ab330b9
C
71 if (this.streamingPlaylist.segmentsSha256Url !== url) {
72 this.streamingPlaylist.segmentsSha256Url = url
73 await this.streamingPlaylist.save()
74 }
cfd57d2c 75 }
9ab330b9 76 })
8ebf2a5d
C
77 }
78}
79
80export {
81 LiveSegmentShaStore
82}