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'
|
2022-10-04 10:03:17 +02:00
|
|
|
import { MStreamingPlaylist, MStreamingPlaylistVideo, MVideo } from '@server/types/models'
|
|
|
|
import { VideoStorage } from '@shared/models'
|
2022-10-25 14:18:59 +02:00
|
|
|
import { listHLSFileKeysOf, removeHLSFileObjectStorageByFullKey, removeHLSObjectStorage } from '../object-storage'
|
2021-08-17 08:26:20 +02:00
|
|
|
import { getLiveDirectory } from '../paths'
|
2021-06-16 15:14:41 +02:00
|
|
|
|
|
|
|
function buildConcatenatedName (segmentOrPlaylistPath: string) {
|
|
|
|
const num = basename(segmentOrPlaylistPath).match(/^(\d+)(-|\.)/)
|
|
|
|
|
|
|
|
return 'concat-' + num[1] + '.ts'
|
|
|
|
}
|
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
async function cleanupAndDestroyPermanentLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
|
|
|
|
await cleanupTMPLiveFiles(video, streamingPlaylist)
|
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
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
// We uploaded files to object storage too, remove them
|
|
|
|
if (streamingPlaylist.storage === VideoStorage.OBJECT_STORAGE) {
|
|
|
|
await removeHLSObjectStorage(streamingPlaylist.withVideo(video))
|
|
|
|
}
|
|
|
|
|
2021-06-16 15:14:41 +02:00
|
|
|
await remove(hlsDirectory)
|
|
|
|
|
2022-06-23 10:29:43 +02:00
|
|
|
await streamingPlaylist.destroy()
|
2022-10-04 10:03:17 +02:00
|
|
|
}
|
2022-06-24 09:34:56 +02:00
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
async function cleanupTMPLiveFiles (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
|
|
|
|
await cleanupTMPLiveFilesFromObjectStorage(streamingPlaylist.withVideo(video))
|
|
|
|
|
|
|
|
await cleanupTMPLiveFilesFromFilesystem(video)
|
2021-06-16 15:14:41 +02:00
|
|
|
}
|
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
export {
|
|
|
|
cleanupAndDestroyPermanentLive,
|
|
|
|
cleanupUnsavedNormalLive,
|
|
|
|
cleanupTMPLiveFiles,
|
|
|
|
buildConcatenatedName
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
2022-06-24 09:34:56 +02:00
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
function isTMPLiveFile (name: string) {
|
|
|
|
return name.endsWith('.ts') ||
|
|
|
|
name.endsWith('.m3u8') ||
|
|
|
|
name.endsWith('.json') ||
|
|
|
|
name.endsWith('.mpd') ||
|
|
|
|
name.endsWith('.m4s') ||
|
|
|
|
name.endsWith('.tmp')
|
|
|
|
}
|
|
|
|
|
|
|
|
async function cleanupTMPLiveFilesFromFilesystem (video: MVideo) {
|
|
|
|
const hlsDirectory = getLiveDirectory(video)
|
2022-06-24 09:34:56 +02:00
|
|
|
|
2022-05-25 14:54:16 +02:00
|
|
|
if (!await pathExists(hlsDirectory)) return
|
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
logger.info('Cleanup TMP live files from filesystem of %s.', hlsDirectory)
|
2022-06-24 09:34:56 +02:00
|
|
|
|
2022-05-25 14:54:16 +02:00
|
|
|
const files = await readdir(hlsDirectory)
|
|
|
|
|
|
|
|
for (const filename of files) {
|
2022-10-04 10:03:17 +02:00
|
|
|
if (isTMPLiveFile(filename)) {
|
2022-05-25 14:54:16 +02:00
|
|
|
const p = join(hlsDirectory, filename)
|
|
|
|
|
|
|
|
remove(p)
|
|
|
|
.catch(err => logger.error('Cannot remove %s.', p, { err }))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
async function cleanupTMPLiveFilesFromObjectStorage (streamingPlaylist: MStreamingPlaylistVideo) {
|
|
|
|
if (streamingPlaylist.storage !== VideoStorage.OBJECT_STORAGE) return
|
|
|
|
|
2022-10-25 14:18:59 +02:00
|
|
|
logger.info('Cleanup TMP live files from object storage for %s.', streamingPlaylist.Video.uuid)
|
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
const keys = await listHLSFileKeysOf(streamingPlaylist)
|
|
|
|
|
|
|
|
for (const key of keys) {
|
|
|
|
if (isTMPLiveFile(key)) {
|
2022-10-25 14:18:59 +02:00
|
|
|
await removeHLSFileObjectStorageByFullKey(key)
|
2022-10-04 10:03:17 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-16 15:14:41 +02:00
|
|
|
}
|