]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
c158a5fa | 2 | import { Transaction } from 'sequelize/types' |
9b293cd6 | 3 | import { updateTorrentMetadata } from '@server/helpers/webtorrent' |
c158a5fa C |
4 | import { changeVideoChannelShare } from '@server/lib/activitypub/share' |
5 | import { buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video' | |
c0e8b12e | 6 | import { openapiOperationDoc } from '@server/middlewares/doc' |
c158a5fa C |
7 | import { FilteredModelAttributes } from '@server/types' |
8 | import { MVideoFullLight } from '@server/types/models' | |
9 | import { VideoUpdate } from '../../../../shared' | |
c0e8b12e | 10 | import { HttpStatusCode } from '../../../../shared/models' |
c158a5fa C |
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' | |
25 | ||
26 | const lTags = loggerTagsFactory('api', 'video') | |
27 | const auditLogger = auditLoggerFactory('videos') | |
28 | const updateRouter = express.Router() | |
29 | ||
30 | const reqVideoFileUpdate = createReqFiles( | |
31 | [ 'thumbnailfile', 'previewfile' ], | |
32 | MIMETYPES.IMAGE.MIMETYPE_EXT, | |
33 | { | |
34 | thumbnailfile: CONFIG.STORAGE.TMP_DIR, | |
35 | previewfile: CONFIG.STORAGE.TMP_DIR | |
36 | } | |
37 | ) | |
38 | ||
39 | updateRouter.put('/:id', | |
1c627fd8 | 40 | openapiOperationDoc({ operationId: 'putVideo' }), |
c158a5fa C |
41 | authenticate, |
42 | reqVideoFileUpdate, | |
43 | asyncMiddleware(videosUpdateValidator), | |
44 | asyncRetryTransactionMiddleware(updateVideo) | |
45 | ) | |
46 | ||
47 | // --------------------------------------------------------------------------- | |
48 | ||
49 | export { | |
50 | updateRouter | |
51 | } | |
52 | ||
53 | // --------------------------------------------------------------------------- | |
54 | ||
b46cf4b9 | 55 | async function updateVideo (req: express.Request, res: express.Response) { |
a2a81f5a C |
56 | const videoFromReq = res.locals.videoAll |
57 | const videoFieldsSave = videoFromReq.toJSON() | |
58 | const oldVideoAuditView = new VideoAuditView(videoFromReq.toFormattedDetailsJSON()) | |
c158a5fa C |
59 | const videoInfoToUpdate: VideoUpdate = req.body |
60 | ||
a2a81f5a C |
61 | const wasConfidentialVideo = videoFromReq.isConfidential() |
62 | const hadPrivacyForFederation = videoFromReq.hasPrivacyForFederation() | |
c158a5fa C |
63 | |
64 | const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({ | |
a2a81f5a | 65 | video: videoFromReq, |
c158a5fa C |
66 | files: req.files, |
67 | fallback: () => Promise.resolve(undefined), | |
68 | automaticallyGenerated: false | |
69 | }) | |
70 | ||
71 | try { | |
38d69d65 | 72 | const { videoInstanceUpdated, isNewVideo } = await sequelizeTypescript.transaction(async t => { |
a2a81f5a C |
73 | // Refresh video since thumbnails to prevent concurrent updates |
74 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoFromReq.id, t) | |
75 | ||
c158a5fa | 76 | const sequelizeOptions = { transaction: t } |
a2a81f5a | 77 | const oldVideoChannel = video.VideoChannel |
c158a5fa C |
78 | |
79 | const keysToUpdate: (keyof VideoUpdate & FilteredModelAttributes<VideoModel>)[] = [ | |
80 | 'name', | |
81 | 'category', | |
82 | 'licence', | |
83 | 'language', | |
84 | 'nsfw', | |
85 | 'waitTranscoding', | |
86 | 'support', | |
87 | 'description', | |
88 | 'commentsEnabled', | |
89 | 'downloadEnabled' | |
90 | ] | |
91 | ||
92 | for (const key of keysToUpdate) { | |
a2a81f5a | 93 | if (videoInfoToUpdate[key] !== undefined) video.set(key, videoInfoToUpdate[key]) |
c158a5fa C |
94 | } |
95 | ||
96 | if (videoInfoToUpdate.originallyPublishedAt !== undefined && videoInfoToUpdate.originallyPublishedAt !== null) { | |
a2a81f5a | 97 | video.originallyPublishedAt = new Date(videoInfoToUpdate.originallyPublishedAt) |
c158a5fa C |
98 | } |
99 | ||
100 | // Privacy update? | |
101 | let isNewVideo = false | |
102 | if (videoInfoToUpdate.privacy !== undefined) { | |
a2a81f5a | 103 | isNewVideo = await updateVideoPrivacy({ videoInstance: video, videoInfoToUpdate, hadPrivacyForFederation, transaction: t }) |
c158a5fa C |
104 | } |
105 | ||
5cf027bd | 106 | // Force updatedAt attribute change |
a2a81f5a | 107 | if (!video.changed()) { |
597f771f | 108 | await video.setAsRefreshed(t) |
5cf027bd C |
109 | } |
110 | ||
a2a81f5a | 111 | const videoInstanceUpdated = await video.save(sequelizeOptions) as MVideoFullLight |
c158a5fa C |
112 | |
113 | // Thumbnail & preview updates? | |
114 | if (thumbnailModel) await videoInstanceUpdated.addAndSaveThumbnail(thumbnailModel, t) | |
115 | if (previewModel) await videoInstanceUpdated.addAndSaveThumbnail(previewModel, t) | |
116 | ||
117 | // Video tags update? | |
118 | if (videoInfoToUpdate.tags !== undefined) { | |
119 | await setVideoTags({ video: videoInstanceUpdated, tags: videoInfoToUpdate.tags, transaction: t }) | |
120 | } | |
121 | ||
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 | |
126 | ||
127 | if (hadPrivacyForFederation === true) await changeVideoChannelShare(videoInstanceUpdated, oldVideoChannel, t) | |
128 | } | |
129 | ||
130 | // Schedule an update in the future? | |
131 | await updateSchedule(videoInstanceUpdated, videoInfoToUpdate, t) | |
132 | ||
133 | await autoBlacklistVideoIfNeeded({ | |
134 | video: videoInstanceUpdated, | |
135 | user: res.locals.oauth.token.User, | |
136 | isRemote: false, | |
137 | isNew: false, | |
138 | transaction: t | |
139 | }) | |
140 | ||
c158a5fa C |
141 | auditLogger.update( |
142 | getAuditIdFromRes(res), | |
143 | new VideoAuditView(videoInstanceUpdated.toFormattedDetailsJSON()), | |
144 | oldVideoAuditView | |
145 | ) | |
a2a81f5a | 146 | logger.info('Video with name %s and uuid %s updated.', video.name, video.uuid, lTags(video.uuid)) |
c158a5fa | 147 | |
38d69d65 | 148 | return { videoInstanceUpdated, isNewVideo } |
c158a5fa C |
149 | }) |
150 | ||
9b293cd6 | 151 | if (videoInfoToUpdate.name) await updateTorrentsMetadata(videoInstanceUpdated) |
38d69d65 C |
152 | |
153 | await federateVideoIfNeeded(videoInstanceUpdated, isNewVideo, undefined) | |
154 | ||
9b293cd6 | 155 | if (wasConfidentialVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(videoInstanceUpdated) |
c158a5fa | 156 | |
7226e90f | 157 | Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body, req, res }) |
c158a5fa C |
158 | } catch (err) { |
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! | |
a2a81f5a | 162 | resetSequelizeInstance(videoFromReq, videoFieldsSave) |
c158a5fa C |
163 | |
164 | throw err | |
165 | } | |
166 | ||
167 | return res.type('json') | |
168 | .status(HttpStatusCode.NO_CONTENT_204) | |
169 | .end() | |
170 | } | |
171 | ||
172 | async function updateVideoPrivacy (options: { | |
173 | videoInstance: MVideoFullLight | |
174 | videoInfoToUpdate: VideoUpdate | |
175 | hadPrivacyForFederation: boolean | |
176 | transaction: Transaction | |
177 | }) { | |
178 | const { videoInstance, videoInfoToUpdate, hadPrivacyForFederation, transaction } = options | |
179 | const isNewVideo = videoInstance.isNewVideo(videoInfoToUpdate.privacy) | |
180 | ||
181 | const newPrivacy = parseInt(videoInfoToUpdate.privacy.toString(), 10) | |
182 | videoInstance.setPrivacy(newPrivacy) | |
183 | ||
184 | // Unfederate the video if the new privacy is not compatible with federation | |
185 | if (hadPrivacyForFederation && !videoInstance.hasPrivacyForFederation()) { | |
186 | await VideoModel.sendDelete(videoInstance, { transaction }) | |
187 | } | |
188 | ||
189 | return isNewVideo | |
190 | } | |
191 | ||
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 | |
198 | }, { transaction }) | |
199 | } else if (videoInfoToUpdate.scheduleUpdate === null) { | |
200 | return ScheduleVideoUpdateModel.deleteByVideoId(videoInstance.id, transaction) | |
201 | } | |
202 | } | |
9b293cd6 C |
203 | |
204 | async function updateTorrentsMetadata (video: MVideoFullLight) { | |
6ba93fa6 | 205 | for (const file of (video.VideoFiles || [])) { |
9b293cd6 | 206 | await updateTorrentMetadata(video, file) |
38d69d65 C |
207 | |
208 | await file.save() | |
9b293cd6 | 209 | } |
6ba93fa6 C |
210 | |
211 | const hls = video.getHLSPlaylist() | |
212 | if (!hls) return | |
213 | ||
214 | for (const file of (hls.VideoFiles || [])) { | |
215 | await updateTorrentMetadata(hls, file) | |
216 | ||
217 | await file.save() | |
218 | } | |
9b293cd6 | 219 | } |