+ const serverActor = await getServerActor()
+
+ logger.info('Duplicating %s - %d in videos redundancy with "%s" strategy.', video.url, file.resolution, strategy, lTags(video.uuid))
+
+ 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 })
+
+ const createdModel: MVideoRedundancyFileVideo = await VideoRedundancyModel.create({
+ expiresOn,
+ url: getLocalVideoCacheFileActivityPubUrl(file),
+ fileUrl: generateWebTorrentRedundancyUrl(file),
+ strategy,
+ videoFileId: file.id,
+ actorId: serverActor.id
+ })
+
+ createdModel.VideoFile = file
+
+ await sendCreateCacheFile(serverActor, video, createdModel)
+
+ logger.info('Duplicated %s - %d -> %s.', video.url, file.resolution, createdModel.url, lTags(video.uuid))
+ }
+
+ private async createStreamingPlaylistRedundancy (
+ redundancy: VideosRedundancyStrategy,
+ video: MVideoAccountLight,
+ playlistArg: MStreamingPlaylistFiles
+ ) {
+ let strategy = 'manual'
+ let expiresOn: Date = null
+
+ if (redundancy) {
+ strategy = redundancy.strategy
+ expiresOn = this.buildNewExpiration(redundancy.minLifetime)
+ }
+
+ const playlist = Object.assign(playlistArg, { Video: video })
+ const serverActor = await getServerActor()
+
+ 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 masterPlaylistUrl = playlist.getMasterPlaylistUrl(video)
+
+ const maxSizeKB = this.getTotalFileSizes([], [ playlist ]) / 1000
+ const toleranceKB = maxSizeKB + ((5 * maxSizeKB) / 100) // 5% more tolerance
+ await downloadPlaylistSegments(masterPlaylistUrl, destDirectory, VIDEO_IMPORT_TIMEOUT, toleranceKB)
+
+ const createdModel: MVideoRedundancyStreamingPlaylistVideo = await VideoRedundancyModel.create({
+ expiresOn,
+ url: getLocalVideoCacheStreamingPlaylistActivityPubUrl(video, playlist),
+ fileUrl: generateHLSRedundancyUrl(video, playlistArg),
+ strategy,
+ videoStreamingPlaylistId: playlist.id,
+ actorId: serverActor.id
+ })
+
+ createdModel.VideoStreamingPlaylist = playlist
+
+ await sendCreateCacheFile(serverActor, video, createdModel)
+
+ logger.info('Duplicated playlist %s -> %s.', masterPlaylistUrl, createdModel.url, lTags(video.uuid))
+ }
+
+ private async extendsExpirationOf (redundancy: MVideoRedundancyVideo, expiresAfterMs: number) {
+ logger.info('Extending expiration of %s.', redundancy.url, lTags(redundancy.getVideoUUID()))
+
+ const serverActor = await getServerActor()
+
+ redundancy.expiresOn = this.buildNewExpiration(expiresAfterMs)
+ await redundancy.save()
+
+ await sendUpdateCacheFile(serverActor, redundancy)
+ }
+
+ private async purgeCacheIfNeeded (candidateToDuplicate: CandidateToDuplicate) {
+ while (await this.isTooHeavy(candidateToDuplicate)) {
+ const redundancy = candidateToDuplicate.redundancy
+ const toDelete = await VideoRedundancyModel.loadOldestLocalExpired(redundancy.strategy, redundancy.minLifetime)
+ if (!toDelete) return
+
+ const videoId = toDelete.VideoFile
+ ? toDelete.VideoFile.videoId
+ : toDelete.VideoStreamingPlaylist.videoId
+
+ const redundancies = await VideoRedundancyModel.listLocalByVideoId(videoId)
+
+ for (const redundancy of redundancies) {
+ await removeVideoRedundancy(redundancy)
+ }