]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/schedulers/videos-redundancy-scheduler.ts
Add ability to disable webtorrent
[github/Chocobozzz/PeerTube.git] / server / lib / schedulers / videos-redundancy-scheduler.ts
index d9018e6060fa8ee5c9c469eb241ed4f1e76daa50..f2bd75cb4a85e44ebf4cedd169a68c7f11a52e80 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'
@@ -24,6 +24,7 @@ import {
   MVideoRedundancyVideo,
   MVideoWithAllFiles
 } from '@server/typings/models'
+import { getVideoFilename } from '../video-paths'
 
 type CandidateToDuplicate = {
   redundancy: VideosRedundancy,
@@ -195,12 +196,12 @@ 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 = await generateMagnetUri(video, file, baseUrlHttp, baseUrlWs)
 
     const tmpPath = await downloadWebTorrentVideo({ magnetUri }, VIDEO_IMPORT_TIMEOUT)
 
-    const destPath = join(CONFIG.STORAGE.REDUNDANCY_DIR, video.getVideoFilename(file))
-    await move(tmpPath, destPath)
+    const destPath = join(CONFIG.STORAGE.REDUNDANCY_DIR, getVideoFilename(video, file))
+    await move(tmpPath, destPath, { overwrite: true })
 
     const createdModel: MVideoRedundancyFileVideo = await VideoRedundancyModel.create({
       expiresOn: this.buildNewExpiration(redundancy.minLifetime),
@@ -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
@@ -293,9 +294,8 @@ export class VideosRedundancyScheduler extends AbstractScheduler {
     const fileReducer = (previous: number, current: MVideoFile) => previous + current.size
 
     const totalSize = files.reduce(fileReducer, 0)
-    if (playlists.length === 0) return totalSize
 
-    return totalSize * playlists.length
+    return totalSize + (totalSize * playlists.length)
   }
 
   private async loadAndRefreshVideo (videoUrl: string) {