X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Flive%2Flive-utils.ts;h=6365e23db371892b4b14601606587bdefe534eb6;hb=3256771725430ab6fc716e937b88ca2b2b85263b;hp=e4526c7a574fb8fe60a15633309a5c4486cdc168;hpb=8ebf2a5d5d126e6ef9b89109124adf2a5e9e293d;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/live/live-utils.ts b/server/lib/live/live-utils.ts index e4526c7a5..6365e23db 100644 --- a/server/lib/live/live-utils.ts +++ b/server/lib/live/live-utils.ts @@ -1,7 +1,8 @@ -import { remove } from 'fs-extra' -import { basename } from 'path' +import { pathExists, readdir, remove } from 'fs-extra' +import { basename, join } from 'path' +import { logger } from '@server/helpers/logger' import { MStreamingPlaylist, MVideo } from '@server/types/models' -import { getHLSDirectory } from '../video-paths' +import { getLiveDirectory } from '../paths' function buildConcatenatedName (segmentOrPlaylistPath: string) { const num = basename(segmentOrPlaylistPath).match(/^(\d+)(-|\.)/) @@ -9,15 +10,46 @@ function buildConcatenatedName (segmentOrPlaylistPath: string) { return 'concat-' + num[1] + '.ts' } -async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) { - const hlsDirectory = getHLSDirectory(video) +async function cleanupPermanentLive (video: MVideo, streamingPlaylist?: MStreamingPlaylist) { + const hlsDirectory = getLiveDirectory(video) + + await cleanupTMPLiveFiles(hlsDirectory) + + if (streamingPlaylist) await streamingPlaylist.destroy() +} + +async function cleanupNormalLive (video: MVideo, streamingPlaylist?: MStreamingPlaylist) { + const hlsDirectory = getLiveDirectory(video) await remove(hlsDirectory) - await streamingPlaylist.destroy() + if (streamingPlaylist) await streamingPlaylist.destroy() +} + +async function cleanupTMPLiveFiles (hlsDirectory: string) { + if (!await pathExists(hlsDirectory)) return + + 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 })) + } + } } export { - cleanupLive, + cleanupPermanentLive, + cleanupNormalLive, + cleanupTMPLiveFiles, buildConcatenatedName }