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'
MVideoRedundancyVideo,
MVideoWithAllFiles
} from '@server/typings/models'
+import { getVideoFilename } from '../video-paths'
type CandidateToDuplicate = {
redundancy: VideosRedundancy,
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),
}
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
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) {