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