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'
7 function buildConcatenatedName (segmentOrPlaylistPath: string) {
8 const num = basename(segmentOrPlaylistPath).match(/^(\d+)(-|\.)/)
10 return 'concat-' + num[1] + '.ts'
13 async function cleanupPermanentLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
14 const hlsDirectory = getLiveDirectory(video)
16 await cleanupTMPLiveFiles(hlsDirectory)
18 await streamingPlaylist.destroy()
21 async function cleanupUnsavedNormalLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
22 const hlsDirectory = getLiveDirectory(video)
24 await remove(hlsDirectory)
26 await streamingPlaylist.destroy()
29 async function cleanupTMPLiveFiles (hlsDirectory: string) {
30 if (!await pathExists(hlsDirectory)) return
32 const files = await readdir(hlsDirectory)
34 for (const filename of files) {
36 filename.endsWith('.ts') ||
37 filename.endsWith('.m3u8') ||
38 filename.endsWith('.mpd') ||
39 filename.endsWith('.m4s') ||
40 filename.endsWith('.tmp')
42 const p = join(hlsDirectory, filename)
45 .catch(err => logger.error('Cannot remove %s.', p, { err }))
52 cleanupUnsavedNormalLive,