]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/update.ts
Prevent broken transcoding with audio only input
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / update.ts
1 import express from 'express'
2 import { Transaction } from 'sequelize/types'
3 import { changeVideoChannelShare } from '@server/lib/activitypub/share'
4 import { addVideoJobsAfterUpdate, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video'
5 import { setVideoPrivacy } from '@server/lib/video-privacy'
6 import { openapiOperationDoc } from '@server/middlewares/doc'
7 import { FilteredModelAttributes } from '@server/types'
8 import { MVideoFullLight } from '@server/types/models'
9 import { HttpStatusCode, VideoUpdate } from '@shared/models'
10 import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger'
11 import { resetSequelizeInstance } from '../../../helpers/database-utils'
12 import { createReqFiles } from '../../../helpers/express-utils'
13 import { logger, loggerTagsFactory } from '../../../helpers/logger'
14 import { MIMETYPES } from '../../../initializers/constants'
15 import { sequelizeTypescript } from '../../../initializers/database'
16 import { Hooks } from '../../../lib/plugins/hooks'
17 import { autoBlacklistVideoIfNeeded } from '../../../lib/video-blacklist'
18 import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videosUpdateValidator } from '../../../middlewares'
19 import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update'
20 import { VideoModel } from '../../../models/video/video'
21 import { VideoPathManager } from '@server/lib/video-path-manager'
22
23 const lTags = loggerTagsFactory('api', 'video')
24 const auditLogger = auditLoggerFactory('videos')
25 const updateRouter = express.Router()
26
27 const reqVideoFileUpdate = createReqFiles([ 'thumbnailfile', 'previewfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT)
28
29 updateRouter.put('/:id',
30 openapiOperationDoc({ operationId: 'putVideo' }),
31 authenticate,
32 reqVideoFileUpdate,
33 asyncMiddleware(videosUpdateValidator),
34 asyncRetryTransactionMiddleware(updateVideo)
35 )
36
37 // ---------------------------------------------------------------------------
38
39 export {
40 updateRouter
41 }
42
43 // ---------------------------------------------------------------------------
44
45 async function updateVideo (req: express.Request, res: express.Response) {
46 const videoFromReq = res.locals.videoAll
47 const videoFieldsSave = videoFromReq.toJSON()
48 const oldVideoAuditView = new VideoAuditView(videoFromReq.toFormattedDetailsJSON())
49 const videoInfoToUpdate: VideoUpdate = req.body
50
51 const hadPrivacyForFederation = videoFromReq.hasPrivacyForFederation()
52 const oldPrivacy = videoFromReq.privacy
53
54 const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({
55 video: videoFromReq,
56 files: req.files,
57 fallback: () => Promise.resolve(undefined),
58 automaticallyGenerated: false
59 })
60
61 const videoFileLockReleaser = await VideoPathManager.Instance.lockFiles(videoFromReq.uuid)
62
63 try {
64 const { videoInstanceUpdated, isNewVideo } = await sequelizeTypescript.transaction(async t => {
65 // Refresh video since thumbnails to prevent concurrent updates
66 const video = await VideoModel.loadFull(videoFromReq.id, t)
67
68 const oldVideoChannel = video.VideoChannel
69
70 const keysToUpdate: (keyof VideoUpdate & FilteredModelAttributes<VideoModel>)[] = [
71 'name',
72 'category',
73 'licence',
74 'language',
75 'nsfw',
76 'waitTranscoding',
77 'support',
78 'description',
79 'commentsEnabled',
80 'downloadEnabled'
81 ]
82
83 for (const key of keysToUpdate) {
84 if (videoInfoToUpdate[key] !== undefined) video.set(key, videoInfoToUpdate[key])
85 }
86
87 if (videoInfoToUpdate.originallyPublishedAt !== undefined && videoInfoToUpdate.originallyPublishedAt !== null) {
88 video.originallyPublishedAt = new Date(videoInfoToUpdate.originallyPublishedAt)
89 }
90
91 // Privacy update?
92 let isNewVideo = false
93 if (videoInfoToUpdate.privacy !== undefined) {
94 isNewVideo = await updateVideoPrivacy({ videoInstance: video, videoInfoToUpdate, hadPrivacyForFederation, transaction: t })
95 }
96
97 // Force updatedAt attribute change
98 if (!video.changed()) {
99 await video.setAsRefreshed(t)
100 }
101
102 const videoInstanceUpdated = await video.save({ transaction: t }) as MVideoFullLight
103
104 // Thumbnail & preview updates?
105 if (thumbnailModel) await videoInstanceUpdated.addAndSaveThumbnail(thumbnailModel, t)
106 if (previewModel) await videoInstanceUpdated.addAndSaveThumbnail(previewModel, t)
107
108 // Video tags update?
109 if (videoInfoToUpdate.tags !== undefined) {
110 await setVideoTags({ video: videoInstanceUpdated, tags: videoInfoToUpdate.tags, transaction: t })
111 }
112
113 // Video channel update?
114 if (res.locals.videoChannel && videoInstanceUpdated.channelId !== res.locals.videoChannel.id) {
115 await videoInstanceUpdated.$set('VideoChannel', res.locals.videoChannel, { transaction: t })
116 videoInstanceUpdated.VideoChannel = res.locals.videoChannel
117
118 if (hadPrivacyForFederation === true) {
119 await changeVideoChannelShare(videoInstanceUpdated, oldVideoChannel, t)
120 }
121 }
122
123 // Schedule an update in the future?
124 await updateSchedule(videoInstanceUpdated, videoInfoToUpdate, t)
125
126 await autoBlacklistVideoIfNeeded({
127 video: videoInstanceUpdated,
128 user: res.locals.oauth.token.User,
129 isRemote: false,
130 isNew: false,
131 transaction: t
132 })
133
134 auditLogger.update(
135 getAuditIdFromRes(res),
136 new VideoAuditView(videoInstanceUpdated.toFormattedDetailsJSON()),
137 oldVideoAuditView
138 )
139 logger.info('Video with name %s and uuid %s updated.', video.name, video.uuid, lTags(video.uuid))
140
141 return { videoInstanceUpdated, isNewVideo }
142 })
143
144 Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body, req, res })
145
146 await addVideoJobsAfterUpdate({
147 video: videoInstanceUpdated,
148 nameChanged: !!videoInfoToUpdate.name,
149 oldPrivacy,
150 isNewVideo
151 })
152 } catch (err) {
153 // Force fields we want to update
154 // If the transaction is retried, sequelize will think the object has not changed
155 // So it will skip the SQL request, even if the last one was ROLLBACKed!
156 resetSequelizeInstance(videoFromReq, videoFieldsSave)
157
158 throw err
159 } finally {
160 videoFileLockReleaser()
161 }
162
163 return res.type('json')
164 .status(HttpStatusCode.NO_CONTENT_204)
165 .end()
166 }
167
168 async function updateVideoPrivacy (options: {
169 videoInstance: MVideoFullLight
170 videoInfoToUpdate: VideoUpdate
171 hadPrivacyForFederation: boolean
172 transaction: Transaction
173 }) {
174 const { videoInstance, videoInfoToUpdate, hadPrivacyForFederation, transaction } = options
175 const isNewVideo = videoInstance.isNewVideo(videoInfoToUpdate.privacy)
176
177 const newPrivacy = parseInt(videoInfoToUpdate.privacy.toString(), 10)
178 setVideoPrivacy(videoInstance, newPrivacy)
179
180 // Unfederate the video if the new privacy is not compatible with federation
181 if (hadPrivacyForFederation && !videoInstance.hasPrivacyForFederation()) {
182 await VideoModel.sendDelete(videoInstance, { transaction })
183 }
184
185 return isNewVideo
186 }
187
188 function updateSchedule (videoInstance: MVideoFullLight, videoInfoToUpdate: VideoUpdate, transaction: Transaction) {
189 if (videoInfoToUpdate.scheduleUpdate) {
190 return ScheduleVideoUpdateModel.upsert({
191 videoId: videoInstance.id,
192 updateAt: new Date(videoInfoToUpdate.scheduleUpdate.updateAt),
193 privacy: videoInfoToUpdate.scheduleUpdate.privacy || null
194 }, { transaction })
195 } else if (videoInfoToUpdate.scheduleUpdate === null) {
196 return ScheduleVideoUpdateModel.deleteByVideoId(videoInstance.id, transaction)
197 }
198 }