]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/live/live-utils.ts
More robust move to object storage job failure
[github/Chocobozzz/PeerTube.git] / server / lib / live / live-utils.ts
index 3bf723b981618663c33fd83b798d3a1a63c6fb71..6365e23db371892b4b14601606587bdefe534eb6 100644 (file)
@@ -1,5 +1,6 @@
-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 { getLiveDirectory } from '../paths'
 
@@ -9,15 +10,46 @@ function buildConcatenatedName (segmentOrPlaylistPath: string) {
   return 'concat-' + num[1] + '.ts'
 }
 
-async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
+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
 }