]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Update torrent metadata on video update
authorChocobozzz <me@florianbigard.com>
Wed, 8 Dec 2021 10:07:19 +0000 (11:07 +0100)
committerChocobozzz <me@florianbigard.com>
Wed, 8 Dec 2021 10:07:19 +0000 (11:07 +0100)
scripts/update-host.ts
server/controllers/api/videos/update.ts
server/helpers/webtorrent.ts
server/lib/job-queue/handlers/move-to-object-storage.ts
server/models/video/video-playlist-element.ts
server/models/video/video.ts
shared/extra-utils/videos/videos.ts

index c6eb9d5337f33da44b14954ffd5c899213cd0783..f752082fd0f4e8408c43cf0a50d19866493c0cd9 100755 (executable)
@@ -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()
     }
index 3fcff3e868041a8fc4f589bcd16e873e550ef723..6f14a678828869ac34415d18b7cdebb88185e95a 100644 (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)
+  }
+}
index c75c058e4cb0628636383ef32327cd4779e479da..b350c9718784f12c20cb2b282e118c2382d392b0 100644 (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}`
+}
index 54a7c566bc52ddf494e3f610748efb2011e6dfa7..b5eea018448c5e7cf6e3c20a57e67ddbfbcb4778 100644 (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)
index 82c832188f9ad91f4d4e67fcaec8ff585578de6f..a87b2bcaeafdf8ff99e70bdde7a661ecfa367605 100644 (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 (
index 1050463d20dd7dba28c4a8f03364efd0c9e47891..801e23f5592c0dac8fdf9d47c77894bdeffa287c 100644 (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) {
index 4d2784dde8393178de858742a33df8450ec61f6c..c05c2be6c08f01454308c99e19284493240bb505 100644 (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