aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-12-08 11:07:19 +0100
committerChocobozzz <me@florianbigard.com>2021-12-08 11:07:19 +0100
commit9b293cd6a2ce9ed1e1ccd41adbf7f2dbe2da8231 (patch)
tree3419e23d551937f716612f93747f58f2d381d310 /server
parenta23f6c94edee1cb11875585a64dc61f1004c1792 (diff)
downloadPeerTube-9b293cd6a2ce9ed1e1ccd41adbf7f2dbe2da8231.tar.gz
PeerTube-9b293cd6a2ce9ed1e1ccd41adbf7f2dbe2da8231.tar.zst
PeerTube-9b293cd6a2ce9ed1e1ccd41adbf7f2dbe2da8231.zip
Update torrent metadata on video update
Diffstat (limited to 'server')
-rw-r--r--server/controllers/api/videos/update.ts12
-rw-r--r--server/helpers/webtorrent.ts15
-rw-r--r--server/lib/job-queue/handlers/move-to-object-storage.ts4
-rw-r--r--server/models/video/video-playlist-element.ts2
-rw-r--r--server/models/video/video.ts15
5 files changed, 38 insertions, 10 deletions
diff --git a/server/controllers/api/videos/update.ts b/server/controllers/api/videos/update.ts
index 3fcff3e86..6f14a6788 100644
--- a/server/controllers/api/videos/update.ts
+++ b/server/controllers/api/videos/update.ts
@@ -1,5 +1,6 @@
1import express from 'express' 1import express from 'express'
2import { Transaction } from 'sequelize/types' 2import { Transaction } from 'sequelize/types'
3import { updateTorrentMetadata } from '@server/helpers/webtorrent'
3import { changeVideoChannelShare } from '@server/lib/activitypub/share' 4import { changeVideoChannelShare } from '@server/lib/activitypub/share'
4import { buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video' 5import { buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video'
5import { openapiOperationDoc } from '@server/middlewares/doc' 6import { openapiOperationDoc } from '@server/middlewares/doc'
@@ -149,9 +150,8 @@ async function updateVideo (req: express.Request, res: express.Response) {
149 return videoInstanceUpdated 150 return videoInstanceUpdated
150 }) 151 })
151 152
152 if (wasConfidentialVideo) { 153 if (videoInfoToUpdate.name) await updateTorrentsMetadata(videoInstanceUpdated)
153 Notifier.Instance.notifyOnNewVideoIfNeeded(videoInstanceUpdated) 154 if (wasConfidentialVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(videoInstanceUpdated)
154 }
155 155
156 Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body, req, res }) 156 Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body, req, res })
157 } catch (err) { 157 } catch (err) {
@@ -199,3 +199,9 @@ function updateSchedule (videoInstance: MVideoFullLight, videoInfoToUpdate: Vide
199 return ScheduleVideoUpdateModel.deleteByVideoId(videoInstance.id, transaction) 199 return ScheduleVideoUpdateModel.deleteByVideoId(videoInstance.id, transaction)
200 } 200 }
201} 201}
202
203async function updateTorrentsMetadata (video: MVideoFullLight) {
204 for (const file of video.getAllFiles()) {
205 await updateTorrentMetadata(video, file)
206 }
207}
diff --git a/server/helpers/webtorrent.ts b/server/helpers/webtorrent.ts
index c75c058e4..b350c9718 100644
--- a/server/helpers/webtorrent.ts
+++ b/server/helpers/webtorrent.ts
@@ -94,7 +94,7 @@ function createTorrentAndSetInfoHash (videoOrPlaylist: MVideo | MStreamingPlayli
94 94
95 const options = { 95 const options = {
96 // Keep the extname, it's used by the client to stream the file inside a web browser 96 // Keep the extname, it's used by the client to stream the file inside a web browser
97 name: `${video.name} ${videoFile.resolution}p${videoFile.extname}`, 97 name: buildInfoName(video, videoFile),
98 createdBy: 'PeerTube', 98 createdBy: 'PeerTube',
99 announceList: buildAnnounceList(), 99 announceList: buildAnnounceList(),
100 urlList: buildUrlList(video, videoFile) 100 urlList: buildUrlList(video, videoFile)
@@ -120,7 +120,7 @@ function createTorrentAndSetInfoHash (videoOrPlaylist: MVideo | MStreamingPlayli
120 }) 120 })
121} 121}
122 122
123async function updateTorrentUrls (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) { 123async function updateTorrentMetadata (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
124 const video = extractVideo(videoOrPlaylist) 124 const video = extractVideo(videoOrPlaylist)
125 125
126 const oldTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename) 126 const oldTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename)
@@ -133,10 +133,13 @@ async function updateTorrentUrls (videoOrPlaylist: MVideo | MStreamingPlaylistVi
133 133
134 decoded['url-list'] = buildUrlList(video, videoFile) 134 decoded['url-list'] = buildUrlList(video, videoFile)
135 135
136 decoded.info.name = buildInfoName(video, videoFile)
137 decoded['creation date'] = Math.ceil(Date.now() / 1000)
138
136 const newTorrentFilename = generateTorrentFileName(videoOrPlaylist, videoFile.resolution) 139 const newTorrentFilename = generateTorrentFileName(videoOrPlaylist, videoFile.resolution)
137 const newTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, newTorrentFilename) 140 const newTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, newTorrentFilename)
138 141
139 logger.info('Updating torrent URLs %s -> %s.', oldTorrentPath, newTorrentPath) 142 logger.info('Updating torrent metadata %s -> %s.', oldTorrentPath, newTorrentPath)
140 143
141 await writeFile(newTorrentPath, encode(decoded)) 144 await writeFile(newTorrentPath, encode(decoded))
142 await remove(join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename)) 145 await remove(join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename))
@@ -171,7 +174,7 @@ function generateMagnetUri (
171 174
172export { 175export {
173 createTorrentPromise, 176 createTorrentPromise,
174 updateTorrentUrls, 177 updateTorrentMetadata,
175 createTorrentAndSetInfoHash, 178 createTorrentAndSetInfoHash,
176 generateMagnetUri, 179 generateMagnetUri,
177 downloadWebTorrentVideo 180 downloadWebTorrentVideo
@@ -226,3 +229,7 @@ function buildAnnounceList () {
226function buildUrlList (video: MVideo, videoFile: MVideoFile) { 229function buildUrlList (video: MVideo, videoFile: MVideoFile) {
227 return [ videoFile.getFileUrl(video) ] 230 return [ videoFile.getFileUrl(video) ]
228} 231}
232
233function buildInfoName (video: MVideo, videoFile: MVideoFile) {
234 return `${video.name} ${videoFile.resolution}p${videoFile.extname}`
235}
diff --git a/server/lib/job-queue/handlers/move-to-object-storage.ts b/server/lib/job-queue/handlers/move-to-object-storage.ts
index 54a7c566b..b5eea0184 100644
--- a/server/lib/job-queue/handlers/move-to-object-storage.ts
+++ b/server/lib/job-queue/handlers/move-to-object-storage.ts
@@ -2,7 +2,7 @@ import { Job } from 'bull'
2import { remove } from 'fs-extra' 2import { remove } from 'fs-extra'
3import { join } from 'path' 3import { join } from 'path'
4import { logger } from '@server/helpers/logger' 4import { logger } from '@server/helpers/logger'
5import { updateTorrentUrls } from '@server/helpers/webtorrent' 5import { updateTorrentMetadata } from '@server/helpers/webtorrent'
6import { CONFIG } from '@server/initializers/config' 6import { CONFIG } from '@server/initializers/config'
7import { P2P_MEDIA_LOADER_PEER_VERSION } from '@server/initializers/constants' 7import { P2P_MEDIA_LOADER_PEER_VERSION } from '@server/initializers/constants'
8import { storeHLSFile, storeWebTorrentFile } from '@server/lib/object-storage' 8import { storeHLSFile, storeWebTorrentFile } from '@server/lib/object-storage'
@@ -113,7 +113,7 @@ async function onFileMoved (options: {
113 file.fileUrl = fileUrl 113 file.fileUrl = fileUrl
114 file.storage = VideoStorage.OBJECT_STORAGE 114 file.storage = VideoStorage.OBJECT_STORAGE
115 115
116 await updateTorrentUrls(videoOrPlaylist, file) 116 await updateTorrentMetadata(videoOrPlaylist, file)
117 await file.save() 117 await file.save()
118 118
119 logger.debug('Removing %s because it\'s now on object storage', oldPath) 119 logger.debug('Removing %s because it\'s now on object storage', oldPath)
diff --git a/server/models/video/video-playlist-element.ts b/server/models/video/video-playlist-element.ts
index 82c832188..a87b2bcae 100644
--- a/server/models/video/video-playlist-element.ts
+++ b/server/models/video/video-playlist-element.ts
@@ -276,7 +276,7 @@ export class VideoPlaylistElementModel extends Model<Partial<AttributesOnly<Vide
276 } 276 }
277 277
278 const positionQuery = Sequelize.literal(`${newPosition} + "position" - ${firstPosition}`) 278 const positionQuery = Sequelize.literal(`${newPosition} + "position" - ${firstPosition}`)
279 return VideoPlaylistElementModel.update({ position: positionQuery as any }, query) 279 return VideoPlaylistElementModel.update({ position: positionQuery }, query)
280 } 280 }
281 281
282 static increasePositionOf ( 282 static increasePositionOf (
diff --git a/server/models/video/video.ts b/server/models/video/video.ts
index 1050463d2..801e23f55 100644
--- a/server/models/video/video.ts
+++ b/server/models/video/video.ts
@@ -1539,6 +1539,21 @@ export class VideoModel extends Model<Partial<AttributesOnly<VideoModel>>> {
1539 return this.VideoChannel.Account.Actor.Server?.isBlocked() || this.VideoChannel.Account.isBlocked() 1539 return this.VideoChannel.Account.Actor.Server?.isBlocked() || this.VideoChannel.Account.isBlocked()
1540 } 1540 }
1541 1541
1542 getAllFiles () {
1543 let files: MVideoFile[] = []
1544
1545 if (Array.isArray(this.VideoFiles)) {
1546 files = files.concat(this.VideoFiles)
1547 }
1548
1549 const hls = this.getHLSPlaylist()
1550 if (hls) {
1551 files = files.concat(hls.VideoFiles)
1552 }
1553
1554 return files
1555 }
1556
1542 getQualityFileBy<T extends MVideoWithFile> (this: T, fun: (files: MVideoFile[], it: (file: MVideoFile) => number) => MVideoFile) { 1557 getQualityFileBy<T extends MVideoWithFile> (this: T, fun: (files: MVideoFile[], it: (file: MVideoFile) => number) => MVideoFile) {
1543 // We first transcode to WebTorrent format, so try this array first 1558 // We first transcode to WebTorrent format, so try this array first
1544 if (Array.isArray(this.VideoFiles) && this.VideoFiles.length !== 0) { 1559 if (Array.isArray(this.VideoFiles) && this.VideoFiles.length !== 0) {