aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos/update.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-10-12 16:09:02 +0200
committerChocobozzz <chocobozzz@cpy.re>2022-10-24 14:48:24 +0200
commit3545e72c686ff1725bbdfd8d16d693e2f4aa75a3 (patch)
treee7f1d12ef5dae1e1142c3a8d0b681c1dbbb0de10 /server/controllers/api/videos/update.ts
parent38a3ccc7f8ad0ea94362b58c732af7c387ab46be (diff)
downloadPeerTube-3545e72c686ff1725bbdfd8d16d693e2f4aa75a3.tar.gz
PeerTube-3545e72c686ff1725bbdfd8d16d693e2f4aa75a3.tar.zst
PeerTube-3545e72c686ff1725bbdfd8d16d693e2f4aa75a3.zip
Put private videos under a specific subdirectory
Diffstat (limited to 'server/controllers/api/videos/update.ts')
-rw-r--r--server/controllers/api/videos/update.ts76
1 files changed, 20 insertions, 56 deletions
diff --git a/server/controllers/api/videos/update.ts b/server/controllers/api/videos/update.ts
index ab1a23d9a..0a910379a 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 { changeVideoChannelShare } from '@server/lib/activitypub/share' 3import { changeVideoChannelShare } from '@server/lib/activitypub/share'
4import { CreateJobArgument, JobQueue } from '@server/lib/job-queue' 4import { addVideoJobsAfterUpdate, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video'
5import { buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video' 5import { setVideoPrivacy } from '@server/lib/video-privacy'
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, ManageVideoTorrentPayload, VideoUpdate } from '@shared/models' 9import { HttpStatusCode, 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'
@@ -18,6 +18,7 @@ import { autoBlacklistVideoIfNeeded } from '../../../lib/video-blacklist'
18import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videosUpdateValidator } from '../../../middlewares' 18import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videosUpdateValidator } from '../../../middlewares'
19import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update' 19import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update'
20import { VideoModel } from '../../../models/video/video' 20import { VideoModel } from '../../../models/video/video'
21import { VideoPathManager } from '@server/lib/video-path-manager'
21 22
22const lTags = loggerTagsFactory('api', 'video') 23const lTags = loggerTagsFactory('api', 'video')
23const auditLogger = auditLoggerFactory('videos') 24const auditLogger = auditLoggerFactory('videos')
@@ -47,8 +48,8 @@ async function updateVideo (req: express.Request, res: express.Response) {
47 const oldVideoAuditView = new VideoAuditView(videoFromReq.toFormattedDetailsJSON()) 48 const oldVideoAuditView = new VideoAuditView(videoFromReq.toFormattedDetailsJSON())
48 const videoInfoToUpdate: VideoUpdate = req.body 49 const videoInfoToUpdate: VideoUpdate = req.body
49 50
50 const wasConfidentialVideo = videoFromReq.isConfidential()
51 const hadPrivacyForFederation = videoFromReq.hasPrivacyForFederation() 51 const hadPrivacyForFederation = videoFromReq.hasPrivacyForFederation()
52 const oldPrivacy = videoFromReq.privacy
52 53
53 const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({ 54 const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({
54 video: videoFromReq, 55 video: videoFromReq,
@@ -57,12 +58,13 @@ async function updateVideo (req: express.Request, res: express.Response) {
57 automaticallyGenerated: false 58 automaticallyGenerated: false
58 }) 59 })
59 60
61 const videoFileLockReleaser = await VideoPathManager.Instance.lockFiles(videoFromReq.uuid)
62
60 try { 63 try {
61 const { videoInstanceUpdated, isNewVideo } = await sequelizeTypescript.transaction(async t => { 64 const { videoInstanceUpdated, isNewVideo } = await sequelizeTypescript.transaction(async t => {
62 // Refresh video since thumbnails to prevent concurrent updates 65 // Refresh video since thumbnails to prevent concurrent updates
63 const video = await VideoModel.loadFull(videoFromReq.id, t) 66 const video = await VideoModel.loadFull(videoFromReq.id, t)
64 67
65 const sequelizeOptions = { transaction: t }
66 const oldVideoChannel = video.VideoChannel 68 const oldVideoChannel = video.VideoChannel
67 69
68 const keysToUpdate: (keyof VideoUpdate & FilteredModelAttributes<VideoModel>)[] = [ 70 const keysToUpdate: (keyof VideoUpdate & FilteredModelAttributes<VideoModel>)[] = [
@@ -97,7 +99,7 @@ async function updateVideo (req: express.Request, res: express.Response) {
97 await video.setAsRefreshed(t) 99 await video.setAsRefreshed(t)
98 } 100 }
99 101
100 const videoInstanceUpdated = await video.save(sequelizeOptions) as MVideoFullLight 102 const videoInstanceUpdated = await video.save({ transaction: t }) as MVideoFullLight
101 103
102 // Thumbnail & preview updates? 104 // Thumbnail & preview updates?
103 if (thumbnailModel) await videoInstanceUpdated.addAndSaveThumbnail(thumbnailModel, t) 105 if (thumbnailModel) await videoInstanceUpdated.addAndSaveThumbnail(thumbnailModel, t)
@@ -113,7 +115,9 @@ async function updateVideo (req: express.Request, res: express.Response) {
113 await videoInstanceUpdated.$set('VideoChannel', res.locals.videoChannel, { transaction: t }) 115 await videoInstanceUpdated.$set('VideoChannel', res.locals.videoChannel, { transaction: t })
114 videoInstanceUpdated.VideoChannel = res.locals.videoChannel 116 videoInstanceUpdated.VideoChannel = res.locals.videoChannel
115 117
116 if (hadPrivacyForFederation === true) await changeVideoChannelShare(videoInstanceUpdated, oldVideoChannel, t) 118 if (hadPrivacyForFederation === true) {
119 await changeVideoChannelShare(videoInstanceUpdated, oldVideoChannel, t)
120 }
117 } 121 }
118 122
119 // Schedule an update in the future? 123 // Schedule an update in the future?
@@ -139,7 +143,12 @@ async function updateVideo (req: express.Request, res: express.Response) {
139 143
140 Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body, req, res }) 144 Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body, req, res })
141 145
142 await addVideoJobsAfterUpdate({ video: videoInstanceUpdated, videoInfoToUpdate, wasConfidentialVideo, isNewVideo }) 146 await addVideoJobsAfterUpdate({
147 video: videoInstanceUpdated,
148 nameChanged: !!videoInfoToUpdate.name,
149 oldPrivacy,
150 isNewVideo
151 })
143 } catch (err) { 152 } catch (err) {
144 // Force fields we want to update 153 // Force fields we want to update
145 // If the transaction is retried, sequelize will think the object has not changed 154 // If the transaction is retried, sequelize will think the object has not changed
@@ -147,6 +156,8 @@ async function updateVideo (req: express.Request, res: express.Response) {
147 resetSequelizeInstance(videoFromReq, videoFieldsSave) 156 resetSequelizeInstance(videoFromReq, videoFieldsSave)
148 157
149 throw err 158 throw err
159 } finally {
160 videoFileLockReleaser()
150 } 161 }
151 162
152 return res.type('json') 163 return res.type('json')
@@ -164,7 +175,7 @@ async function updateVideoPrivacy (options: {
164 const isNewVideo = videoInstance.isNewVideo(videoInfoToUpdate.privacy) 175 const isNewVideo = videoInstance.isNewVideo(videoInfoToUpdate.privacy)
165 176
166 const newPrivacy = parseInt(videoInfoToUpdate.privacy.toString(), 10) 177 const newPrivacy = parseInt(videoInfoToUpdate.privacy.toString(), 10)
167 videoInstance.setPrivacy(newPrivacy) 178 setVideoPrivacy(videoInstance, newPrivacy)
168 179
169 // Unfederate the video if the new privacy is not compatible with federation 180 // Unfederate the video if the new privacy is not compatible with federation
170 if (hadPrivacyForFederation && !videoInstance.hasPrivacyForFederation()) { 181 if (hadPrivacyForFederation && !videoInstance.hasPrivacyForFederation()) {
@@ -185,50 +196,3 @@ function updateSchedule (videoInstance: MVideoFullLight, videoInfoToUpdate: Vide
185 return ScheduleVideoUpdateModel.deleteByVideoId(videoInstance.id, transaction) 196 return ScheduleVideoUpdateModel.deleteByVideoId(videoInstance.id, transaction)
186 } 197 }
187} 198}
188
189async function addVideoJobsAfterUpdate (options: {
190 video: MVideoFullLight
191 videoInfoToUpdate: VideoUpdate
192 wasConfidentialVideo: boolean
193 isNewVideo: boolean
194}) {
195 const { video, videoInfoToUpdate, wasConfidentialVideo, isNewVideo } = options
196 const jobs: CreateJobArgument[] = []
197
198 if (!video.isLive && videoInfoToUpdate.name) {
199
200 for (const file of (video.VideoFiles || [])) {
201 const payload: ManageVideoTorrentPayload = { action: 'update-metadata', videoId: video.id, videoFileId: file.id }
202
203 jobs.push({ type: 'manage-video-torrent', payload })
204 }
205
206 const hls = video.getHLSPlaylist()
207
208 for (const file of (hls?.VideoFiles || [])) {
209 const payload: ManageVideoTorrentPayload = { action: 'update-metadata', streamingPlaylistId: hls.id, videoFileId: file.id }
210
211 jobs.push({ type: 'manage-video-torrent', payload })
212 }
213 }
214
215 jobs.push({
216 type: 'federate-video',
217 payload: {
218 videoUUID: video.uuid,
219 isNewVideo
220 }
221 })
222
223 if (wasConfidentialVideo) {
224 jobs.push({
225 type: 'notify',
226 payload: {
227 action: 'new-video',
228 videoUUID: video.uuid
229 }
230 })
231 }
232
233 return JobQueue.Instance.createSequentialJobFlow(...jobs)
234}