Update torrent metadata on video update

pull/4624/head
Chocobozzz 2021-12-08 11:07:19 +01:00
parent a23f6c94ed
commit 9b293cd6a2
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
7 changed files with 42 additions and 13 deletions

View File

@ -17,7 +17,7 @@ import { VideoCommentModel } from '../server/models/video/video-comment'
import { AccountModel } from '../server/models/account/account'
import { VideoChannelModel } from '../server/models/video/video-channel'
import { initDatabaseModels } from '../server/initializers/database'
import { updateTorrentUrls } from '@server/helpers/webtorrent'
import { updateTorrentMetadata } from '@server/helpers/webtorrent'
import { getServerActor } from '@server/models/application/application'
run()
@ -126,7 +126,7 @@ async function run () {
for (const file of video.VideoFiles) {
console.log('Updating torrent file %s of video %s.', file.resolution, video.uuid)
await updateTorrentUrls(video, file)
await updateTorrentMetadata(video, file)
await file.save()
}
@ -135,7 +135,7 @@ async function run () {
for (const file of (playlist?.VideoFiles || [])) {
console.log('Updating fragmented torrent file %s of video %s.', file.resolution, video.uuid)
await updateTorrentUrls(video, file)
await updateTorrentMetadata(video, file)
await file.save()
}

View File

@ -1,5 +1,6 @@
import express from 'express'
import { Transaction } from 'sequelize/types'
import { updateTorrentMetadata } from '@server/helpers/webtorrent'
import { changeVideoChannelShare } from '@server/lib/activitypub/share'
import { buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video'
import { openapiOperationDoc } from '@server/middlewares/doc'
@ -149,9 +150,8 @@ async function updateVideo (req: express.Request, res: express.Response) {
return videoInstanceUpdated
})
if (wasConfidentialVideo) {
Notifier.Instance.notifyOnNewVideoIfNeeded(videoInstanceUpdated)
}
if (videoInfoToUpdate.name) await updateTorrentsMetadata(videoInstanceUpdated)
if (wasConfidentialVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(videoInstanceUpdated)
Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body, req, res })
} catch (err) {
@ -199,3 +199,9 @@ function updateSchedule (videoInstance: MVideoFullLight, videoInfoToUpdate: Vide
return ScheduleVideoUpdateModel.deleteByVideoId(videoInstance.id, transaction)
}
}
async function updateTorrentsMetadata (video: MVideoFullLight) {
for (const file of video.getAllFiles()) {
await updateTorrentMetadata(video, file)
}
}

View File

@ -94,7 +94,7 @@ function createTorrentAndSetInfoHash (videoOrPlaylist: MVideo | MStreamingPlayli
const options = {
// Keep the extname, it's used by the client to stream the file inside a web browser
name: `${video.name} ${videoFile.resolution}p${videoFile.extname}`,
name: buildInfoName(video, videoFile),
createdBy: 'PeerTube',
announceList: buildAnnounceList(),
urlList: buildUrlList(video, videoFile)
@ -120,7 +120,7 @@ function createTorrentAndSetInfoHash (videoOrPlaylist: MVideo | MStreamingPlayli
})
}
async function updateTorrentUrls (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
async function updateTorrentMetadata (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
const video = extractVideo(videoOrPlaylist)
const oldTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename)
@ -133,10 +133,13 @@ async function updateTorrentUrls (videoOrPlaylist: MVideo | MStreamingPlaylistVi
decoded['url-list'] = buildUrlList(video, videoFile)
decoded.info.name = buildInfoName(video, videoFile)
decoded['creation date'] = Math.ceil(Date.now() / 1000)
const newTorrentFilename = generateTorrentFileName(videoOrPlaylist, videoFile.resolution)
const newTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, newTorrentFilename)
logger.info('Updating torrent URLs %s -> %s.', oldTorrentPath, newTorrentPath)
logger.info('Updating torrent metadata %s -> %s.', oldTorrentPath, newTorrentPath)
await writeFile(newTorrentPath, encode(decoded))
await remove(join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename))
@ -171,7 +174,7 @@ function generateMagnetUri (
export {
createTorrentPromise,
updateTorrentUrls,
updateTorrentMetadata,
createTorrentAndSetInfoHash,
generateMagnetUri,
downloadWebTorrentVideo
@ -226,3 +229,7 @@ function buildAnnounceList () {
function buildUrlList (video: MVideo, videoFile: MVideoFile) {
return [ videoFile.getFileUrl(video) ]
}
function buildInfoName (video: MVideo, videoFile: MVideoFile) {
return `${video.name} ${videoFile.resolution}p${videoFile.extname}`
}

View File

@ -2,7 +2,7 @@ import { Job } from 'bull'
import { remove } from 'fs-extra'
import { join } from 'path'
import { logger } from '@server/helpers/logger'
import { updateTorrentUrls } from '@server/helpers/webtorrent'
import { updateTorrentMetadata } from '@server/helpers/webtorrent'
import { CONFIG } from '@server/initializers/config'
import { P2P_MEDIA_LOADER_PEER_VERSION } from '@server/initializers/constants'
import { storeHLSFile, storeWebTorrentFile } from '@server/lib/object-storage'
@ -113,7 +113,7 @@ async function onFileMoved (options: {
file.fileUrl = fileUrl
file.storage = VideoStorage.OBJECT_STORAGE
await updateTorrentUrls(videoOrPlaylist, file)
await updateTorrentMetadata(videoOrPlaylist, file)
await file.save()
logger.debug('Removing %s because it\'s now on object storage', oldPath)

View File

@ -276,7 +276,7 @@ export class VideoPlaylistElementModel extends Model<Partial<AttributesOnly<Vide
}
const positionQuery = Sequelize.literal(`${newPosition} + "position" - ${firstPosition}`)
return VideoPlaylistElementModel.update({ position: positionQuery as any }, query)
return VideoPlaylistElementModel.update({ position: positionQuery }, query)
}
static increasePositionOf (

View File

@ -1539,6 +1539,21 @@ export class VideoModel extends Model<Partial<AttributesOnly<VideoModel>>> {
return this.VideoChannel.Account.Actor.Server?.isBlocked() || this.VideoChannel.Account.isBlocked()
}
getAllFiles () {
let files: MVideoFile[] = []
if (Array.isArray(this.VideoFiles)) {
files = files.concat(this.VideoFiles)
}
const hls = this.getHLSPlaylist()
if (hls) {
files = files.concat(hls.VideoFiles)
}
return files
}
getQualityFileBy<T extends MVideoWithFile> (this: T, fun: (files: MVideoFile[], it: (file: MVideoFile) => number) => MVideoFile) {
// We first transcode to WebTorrent format, so try this array first
if (Array.isArray(this.VideoFiles) && this.VideoFiles.length !== 0) {

View File

@ -217,6 +217,7 @@ async function completeVideoCheck (
expect(torrent.files).to.be.an('array')
expect(torrent.files.length).to.equal(1)
expect(torrent.files[0].path).to.exist.and.to.not.equal('')
expect(torrent.files[0].name).to.equal(`${videoDetails.name} ${file.resolution.id}p${extension}`)
}
expect(videoDetails.thumbnailPath).to.exist