]>
Commit | Line | Data |
---|---|---|
5333788c C |
1 | import { pathExists, readdir, remove } from 'fs-extra' |
2 | import { basename, join } from 'path' | |
3 | import { logger } from '@server/helpers/logger' | |
8ebf2a5d | 4 | import { MStreamingPlaylist, MVideo } from '@server/types/models' |
0305db28 | 5 | import { getLiveDirectory } from '../paths' |
8ebf2a5d C |
6 | |
7 | function buildConcatenatedName (segmentOrPlaylistPath: string) { | |
8 | const num = basename(segmentOrPlaylistPath).match(/^(\d+)(-|\.)/) | |
9 | ||
10 | return 'concat-' + num[1] + '.ts' | |
11 | } | |
12 | ||
53023be3 | 13 | async function cleanupPermanentLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) { |
5333788c C |
14 | const hlsDirectory = getLiveDirectory(video) |
15 | ||
16 | await cleanupTMPLiveFiles(hlsDirectory) | |
17 | ||
53023be3 | 18 | await streamingPlaylist.destroy() |
5333788c C |
19 | } |
20 | ||
53023be3 | 21 | async function cleanupUnsavedNormalLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) { |
0305db28 | 22 | const hlsDirectory = getLiveDirectory(video) |
8ebf2a5d C |
23 | |
24 | await remove(hlsDirectory) | |
25 | ||
53023be3 | 26 | await streamingPlaylist.destroy() |
8ebf2a5d C |
27 | } |
28 | ||
5333788c C |
29 | async function cleanupTMPLiveFiles (hlsDirectory: string) { |
30 | if (!await pathExists(hlsDirectory)) return | |
31 | ||
32 | const files = await readdir(hlsDirectory) | |
33 | ||
34 | for (const filename of files) { | |
35 | if ( | |
36 | filename.endsWith('.ts') || | |
37 | filename.endsWith('.m3u8') || | |
38 | filename.endsWith('.mpd') || | |
39 | filename.endsWith('.m4s') || | |
40 | filename.endsWith('.tmp') | |
41 | ) { | |
42 | const p = join(hlsDirectory, filename) | |
43 | ||
44 | remove(p) | |
45 | .catch(err => logger.error('Cannot remove %s.', p, { err })) | |
46 | } | |
47 | } | |
48 | } | |
49 | ||
8ebf2a5d | 50 | export { |
5333788c | 51 | cleanupPermanentLive, |
53023be3 | 52 | cleanupUnsavedNormalLive, |
5333788c | 53 | cleanupTMPLiveFiles, |
8ebf2a5d C |
54 | buildConcatenatedName |
55 | } |