]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/live/live-utils.ts
More robust quota check
[github/Chocobozzz/PeerTube.git] / server / lib / live / live-utils.ts
... / ...
CommitLineData
1import { pathExists, readdir, remove } from 'fs-extra'
2import { basename, join } from 'path'
3import { logger } from '@server/helpers/logger'
4import { VIDEO_LIVE } from '@server/initializers/constants'
5import { MStreamingPlaylist, MStreamingPlaylistVideo, MVideo } from '@server/types/models'
6import { LiveVideoLatencyMode, VideoStorage } from '@shared/models'
7import { listHLSFileKeysOf, removeHLSFileObjectStorageByFullKey, removeHLSObjectStorage } from '../object-storage'
8import { getLiveDirectory } from '../paths'
9
10function buildConcatenatedName (segmentOrPlaylistPath: string) {
11 const num = basename(segmentOrPlaylistPath).match(/^(\d+)(-|\.)/)
12
13 return 'concat-' + num[1] + '.ts'
14}
15
16async function cleanupAndDestroyPermanentLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
17 await cleanupTMPLiveFiles(video, streamingPlaylist)
18
19 await streamingPlaylist.destroy()
20}
21
22async function cleanupUnsavedNormalLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
23 const hlsDirectory = getLiveDirectory(video)
24
25 // We uploaded files to object storage too, remove them
26 if (streamingPlaylist.storage === VideoStorage.OBJECT_STORAGE) {
27 await removeHLSObjectStorage(streamingPlaylist.withVideo(video))
28 }
29
30 await remove(hlsDirectory)
31
32 await streamingPlaylist.destroy()
33}
34
35async function cleanupTMPLiveFiles (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
36 await cleanupTMPLiveFilesFromObjectStorage(streamingPlaylist.withVideo(video))
37
38 await cleanupTMPLiveFilesFromFilesystem(video)
39}
40
41function getLiveSegmentTime (latencyMode: LiveVideoLatencyMode) {
42 if (latencyMode === LiveVideoLatencyMode.SMALL_LATENCY) {
43 return VIDEO_LIVE.SEGMENT_TIME_SECONDS.SMALL_LATENCY
44 }
45
46 return VIDEO_LIVE.SEGMENT_TIME_SECONDS.DEFAULT_LATENCY
47}
48
49export {
50 cleanupAndDestroyPermanentLive,
51 cleanupUnsavedNormalLive,
52 cleanupTMPLiveFiles,
53 getLiveSegmentTime,
54 buildConcatenatedName
55}
56
57// ---------------------------------------------------------------------------
58
59function isTMPLiveFile (name: string) {
60 return name.endsWith('.ts') ||
61 name.endsWith('.m3u8') ||
62 name.endsWith('.json') ||
63 name.endsWith('.mpd') ||
64 name.endsWith('.m4s') ||
65 name.endsWith('.tmp')
66}
67
68async function cleanupTMPLiveFilesFromFilesystem (video: MVideo) {
69 const hlsDirectory = getLiveDirectory(video)
70
71 if (!await pathExists(hlsDirectory)) return
72
73 logger.info('Cleanup TMP live files from filesystem of %s.', hlsDirectory)
74
75 const files = await readdir(hlsDirectory)
76
77 for (const filename of files) {
78 if (isTMPLiveFile(filename)) {
79 const p = join(hlsDirectory, filename)
80
81 remove(p)
82 .catch(err => logger.error('Cannot remove %s.', p, { err }))
83 }
84 }
85}
86
87async function cleanupTMPLiveFilesFromObjectStorage (streamingPlaylist: MStreamingPlaylistVideo) {
88 if (streamingPlaylist.storage !== VideoStorage.OBJECT_STORAGE) return
89
90 logger.info('Cleanup TMP live files from object storage for %s.', streamingPlaylist.Video.uuid)
91
92 const keys = await listHLSFileKeysOf(streamingPlaylist)
93
94 for (const key of keys) {
95 if (isTMPLiveFile(key)) {
96 await removeHLSFileObjectStorageByFullKey(key)
97 }
98 }
99}