]>
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' | |
cfd57d2c C |
4 | import { MStreamingPlaylist, MStreamingPlaylistVideo, MVideo } from '@server/types/models' |
5 | import { VideoStorage } from '@shared/models' | |
aa887096 | 6 | import { listHLSFileKeysOf, removeHLSFileObjectStorageByFullKey, removeHLSObjectStorage } from '../object-storage' |
0305db28 | 7 | import { getLiveDirectory } from '../paths' |
8ebf2a5d C |
8 | |
9 | function buildConcatenatedName (segmentOrPlaylistPath: string) { | |
10 | const num = basename(segmentOrPlaylistPath).match(/^(\d+)(-|\.)/) | |
11 | ||
12 | return 'concat-' + num[1] + '.ts' | |
13 | } | |
14 | ||
cfd57d2c C |
15 | async function cleanupAndDestroyPermanentLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) { |
16 | await cleanupTMPLiveFiles(video, streamingPlaylist) | |
5333788c | 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 | 23 | |
cfd57d2c C |
24 | // We uploaded files to object storage too, remove them |
25 | if (streamingPlaylist.storage === VideoStorage.OBJECT_STORAGE) { | |
26 | await removeHLSObjectStorage(streamingPlaylist.withVideo(video)) | |
27 | } | |
28 | ||
8ebf2a5d C |
29 | await remove(hlsDirectory) |
30 | ||
53023be3 | 31 | await streamingPlaylist.destroy() |
cfd57d2c | 32 | } |
92083e42 | 33 | |
cfd57d2c C |
34 | async function cleanupTMPLiveFiles (video: MVideo, streamingPlaylist: MStreamingPlaylist) { |
35 | await cleanupTMPLiveFilesFromObjectStorage(streamingPlaylist.withVideo(video)) | |
36 | ||
37 | await cleanupTMPLiveFilesFromFilesystem(video) | |
8ebf2a5d C |
38 | } |
39 | ||
cfd57d2c C |
40 | export { |
41 | cleanupAndDestroyPermanentLive, | |
42 | cleanupUnsavedNormalLive, | |
43 | cleanupTMPLiveFiles, | |
44 | buildConcatenatedName | |
45 | } | |
46 | ||
47 | // --------------------------------------------------------------------------- | |
92083e42 | 48 | |
cfd57d2c C |
49 | function isTMPLiveFile (name: string) { |
50 | return name.endsWith('.ts') || | |
51 | name.endsWith('.m3u8') || | |
52 | name.endsWith('.json') || | |
53 | name.endsWith('.mpd') || | |
54 | name.endsWith('.m4s') || | |
55 | name.endsWith('.tmp') | |
56 | } | |
57 | ||
58 | async function cleanupTMPLiveFilesFromFilesystem (video: MVideo) { | |
59 | const hlsDirectory = getLiveDirectory(video) | |
92083e42 | 60 | |
5333788c C |
61 | if (!await pathExists(hlsDirectory)) return |
62 | ||
cfd57d2c | 63 | logger.info('Cleanup TMP live files from filesystem of %s.', hlsDirectory) |
92083e42 | 64 | |
5333788c C |
65 | const files = await readdir(hlsDirectory) |
66 | ||
67 | for (const filename of files) { | |
cfd57d2c | 68 | if (isTMPLiveFile(filename)) { |
5333788c C |
69 | const p = join(hlsDirectory, filename) |
70 | ||
71 | remove(p) | |
72 | .catch(err => logger.error('Cannot remove %s.', p, { err })) | |
73 | } | |
74 | } | |
75 | } | |
76 | ||
cfd57d2c C |
77 | async function cleanupTMPLiveFilesFromObjectStorage (streamingPlaylist: MStreamingPlaylistVideo) { |
78 | if (streamingPlaylist.storage !== VideoStorage.OBJECT_STORAGE) return | |
79 | ||
aa887096 C |
80 | logger.info('Cleanup TMP live files from object storage for %s.', streamingPlaylist.Video.uuid) |
81 | ||
cfd57d2c C |
82 | const keys = await listHLSFileKeysOf(streamingPlaylist) |
83 | ||
84 | for (const key of keys) { | |
85 | if (isTMPLiveFile(key)) { | |
aa887096 | 86 | await removeHLSFileObjectStorageByFullKey(key) |
cfd57d2c C |
87 | } |
88 | } | |
8ebf2a5d | 89 | } |