]> 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 import { LiveSegmentShaStore } from './live-segment-sha-store'
7
8 function buildConcatenatedName (segmentOrPlaylistPath: string) {
9 const num = basename(segmentOrPlaylistPath).match(/^(\d+)(-|\.)/)
10
11 return 'concat-' + num[1] + '.ts'
12 }
13
14 async function cleanupPermanentLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
15 await cleanupTMPLiveFiles(video)
16
17 await streamingPlaylist.destroy()
18 }
19
20 async function cleanupUnsavedNormalLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
21 const hlsDirectory = getLiveDirectory(video)
22
23 await remove(hlsDirectory)
24
25 await streamingPlaylist.destroy()
26
27 LiveSegmentShaStore.Instance.cleanupShaSegments(video.uuid)
28 }
29
30 async function cleanupTMPLiveFiles (video: MVideo) {
31 const hlsDirectory = getLiveDirectory(video)
32
33 LiveSegmentShaStore.Instance.cleanupShaSegments(video.uuid)
34
35 if (!await pathExists(hlsDirectory)) return
36
37 logger.info('Cleanup TMP live files of %s.', hlsDirectory)
38
39 const files = await readdir(hlsDirectory)
40
41 for (const filename of files) {
42 if (
43 filename.endsWith('.ts') ||
44 filename.endsWith('.m3u8') ||
45 filename.endsWith('.mpd') ||
46 filename.endsWith('.m4s') ||
47 filename.endsWith('.tmp')
48 ) {
49 const p = join(hlsDirectory, filename)
50
51 remove(p)
52 .catch(err => logger.error('Cannot remove %s.', p, { err }))
53 }
54 }
55 }
56
57 export {
58 cleanupPermanentLive,
59 cleanupUnsavedNormalLive,
60 cleanupTMPLiveFiles,
61 buildConcatenatedName
62 }