]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/schedulers/videos-redundancy-scheduler.ts
Don't always replace actor avatar
[github/Chocobozzz/PeerTube.git] / server / lib / schedulers / videos-redundancy-scheduler.ts
index 21fe51156e995d782be6d370d97c4188ff18e930..c1c91b6563d9488ca1b37d8ea2cce68fc544d119 100644 (file)
@@ -3,7 +3,7 @@ import { HLS_REDUNDANCY_DIRECTORY, REDUNDANCY, VIDEO_IMPORT_TIMEOUT, WEBSERVER }
 import { logger } from '../../helpers/logger'
 import { VideosRedundancy } from '../../../shared/models/redundancy'
 import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
-import { downloadWebTorrentVideo } from '../../helpers/webtorrent'
+import { downloadWebTorrentVideo, generateMagnetUri } from '../../helpers/webtorrent'
 import { join } from 'path'
 import { move } from 'fs-extra'
 import { getServerActor } from '../../helpers/utils'
@@ -14,7 +14,7 @@ import { getOrCreateVideoAndAccountAndChannel } from '../activitypub'
 import { downloadPlaylistSegments } from '../hls'
 import { CONFIG } from '../../initializers/config'
 import {
-  MStreamingPlaylist,
+  MStreamingPlaylist, MStreamingPlaylistFiles,
   MStreamingPlaylistVideo,
   MVideoAccountLight,
   MVideoFile,
@@ -24,12 +24,13 @@ import {
   MVideoRedundancyVideo,
   MVideoWithAllFiles
 } from '@server/typings/models'
+import { getVideoFilename } from '../video-paths'
 
 type CandidateToDuplicate = {
   redundancy: VideosRedundancy,
   video: MVideoWithAllFiles,
   files: MVideoFile[],
-  streamingPlaylists: MStreamingPlaylist[]
+  streamingPlaylists: MStreamingPlaylistFiles[]
 }
 
 function isMVideoRedundancyFileVideo (
@@ -195,11 +196,11 @@ export class VideosRedundancyScheduler extends AbstractScheduler {
     logger.info('Duplicating %s - %d in videos redundancy with "%s" strategy.', video.url, file.resolution, redundancy.strategy)
 
     const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
-    const magnetUri = video.generateMagnetUri(file, baseUrlHttp, baseUrlWs)
+    const magnetUri = generateMagnetUri(video, file, baseUrlHttp, baseUrlWs)
 
     const tmpPath = await downloadWebTorrentVideo({ magnetUri }, VIDEO_IMPORT_TIMEOUT)
 
-    const destPath = join(CONFIG.STORAGE.REDUNDANCY_DIR, video.getVideoFilename(file))
+    const destPath = join(CONFIG.STORAGE.REDUNDANCY_DIR, getVideoFilename(video, file))
     await move(tmpPath, destPath, { overwrite: true })
 
     const createdModel: MVideoRedundancyFileVideo = await VideoRedundancyModel.create({
@@ -261,7 +262,7 @@ export class VideosRedundancyScheduler extends AbstractScheduler {
   }
 
   private async purgeCacheIfNeeded (candidateToDuplicate: CandidateToDuplicate) {
-    while (this.isTooHeavy(candidateToDuplicate)) {
+    while (await this.isTooHeavy(candidateToDuplicate)) {
       const redundancy = candidateToDuplicate.redundancy
       const toDelete = await VideoRedundancyModel.loadOldestLocalExpired(redundancy.strategy, redundancy.minLifetime)
       if (!toDelete) return
@@ -289,13 +290,15 @@ export class VideosRedundancyScheduler extends AbstractScheduler {
     return `${object.VideoStreamingPlaylist.playlistUrl}`
   }
 
-  private getTotalFileSizes (files: MVideoFile[], playlists: MStreamingPlaylist[]) {
+  private getTotalFileSizes (files: MVideoFile[], playlists: MStreamingPlaylistFiles[]) {
     const fileReducer = (previous: number, current: MVideoFile) => previous + current.size
 
-    const totalSize = files.reduce(fileReducer, 0)
-    if (playlists.length === 0) return totalSize
+    let allFiles = files
+    for (const p of playlists) {
+      allFiles = allFiles.concat(p.VideoFiles)
+    }
 
-    return totalSize * playlists.length
+    return allFiles.reduce(fileReducer, 0)
   }
 
   private async loadAndRefreshVideo (videoUrl: string) {