- return res.json({
- video: {
- id: videoCreated.id,
- uuid: videoCreated.uuid
- }
- }).end()
-}
-
-async function updateVideo (req: express.Request, res: express.Response) {
- const videoInstance = res.locals.video
- const videoFieldsSave = videoInstance.toJSON()
- const oldVideoAuditView = new VideoAuditView(videoInstance.toFormattedDetailsJSON())
- const videoInfoToUpdate: VideoUpdate = req.body
- const wasPrivateVideo = videoInstance.privacy === VideoPrivacy.PRIVATE
- const wasUnlistedVideo = videoInstance.privacy === VideoPrivacy.UNLISTED
-
- // Process thumbnail or create it from the video
- const thumbnailModel = req.files && req.files['thumbnailfile']
- ? await createVideoMiniatureFromExisting(req.files['thumbnailfile'][0].path, videoInstance, ThumbnailType.MINIATURE)
- : undefined
-
- const previewModel = req.files && req.files['previewfile']
- ? await createVideoMiniatureFromExisting(req.files['previewfile'][0].path, videoInstance, ThumbnailType.PREVIEW)
- : undefined
-
- try {
- const videoInstanceUpdated = await sequelizeTypescript.transaction(async t => {
- const sequelizeOptions = { transaction: t }
- const oldVideoChannel = videoInstance.VideoChannel
-
- if (videoInfoToUpdate.name !== undefined) videoInstance.set('name', videoInfoToUpdate.name)
- if (videoInfoToUpdate.category !== undefined) videoInstance.set('category', videoInfoToUpdate.category)
- if (videoInfoToUpdate.licence !== undefined) videoInstance.set('licence', videoInfoToUpdate.licence)
- if (videoInfoToUpdate.language !== undefined) videoInstance.set('language', videoInfoToUpdate.language)
- if (videoInfoToUpdate.nsfw !== undefined) videoInstance.set('nsfw', videoInfoToUpdate.nsfw)
- if (videoInfoToUpdate.waitTranscoding !== undefined) videoInstance.set('waitTranscoding', videoInfoToUpdate.waitTranscoding)
- if (videoInfoToUpdate.support !== undefined) videoInstance.set('support', videoInfoToUpdate.support)
- if (videoInfoToUpdate.description !== undefined) videoInstance.set('description', videoInfoToUpdate.description)
- if (videoInfoToUpdate.commentsEnabled !== undefined) videoInstance.set('commentsEnabled', videoInfoToUpdate.commentsEnabled)
- if (videoInfoToUpdate.downloadEnabled !== undefined) videoInstance.set('downloadEnabled', videoInfoToUpdate.downloadEnabled)
-
- if (videoInfoToUpdate.originallyPublishedAt !== undefined && videoInfoToUpdate.originallyPublishedAt !== null) {
- videoInstance.originallyPublishedAt = new Date(videoInfoToUpdate.originallyPublishedAt)
- }
-
- if (videoInfoToUpdate.privacy !== undefined) {
- const newPrivacy = parseInt(videoInfoToUpdate.privacy.toString(), 10)
- videoInstance.privacy = newPrivacy
-
- if (wasPrivateVideo === true && newPrivacy !== VideoPrivacy.PRIVATE) {
- videoInstance.publishedAt = new Date()
- }
- }
-
- const videoInstanceUpdated = await videoInstance.save(sequelizeOptions)
-
- if (thumbnailModel) await videoInstanceUpdated.addAndSaveThumbnail(thumbnailModel, t)
- if (previewModel) await videoInstanceUpdated.addAndSaveThumbnail(previewModel, t)
-
- // Video tags update?
- if (videoInfoToUpdate.tags !== undefined) {
- const tagInstances = await TagModel.findOrCreateTags(videoInfoToUpdate.tags, t)
-
- await videoInstanceUpdated.$set('Tags', tagInstances, sequelizeOptions)
- videoInstanceUpdated.Tags = tagInstances
- }
-
- // Video channel update?
- if (res.locals.videoChannel && videoInstanceUpdated.channelId !== res.locals.videoChannel.id) {
- await videoInstanceUpdated.$set('VideoChannel', res.locals.videoChannel, { transaction: t })
- videoInstanceUpdated.VideoChannel = res.locals.videoChannel
-
- if (wasPrivateVideo === false) await changeVideoChannelShare(videoInstanceUpdated, oldVideoChannel, t)
- }
-
- // Schedule an update in the future?
- if (videoInfoToUpdate.scheduleUpdate) {
- await ScheduleVideoUpdateModel.upsert({
- videoId: videoInstanceUpdated.id,
- updateAt: videoInfoToUpdate.scheduleUpdate.updateAt,
- privacy: videoInfoToUpdate.scheduleUpdate.privacy || null
- }, { transaction: t })
- } else if (videoInfoToUpdate.scheduleUpdate === null) {
- await ScheduleVideoUpdateModel.deleteByVideoId(videoInstanceUpdated.id, t)
- }
-
- const isNewVideo = wasPrivateVideo && videoInstanceUpdated.privacy !== VideoPrivacy.PRIVATE
-
- // Don't send update if the video was unfederated
- if (!videoInstanceUpdated.VideoBlacklist || videoInstanceUpdated.VideoBlacklist.unfederated === false) {
- await federateVideoIfNeeded(videoInstanceUpdated, isNewVideo, t)
- }
-
- auditLogger.update(
- getAuditIdFromRes(res),
- new VideoAuditView(videoInstanceUpdated.toFormattedDetailsJSON()),
- oldVideoAuditView
- )
- logger.info('Video with name %s and uuid %s updated.', videoInstance.name, videoInstance.uuid)
-
- return videoInstanceUpdated
- })
-
- if (wasUnlistedVideo || wasPrivateVideo) {
- Notifier.Instance.notifyOnNewVideo(videoInstanceUpdated)
- }
- } catch (err) {
- // Force fields we want to update
- // If the transaction is retried, sequelize will think the object has not changed
- // So it will skip the SQL request, even if the last one was ROLLBACKed!
- resetSequelizeInstance(videoInstance, videoFieldsSave)
-
- throw err