+ const serverActor = await getServerActor()
+
+ logger.info('Duplicating %s - %d in videos redundancy with "%s" strategy.', video.url, file.resolution, strategy)
+
+ const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
+ const magnetUri = generateMagnetUri(video, file, baseUrlHttp, baseUrlWs)
+
+ const tmpPath = await downloadWebTorrentVideo({ magnetUri }, VIDEO_IMPORT_TIMEOUT)
+
+ const destPath = join(CONFIG.STORAGE.REDUNDANCY_DIR, getVideoFilename(video, file))
+ await move(tmpPath, destPath, { overwrite: true })
+
+ const createdModel: MVideoRedundancyFileVideo = await VideoRedundancyModel.create({
+ expiresOn,
+ url: getVideoCacheFileActivityPubUrl(file),
+ fileUrl: video.getVideoRedundancyUrl(file, WEBSERVER.URL),
+ 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)
+ }
+
+ private async createStreamingPlaylistRedundancy (
+ redundancy: VideosRedundancyStrategy,
+ video: MVideoAccountLight,
+ playlistArg: MStreamingPlaylist
+ ) {
+ let strategy = 'manual'
+ let expiresOn: Date = null
+
+ if (redundancy) {
+ strategy = redundancy.strategy
+ expiresOn = this.buildNewExpiration(redundancy.minLifetime)
+ }
+
+ const playlist = playlistArg as MStreamingPlaylistVideo
+ playlist.Video = video
+
+ const serverActor = await getServerActor()
+
+ logger.info('Duplicating %s streaming playlist in videos redundancy with "%s" strategy.', video.url, strategy)
+
+ const destDirectory = join(HLS_REDUNDANCY_DIRECTORY, video.uuid)
+ await downloadPlaylistSegments(playlist.playlistUrl, destDirectory, VIDEO_IMPORT_TIMEOUT)
+
+ const createdModel: MVideoRedundancyStreamingPlaylistVideo = await VideoRedundancyModel.create({
+ expiresOn,
+ url: getVideoCacheStreamingPlaylistActivityPubUrl(video, playlist),
+ fileUrl: playlist.getVideoRedundancyUrl(WEBSERVER.URL),
+ strategy,
+ videoStreamingPlaylistId: playlist.id,
+ actorId: serverActor.id
+ })
+
+ createdModel.VideoStreamingPlaylist = playlist
+
+ await sendCreateCacheFile(serverActor, video, createdModel)
+
+ logger.info('Duplicated playlist %s -> %s.', playlist.playlistUrl, createdModel.url)
+ }
+
+ private async extendsExpirationOf (redundancy: MVideoRedundancyVideo, expiresAfterMs: number) {
+ logger.info('Extending expiration of %s.', redundancy.url)
+
+ 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
+
+ await removeVideoRedundancy(toDelete)