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'
8 function buildConcatenatedName (segmentOrPlaylistPath: string) {
9 const num = basename(segmentOrPlaylistPath).match(/^(\d+)(-|\.)/)
11 return 'concat-' + num[1] + '.ts'
14 async function cleanupPermanentLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
15 await cleanupTMPLiveFiles(video)
17 await streamingPlaylist.destroy()
20 async function cleanupUnsavedNormalLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
21 const hlsDirectory = getLiveDirectory(video)
23 await remove(hlsDirectory)
25 await streamingPlaylist.destroy()
27 LiveSegmentShaStore.Instance.cleanupShaSegments(video.uuid)
30 async function cleanupTMPLiveFiles (video: MVideo) {
31 const hlsDirectory = getLiveDirectory(video)
33 LiveSegmentShaStore.Instance.cleanupShaSegments(video.uuid)
35 if (!await pathExists(hlsDirectory)) return
37 logger.info('Cleanup TMP live files of %s.', hlsDirectory)
39 const files = await readdir(hlsDirectory)
41 for (const filename of files) {
43 filename.endsWith('.ts') ||
44 filename.endsWith('.m3u8') ||
45 filename.endsWith('.mpd') ||
46 filename.endsWith('.m4s') ||
47 filename.endsWith('.tmp')
49 const p = join(hlsDirectory, filename)
52 .catch(err => logger.error('Cannot remove %s.', p, { err }))
59 cleanupUnsavedNormalLive,