1 import express from 'express'
2 import { Transaction } from 'sequelize/types'
3 import { updateTorrentMetadata } from '@server/helpers/webtorrent'
4 import { changeVideoChannelShare } from '@server/lib/activitypub/share'
5 import { buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video'
6 import { openapiOperationDoc } from '@server/middlewares/doc'
7 import { FilteredModelAttributes } from '@server/types'
8 import { MVideoFullLight } from '@server/types/models'
9 import { VideoUpdate } from '../../../../shared'
10 import { HttpStatusCode } from '../../../../shared/models'
11 import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger'
12 import { resetSequelizeInstance } from '../../../helpers/database-utils'
13 import { createReqFiles } from '../../../helpers/express-utils'
14 import { logger, loggerTagsFactory } from '../../../helpers/logger'
15 import { CONFIG } from '../../../initializers/config'
16 import { MIMETYPES } from '../../../initializers/constants'
17 import { sequelizeTypescript } from '../../../initializers/database'
18 import { federateVideoIfNeeded } from '../../../lib/activitypub/videos'
19 import { Notifier } from '../../../lib/notifier'
20 import { Hooks } from '../../../lib/plugins/hooks'
21 import { autoBlacklistVideoIfNeeded } from '../../../lib/video-blacklist'
22 import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videosUpdateValidator } from '../../../middlewares'
23 import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update'
24 import { VideoModel } from '../../../models/video/video'
26 const lTags = loggerTagsFactory('api', 'video')
27 const auditLogger = auditLoggerFactory('videos')
28 const updateRouter = express.Router()
30 const reqVideoFileUpdate = createReqFiles(
31 [ 'thumbnailfile', 'previewfile' ],
32 MIMETYPES.IMAGE.MIMETYPE_EXT,
34 thumbnailfile: CONFIG.STORAGE.TMP_DIR,
35 previewfile: CONFIG.STORAGE.TMP_DIR
39 updateRouter.put('/:id',
40 openapiOperationDoc({ operationId: 'putVideo' }),
43 asyncMiddleware(videosUpdateValidator),
44 asyncRetryTransactionMiddleware(updateVideo)
47 // ---------------------------------------------------------------------------
53 // ---------------------------------------------------------------------------
55 async function updateVideo (req: express.Request, res: express.Response) {
56 const videoFromReq = res.locals.videoAll
57 const videoFieldsSave = videoFromReq.toJSON()
58 const oldVideoAuditView = new VideoAuditView(videoFromReq.toFormattedDetailsJSON())
59 const videoInfoToUpdate: VideoUpdate = req.body
61 const wasConfidentialVideo = videoFromReq.isConfidential()
62 const hadPrivacyForFederation = videoFromReq.hasPrivacyForFederation()
64 const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({
67 fallback: () => Promise.resolve(undefined),
68 automaticallyGenerated: false
72 const { videoInstanceUpdated, isNewVideo } = await sequelizeTypescript.transaction(async t => {
73 // Refresh video since thumbnails to prevent concurrent updates
74 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoFromReq.id, t)
76 const sequelizeOptions = { transaction: t }
77 const oldVideoChannel = video.VideoChannel
79 const keysToUpdate: (keyof VideoUpdate & FilteredModelAttributes<VideoModel>)[] = [
92 for (const key of keysToUpdate) {
93 if (videoInfoToUpdate[key] !== undefined) video.set(key, videoInfoToUpdate[key])
96 if (videoInfoToUpdate.originallyPublishedAt !== undefined && videoInfoToUpdate.originallyPublishedAt !== null) {
97 video.originallyPublishedAt = new Date(videoInfoToUpdate.originallyPublishedAt)
101 let isNewVideo = false
102 if (videoInfoToUpdate.privacy !== undefined) {
103 isNewVideo = await updateVideoPrivacy({ videoInstance: video, videoInfoToUpdate, hadPrivacyForFederation, transaction: t })
106 // Force updatedAt attribute change
107 if (!video.changed()) {
108 await video.setAsRefreshed(t)
111 const videoInstanceUpdated = await video.save(sequelizeOptions) as MVideoFullLight
113 // Thumbnail & preview updates?
114 if (thumbnailModel) await videoInstanceUpdated.addAndSaveThumbnail(thumbnailModel, t)
115 if (previewModel) await videoInstanceUpdated.addAndSaveThumbnail(previewModel, t)
117 // Video tags update?
118 if (videoInfoToUpdate.tags !== undefined) {
119 await setVideoTags({ video: videoInstanceUpdated, tags: videoInfoToUpdate.tags, transaction: t })
122 // Video channel update?
123 if (res.locals.videoChannel && videoInstanceUpdated.channelId !== res.locals.videoChannel.id) {
124 await videoInstanceUpdated.$set('VideoChannel', res.locals.videoChannel, { transaction: t })
125 videoInstanceUpdated.VideoChannel = res.locals.videoChannel
127 if (hadPrivacyForFederation === true) await changeVideoChannelShare(videoInstanceUpdated, oldVideoChannel, t)
130 // Schedule an update in the future?
131 await updateSchedule(videoInstanceUpdated, videoInfoToUpdate, t)
133 await autoBlacklistVideoIfNeeded({
134 video: videoInstanceUpdated,
135 user: res.locals.oauth.token.User,
142 getAuditIdFromRes(res),
143 new VideoAuditView(videoInstanceUpdated.toFormattedDetailsJSON()),
146 logger.info('Video with name %s and uuid %s updated.', video.name, video.uuid, lTags(video.uuid))
148 return { videoInstanceUpdated, isNewVideo }
151 if (videoInfoToUpdate.name) await updateTorrentsMetadata(videoInstanceUpdated)
153 await federateVideoIfNeeded(videoInstanceUpdated, isNewVideo, undefined)
155 if (wasConfidentialVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(videoInstanceUpdated)
157 Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body, req, res })
159 // Force fields we want to update
160 // If the transaction is retried, sequelize will think the object has not changed
161 // So it will skip the SQL request, even if the last one was ROLLBACKed!
162 resetSequelizeInstance(videoFromReq, videoFieldsSave)
167 return res.type('json')
168 .status(HttpStatusCode.NO_CONTENT_204)
172 async function updateVideoPrivacy (options: {
173 videoInstance: MVideoFullLight
174 videoInfoToUpdate: VideoUpdate
175 hadPrivacyForFederation: boolean
176 transaction: Transaction
178 const { videoInstance, videoInfoToUpdate, hadPrivacyForFederation, transaction } = options
179 const isNewVideo = videoInstance.isNewVideo(videoInfoToUpdate.privacy)
181 const newPrivacy = parseInt(videoInfoToUpdate.privacy.toString(), 10)
182 videoInstance.setPrivacy(newPrivacy)
184 // Unfederate the video if the new privacy is not compatible with federation
185 if (hadPrivacyForFederation && !videoInstance.hasPrivacyForFederation()) {
186 await VideoModel.sendDelete(videoInstance, { transaction })
192 function updateSchedule (videoInstance: MVideoFullLight, videoInfoToUpdate: VideoUpdate, transaction: Transaction) {
193 if (videoInfoToUpdate.scheduleUpdate) {
194 return ScheduleVideoUpdateModel.upsert({
195 videoId: videoInstance.id,
196 updateAt: new Date(videoInfoToUpdate.scheduleUpdate.updateAt),
197 privacy: videoInfoToUpdate.scheduleUpdate.privacy || null
199 } else if (videoInfoToUpdate.scheduleUpdate === null) {
200 return ScheduleVideoUpdateModel.deleteByVideoId(videoInstance.id, transaction)
204 async function updateTorrentsMetadata (video: MVideoFullLight) {
205 for (const file of video.getAllFiles()) {
206 await updateTorrentMetadata(video, file)