]>
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' |
92083e42 | 6 | import { LiveSegmentShaStore } from './live-segment-sha-store' |
8ebf2a5d C |
7 | |
8 | function buildConcatenatedName (segmentOrPlaylistPath: string) { | |
9 | const num = basename(segmentOrPlaylistPath).match(/^(\d+)(-|\.)/) | |
10 | ||
11 | return 'concat-' + num[1] + '.ts' | |
12 | } | |
13 | ||
53023be3 | 14 | async function cleanupPermanentLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) { |
92083e42 | 15 | await cleanupTMPLiveFiles(video) |
5333788c | 16 | |
53023be3 | 17 | await streamingPlaylist.destroy() |
5333788c C |
18 | } |
19 | ||
53023be3 | 20 | async function cleanupUnsavedNormalLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) { |
0305db28 | 21 | const hlsDirectory = getLiveDirectory(video) |
8ebf2a5d C |
22 | |
23 | await remove(hlsDirectory) | |
24 | ||
53023be3 | 25 | await streamingPlaylist.destroy() |
92083e42 C |
26 | |
27 | LiveSegmentShaStore.Instance.cleanupShaSegments(video.uuid) | |
8ebf2a5d C |
28 | } |
29 | ||
92083e42 C |
30 | async function cleanupTMPLiveFiles (video: MVideo) { |
31 | const hlsDirectory = getLiveDirectory(video) | |
32 | ||
33 | LiveSegmentShaStore.Instance.cleanupShaSegments(video.uuid) | |
34 | ||
5333788c C |
35 | if (!await pathExists(hlsDirectory)) return |
36 | ||
92083e42 C |
37 | logger.info('Cleanup TMP live files of %s.', hlsDirectory) |
38 | ||
5333788c C |
39 | const files = await readdir(hlsDirectory) |
40 | ||
41 | for (const filename of files) { | |
42 | if ( | |
43 | filename.endsWith('.ts') || | |
44 | filename.endsWith('.m3u8') || | |
45 | filename.endsWith('.mpd') || | |
46 | filename.endsWith('.m4s') || | |
47 | filename.endsWith('.tmp') | |
48 | ) { | |
49 | const p = join(hlsDirectory, filename) | |
50 | ||
51 | remove(p) | |
52 | .catch(err => logger.error('Cannot remove %s.', p, { err })) | |
53 | } | |
54 | } | |
55 | } | |
56 | ||
8ebf2a5d | 57 | export { |
5333788c | 58 | cleanupPermanentLive, |
53023be3 | 59 | cleanupUnsavedNormalLive, |
5333788c | 60 | cleanupTMPLiveFiles, |
8ebf2a5d C |
61 | buildConcatenatedName |
62 | } |