]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/live/live-utils.ts
Merge branch 'release/4.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / live / live-utils.ts
1 import { pathExists, readdir, remove } from 'fs-extra'
2 import { basename, join } from 'path'
3 import { logger } from '@server/helpers/logger'
4 import { MStreamingPlaylist, MVideo } from '@server/types/models'
5 import { getLiveDirectory } from '../paths'
6
7 function buildConcatenatedName (segmentOrPlaylistPath: string) {
8 const num = basename(segmentOrPlaylistPath).match(/^(\d+)(-|\.)/)
9
10 return 'concat-' + num[1] + '.ts'
11 }
12
13 async function cleanupPermanentLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
14 const hlsDirectory = getLiveDirectory(video)
15
16 await cleanupTMPLiveFiles(hlsDirectory)
17
18 await streamingPlaylist.destroy()
19 }
20
21 async function cleanupUnsavedNormalLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
22 const hlsDirectory = getLiveDirectory(video)
23
24 await remove(hlsDirectory)
25
26 await streamingPlaylist.destroy()
27 }
28
29 async 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
50 export {
51 cleanupPermanentLive,
52 cleanupUnsavedNormalLive,
53 cleanupTMPLiveFiles,
54 buildConcatenatedName
55 }