aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos/update.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-03-16 15:34:21 +0100
committerChocobozzz <me@florianbigard.com>2022-03-16 15:45:58 +0100
commitf012319a644fe8d9d33f2f567fa828442a3b39fd (patch)
tree8fc7aeff10749ed8088e3f89745433b59bb62c20 /server/controllers/api/videos/update.ts
parent83664918901564830f3b7d1bd9879411a1b857a8 (diff)
downloadPeerTube-f012319a644fe8d9d33f2f567fa828442a3b39fd.tar.gz
PeerTube-f012319a644fe8d9d33f2f567fa828442a3b39fd.tar.zst
PeerTube-f012319a644fe8d9d33f2f567fa828442a3b39fd.zip
Process video torrents in order
Prevent update before video torrent generation for example
Diffstat (limited to 'server/controllers/api/videos/update.ts')
-rw-r--r--server/controllers/api/videos/update.ts34
1 files changed, 19 insertions, 15 deletions
diff --git a/server/controllers/api/videos/update.ts b/server/controllers/api/videos/update.ts
index 15899307d..2cf8a5883 100644
--- a/server/controllers/api/videos/update.ts
+++ b/server/controllers/api/videos/update.ts
@@ -1,12 +1,12 @@
1import express from 'express' 1import express from 'express'
2import { Transaction } from 'sequelize/types' 2import { Transaction } from 'sequelize/types'
3import { updateTorrentMetadata } from '@server/helpers/webtorrent'
4import { changeVideoChannelShare } from '@server/lib/activitypub/share' 3import { changeVideoChannelShare } from '@server/lib/activitypub/share'
4import { JobQueue } from '@server/lib/job-queue'
5import { buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video' 5import { buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video'
6import { openapiOperationDoc } from '@server/middlewares/doc' 6import { openapiOperationDoc } from '@server/middlewares/doc'
7import { FilteredModelAttributes } from '@server/types' 7import { FilteredModelAttributes } from '@server/types'
8import { MVideoFullLight } from '@server/types/models' 8import { MVideoFullLight } from '@server/types/models'
9import { HttpStatusCode, VideoUpdate } from '@shared/models' 9import { HttpStatusCode, ManageVideoTorrentPayload, VideoUpdate } from '@shared/models'
10import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' 10import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger'
11import { resetSequelizeInstance } from '../../../helpers/database-utils' 11import { resetSequelizeInstance } from '../../../helpers/database-utils'
12import { createReqFiles } from '../../../helpers/express-utils' 12import { createReqFiles } from '../../../helpers/express-utils'
@@ -139,15 +139,13 @@ async function updateVideo (req: express.Request, res: express.Response) {
139 return { videoInstanceUpdated, isNewVideo } 139 return { videoInstanceUpdated, isNewVideo }
140 }) 140 })
141 141
142 if (videoInstanceUpdated.isLive !== true && videoInfoToUpdate.name) { 142 const refreshedVideo = await updateTorrentsMetadataIfNeeded(videoInstanceUpdated, videoInfoToUpdate)
143 await updateTorrentsMetadata(videoInstanceUpdated)
144 }
145 143
146 await sequelizeTypescript.transaction(t => federateVideoIfNeeded(videoInstanceUpdated, isNewVideo, t)) 144 await sequelizeTypescript.transaction(t => federateVideoIfNeeded(refreshedVideo, isNewVideo, t))
147 145
148 if (wasConfidentialVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(videoInstanceUpdated) 146 if (wasConfidentialVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(refreshedVideo)
149 147
150 Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body, req, res }) 148 Hooks.runAction('action:api.video.updated', { video: refreshedVideo, body: req.body, req, res })
151 } catch (err) { 149 } catch (err) {
152 // Force fields we want to update 150 // Force fields we want to update
153 // If the transaction is retried, sequelize will think the object has not changed 151 // If the transaction is retried, sequelize will think the object has not changed
@@ -194,19 +192,25 @@ function updateSchedule (videoInstance: MVideoFullLight, videoInfoToUpdate: Vide
194 } 192 }
195} 193}
196 194
197async function updateTorrentsMetadata (video: MVideoFullLight) { 195async function updateTorrentsMetadataIfNeeded (video: MVideoFullLight, videoInfoToUpdate: VideoUpdate) {
196 if (video.isLive || !videoInfoToUpdate.name) return video
197
198 for (const file of (video.VideoFiles || [])) { 198 for (const file of (video.VideoFiles || [])) {
199 await updateTorrentMetadata(video, file) 199 const payload: ManageVideoTorrentPayload = { action: 'update-metadata', videoId: video.id, videoFileId: file.id }
200 200
201 await file.save() 201 const job = await JobQueue.Instance.createJobWithPromise({ type: 'manage-video-torrent', payload })
202 await job.finished()
202 } 203 }
203 204
204 const hls = video.getHLSPlaylist() 205 const hls = video.getHLSPlaylist()
205 if (!hls) return
206 206
207 for (const file of (hls.VideoFiles || [])) { 207 for (const file of (hls?.VideoFiles || [])) {
208 await updateTorrentMetadata(hls, file) 208 const payload: ManageVideoTorrentPayload = { action: 'update-metadata', streamingPlaylistId: hls.id, videoFileId: file.id }
209 209
210 await file.save() 210 const job = await JobQueue.Instance.createJobWithPromise({ type: 'manage-video-torrent', payload })
211 await job.finished()
211 } 212 }
213
214 // Refresh video since files have changed
215 return VideoModel.loadAndPopulateAccountAndServerAndTags(video.id)
212} 216}