2022-05-25 07:54:16 -05:00
|
|
|
import { pathExists, readdir, remove } from 'fs-extra'
|
|
|
|
import { basename, join } from 'path'
|
|
|
|
import { logger } from '@server/helpers/logger'
|
2021-06-16 08:14:41 -05:00
|
|
|
import { MStreamingPlaylist, MVideo } from '@server/types/models'
|
2021-08-17 01:26:20 -05:00
|
|
|
import { getLiveDirectory } from '../paths'
|
2022-06-24 02:34:56 -05:00
|
|
|
import { LiveSegmentShaStore } from './live-segment-sha-store'
|
2021-06-16 08:14:41 -05:00
|
|
|
|
|
|
|
function buildConcatenatedName (segmentOrPlaylistPath: string) {
|
|
|
|
const num = basename(segmentOrPlaylistPath).match(/^(\d+)(-|\.)/)
|
|
|
|
|
|
|
|
return 'concat-' + num[1] + '.ts'
|
|
|
|
}
|
|
|
|
|
2022-06-23 03:29:43 -05:00
|
|
|
async function cleanupPermanentLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
|
2022-06-24 02:34:56 -05:00
|
|
|
await cleanupTMPLiveFiles(video)
|
2022-05-25 07:54:16 -05:00
|
|
|
|
2022-06-23 03:29:43 -05:00
|
|
|
await streamingPlaylist.destroy()
|
2022-05-25 07:54:16 -05:00
|
|
|
}
|
|
|
|
|
2022-06-23 03:29:43 -05:00
|
|
|
async function cleanupUnsavedNormalLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
|
2021-08-17 01:26:20 -05:00
|
|
|
const hlsDirectory = getLiveDirectory(video)
|
2021-06-16 08:14:41 -05:00
|
|
|
|
|
|
|
await remove(hlsDirectory)
|
|
|
|
|
2022-06-23 03:29:43 -05:00
|
|
|
await streamingPlaylist.destroy()
|
2022-06-24 02:34:56 -05:00
|
|
|
|
|
|
|
LiveSegmentShaStore.Instance.cleanupShaSegments(video.uuid)
|
2021-06-16 08:14:41 -05:00
|
|
|
}
|
|
|
|
|
2022-06-24 02:34:56 -05:00
|
|
|
async function cleanupTMPLiveFiles (video: MVideo) {
|
|
|
|
const hlsDirectory = getLiveDirectory(video)
|
|
|
|
|
|
|
|
LiveSegmentShaStore.Instance.cleanupShaSegments(video.uuid)
|
|
|
|
|
2022-05-25 07:54:16 -05:00
|
|
|
if (!await pathExists(hlsDirectory)) return
|
|
|
|
|
2022-06-24 02:34:56 -05:00
|
|
|
logger.info('Cleanup TMP live files of %s.', hlsDirectory)
|
|
|
|
|
2022-05-25 07:54:16 -05:00
|
|
|
const files = await readdir(hlsDirectory)
|
|
|
|
|
|
|
|
for (const filename of files) {
|
|
|
|
if (
|
|
|
|
filename.endsWith('.ts') ||
|
|
|
|
filename.endsWith('.m3u8') ||
|
|
|
|
filename.endsWith('.mpd') ||
|
|
|
|
filename.endsWith('.m4s') ||
|
|
|
|
filename.endsWith('.tmp')
|
|
|
|
) {
|
|
|
|
const p = join(hlsDirectory, filename)
|
|
|
|
|
|
|
|
remove(p)
|
|
|
|
.catch(err => logger.error('Cannot remove %s.', p, { err }))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 08:14:41 -05:00
|
|
|
export {
|
2022-05-25 07:54:16 -05:00
|
|
|
cleanupPermanentLive,
|
2022-06-23 03:29:43 -05:00
|
|
|
cleanupUnsavedNormalLive,
|
2022-05-25 07:54:16 -05:00
|
|
|
cleanupTMPLiveFiles,
|
2021-06-16 08:14:41 -05:00
|
|
|
buildConcatenatedName
|
|
|
|
}
|