]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/live/live-utils.ts
Reduce lazy static error logs
[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'
92083e42 6import { LiveSegmentShaStore } from './live-segment-sha-store'
8ebf2a5d
C
7
8function buildConcatenatedName (segmentOrPlaylistPath: string) {
9 const num = basename(segmentOrPlaylistPath).match(/^(\d+)(-|\.)/)
10
11 return 'concat-' + num[1] + '.ts'
12}
13
53023be3 14async function cleanupPermanentLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
92083e42 15 await cleanupTMPLiveFiles(video)
5333788c 16
53023be3 17 await streamingPlaylist.destroy()
5333788c
C
18}
19
53023be3 20async function cleanupUnsavedNormalLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
0305db28 21 const hlsDirectory = getLiveDirectory(video)
8ebf2a5d
C
22
23 await remove(hlsDirectory)
24
53023be3 25 await streamingPlaylist.destroy()
92083e42
C
26
27 LiveSegmentShaStore.Instance.cleanupShaSegments(video.uuid)
8ebf2a5d
C
28}
29
92083e42
C
30async function cleanupTMPLiveFiles (video: MVideo) {
31 const hlsDirectory = getLiveDirectory(video)
32
33 LiveSegmentShaStore.Instance.cleanupShaSegments(video.uuid)
34
5333788c
C
35 if (!await pathExists(hlsDirectory)) return
36
92083e42
C
37 logger.info('Cleanup TMP live files of %s.', hlsDirectory)
38
5333788c
C
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
8ebf2a5d 57export {
5333788c 58 cleanupPermanentLive,
53023be3 59 cleanupUnsavedNormalLive,
5333788c 60 cleanupTMPLiveFiles,
8ebf2a5d
C
61 buildConcatenatedName
62}