]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/live/live-utils.ts
Merge branch 'release/4.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / live / live-utils.ts
CommitLineData
5333788c
C
1import { pathExists, readdir, remove } from 'fs-extra'
2import { basename, join } from 'path'
3import { logger } from '@server/helpers/logger'
8ebf2a5d 4import { MStreamingPlaylist, MVideo } from '@server/types/models'
0305db28 5import { getLiveDirectory } from '../paths'
8ebf2a5d
C
6
7function buildConcatenatedName (segmentOrPlaylistPath: string) {
8 const num = basename(segmentOrPlaylistPath).match(/^(\d+)(-|\.)/)
9
10 return 'concat-' + num[1] + '.ts'
11}
12
53023be3 13async function cleanupPermanentLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
5333788c
C
14 const hlsDirectory = getLiveDirectory(video)
15
16 await cleanupTMPLiveFiles(hlsDirectory)
17
53023be3 18 await streamingPlaylist.destroy()
5333788c
C
19}
20
53023be3 21async function cleanupUnsavedNormalLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
0305db28 22 const hlsDirectory = getLiveDirectory(video)
8ebf2a5d
C
23
24 await remove(hlsDirectory)
25
53023be3 26 await streamingPlaylist.destroy()
8ebf2a5d
C
27}
28
5333788c
C
29async function cleanupTMPLiveFiles (hlsDirectory: string) {
30 if (!await pathExists(hlsDirectory)) return
31
32 const files = await readdir(hlsDirectory)
33
34 for (const filename of files) {
35 if (
36 filename.endsWith('.ts') ||
37 filename.endsWith('.m3u8') ||
38 filename.endsWith('.mpd') ||
39 filename.endsWith('.m4s') ||
40 filename.endsWith('.tmp')
41 ) {
42 const p = join(hlsDirectory, filename)
43
44 remove(p)
45 .catch(err => logger.error('Cannot remove %s.', p, { err }))
46 }
47 }
48}
49
8ebf2a5d 50export {
5333788c 51 cleanupPermanentLive,
53023be3 52 cleanupUnsavedNormalLive,
5333788c 53 cleanupTMPLiveFiles,
8ebf2a5d
C
54 buildConcatenatedName
55}