]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/schedulers/videos-redundancy-scheduler.ts
Add ability for plugins to alter video jsonld
[github/Chocobozzz/PeerTube.git] / server / lib / schedulers / videos-redundancy-scheduler.ts
index d9988157d3ba87588d784fd2738dc4b38d929c38..dc450c338d19d4812e10ff99a83bbea161cb36a8 100644 (file)
@@ -1,7 +1,6 @@
 import { move } from 'fs-extra'
 import { join } from 'path'
 import { getServerActor } from '@server/models/application/application'
-import { TrackerModel } from '@server/models/server/tracker'
 import { VideoModel } from '@server/models/video/video'
 import {
   MStreamingPlaylistFiles,
@@ -15,9 +14,9 @@ import {
 } from '@server/types/models'
 import { VideosRedundancyStrategy } from '../../../shared/models/redundancy'
 import { logger, loggerTagsFactory } from '../../helpers/logger'
-import { downloadWebTorrentVideo, generateMagnetUri } from '../../helpers/webtorrent'
+import { downloadWebTorrentVideo } from '../../helpers/webtorrent'
 import { CONFIG } from '../../initializers/config'
-import { HLS_REDUNDANCY_DIRECTORY, REDUNDANCY, VIDEO_IMPORT_TIMEOUT } from '../../initializers/constants'
+import { DIRECTORIES, REDUNDANCY, VIDEO_IMPORT_TIMEOUT } from '../../initializers/constants'
 import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
 import { sendCreateCacheFile, sendUpdateCacheFile } from '../activitypub/send'
 import { getLocalVideoCacheFileActivityPubUrl, getLocalVideoCacheStreamingPlaylistActivityPubUrl } from '../activitypub/url'
@@ -116,24 +115,29 @@ export class VideosRedundancyScheduler extends AbstractScheduler {
     for (const redundancyModel of expired) {
       try {
         const redundancyConfig = CONFIG.REDUNDANCY.VIDEOS.STRATEGIES.find(s => s.strategy === redundancyModel.strategy)
-        const candidate: CandidateToDuplicate = {
-          redundancy: redundancyConfig,
-          video: null,
-          files: [],
-          streamingPlaylists: []
-        }
 
-        // If the administrator disabled the redundancy or decreased the cache size, remove this redundancy instead of extending it
-        if (!redundancyConfig || await this.isTooHeavy(candidate)) {
+        // If the admin disabled the redundancy, remove this redundancy instead of extending it
+        if (!redundancyConfig) {
           logger.info(
-            'Destroying redundancy %s because the cache size %s is too heavy.',
-            redundancyModel.url, redundancyModel.strategy, lTags(candidate.video.uuid)
+            'Destroying redundancy %s because the redundancy %s does not exist anymore.',
+            redundancyModel.url, redundancyModel.strategy
           )
 
           await removeVideoRedundancy(redundancyModel)
-        } else {
-          await this.extendsRedundancy(redundancyModel)
+          continue
         }
+
+        const { totalUsed } = await VideoRedundancyModel.getStats(redundancyConfig.strategy)
+
+        // If the admin decreased the cache size, remove this redundancy instead of extending it
+        if (totalUsed > redundancyConfig.size) {
+          logger.info('Destroying redundancy %s because the cache size %s is too heavy.', redundancyModel.url, redundancyModel.strategy)
+
+          await removeVideoRedundancy(redundancyModel)
+          continue
+        }
+
+        await this.extendsRedundancy(redundancyModel)
       } catch (err) {
         logger.error(
           'Cannot extend or remove expiration of %s video from our redundancy system.',
@@ -232,10 +236,7 @@ export class VideosRedundancyScheduler extends AbstractScheduler {
 
     logger.info('Duplicating %s - %d in videos redundancy with "%s" strategy.', video.url, file.resolution, strategy, lTags(video.uuid))
 
-    const trackerUrls = await TrackerModel.listUrlsByVideoId(video.id)
-    const magnetUri = generateMagnetUri(video, file, trackerUrls)
-
-    const tmpPath = await downloadWebTorrentVideo({ magnetUri }, VIDEO_IMPORT_TIMEOUT)
+    const tmpPath = await downloadWebTorrentVideo({ uri: file.torrentUrl }, VIDEO_IMPORT_TIMEOUT)
 
     const destPath = join(CONFIG.STORAGE.REDUNDANCY_DIR, file.filename)
     await move(tmpPath, destPath, { overwrite: true })
@@ -274,7 +275,7 @@ export class VideosRedundancyScheduler extends AbstractScheduler {
 
     logger.info('Duplicating %s streaming playlist in videos redundancy with "%s" strategy.', video.url, strategy, lTags(video.uuid))
 
-    const destDirectory = join(HLS_REDUNDANCY_DIRECTORY, video.uuid)
+    const destDirectory = join(DIRECTORIES.HLS_REDUNDANCY, video.uuid)
     const masterPlaylistUrl = playlist.getMasterPlaylistUrl(video)
 
     const maxSizeKB = this.getTotalFileSizes([], [ playlist ]) / 1000
@@ -329,14 +330,14 @@ export class VideosRedundancyScheduler extends AbstractScheduler {
   private async isTooHeavy (candidateToDuplicate: CandidateToDuplicate) {
     const maxSize = candidateToDuplicate.redundancy.size
 
-    const { totalUsed: used } = await VideoRedundancyModel.getStats(candidateToDuplicate.redundancy.strategy)
+    const { totalUsed: alreadyUsed } = await VideoRedundancyModel.getStats(candidateToDuplicate.redundancy.strategy)
 
     const videoSize = this.getTotalFileSizes(candidateToDuplicate.files, candidateToDuplicate.streamingPlaylists)
-    const total = used + videoSize
+    const willUse = alreadyUsed + videoSize
 
-    logger.debug('Checking candidate size.', { used, videoSize, total, ...lTags(candidateToDuplicate.video.uuid) })
+    logger.debug('Checking candidate size.', { maxSize, alreadyUsed, videoSize, willUse, ...lTags(candidateToDuplicate.video.uuid) })
 
-    return total > maxSize
+    return willUse > maxSize
   }
 
   private buildNewExpiration (expiresAfterMs: number) {
@@ -364,7 +365,7 @@ export class VideosRedundancyScheduler extends AbstractScheduler {
     // We need more attributes and check if the video still exists
     const getVideoOptions = {
       videoObject: videoUrl,
-      syncParam: { likes: false, dislikes: false, shares: false, comments: false, thumbnail: false, refreshVideo: true },
+      syncParam: { rates: false, shares: false, comments: false, thumbnail: false, refreshVideo: true },
       fetchType: 'all' as 'all'
     }
     const { video } = await getOrCreateAPVideo(getVideoOptions)