]>
Commit | Line | Data |
---|---|---|
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' | |
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', | |
40 | openapiOperationDoc({ operationId: 'putVideo' }), | |
41 | authenticate, | |
42 | reqVideoFileUpdate, | |
43 | asyncMiddleware(videosUpdateValidator), | |
44 | asyncRetryTransactionMiddleware(updateVideo) | |
45 | ) | |
46 | ||
47 | // --------------------------------------------------------------------------- | |
48 | ||
49 | export { | |
50 | updateRouter | |
51 | } | |
52 | ||
53 | // --------------------------------------------------------------------------- | |
54 | ||
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 | |
60 | ||
61 | const wasConfidentialVideo = videoFromReq.isConfidential() | |
62 | const hadPrivacyForFederation = videoFromReq.hasPrivacyForFederation() | |
63 | ||
64 | const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({ | |
65 | video: videoFromReq, | |
66 | files: req.files, | |
67 | fallback: () => Promise.resolve(undefined), | |
68 | automaticallyGenerated: false | |
69 | }) | |
70 | ||
71 | try { | |
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) | |
75 | ||
76 | const sequelizeOptions = { transaction: t } | |
77 | const oldVideoChannel = video.VideoChannel | |
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) { | |
93 | if (videoInfoToUpdate[key] !== undefined) video.set(key, videoInfoToUpdate[key]) | |
94 | } | |
95 | ||
96 | if (videoInfoToUpdate.originallyPublishedAt !== undefined && videoInfoToUpdate.originallyPublishedAt !== null) { | |
97 | video.originallyPublishedAt = new Date(videoInfoToUpdate.originallyPublishedAt) | |
98 | } | |
99 | ||
100 | // Privacy update? | |
101 | let isNewVideo = false | |
102 | if (videoInfoToUpdate.privacy !== undefined) { | |
103 | isNewVideo = await updateVideoPrivacy({ videoInstance: video, videoInfoToUpdate, hadPrivacyForFederation, transaction: t }) | |
104 | } | |
105 | ||
106 | // Force updatedAt attribute change | |
107 | if (!video.changed()) { | |
108 | await video.setAsRefreshed(t) | |
109 | } | |
110 | ||
111 | const videoInstanceUpdated = await video.save(sequelizeOptions) as MVideoFullLight | |
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 | ||
141 | auditLogger.update( | |
142 | getAuditIdFromRes(res), | |
143 | new VideoAuditView(videoInstanceUpdated.toFormattedDetailsJSON()), | |
144 | oldVideoAuditView | |
145 | ) | |
146 | logger.info('Video with name %s and uuid %s updated.', video.name, video.uuid, lTags(video.uuid)) | |
147 | ||
148 | return { videoInstanceUpdated, isNewVideo } | |
149 | }) | |
150 | ||
151 | if (videoInfoToUpdate.name) await updateTorrentsMetadata(videoInstanceUpdated) | |
152 | ||
153 | await federateVideoIfNeeded(videoInstanceUpdated, isNewVideo, undefined) | |
154 | ||
155 | if (wasConfidentialVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(videoInstanceUpdated) | |
156 | ||
157 | Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body, req, res }) | |
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! | |
162 | resetSequelizeInstance(videoFromReq, videoFieldsSave) | |
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 | } | |
203 | ||
204 | async function updateTorrentsMetadata (video: MVideoFullLight) { | |
205 | for (const file of (video.VideoFiles || [])) { | |
206 | await updateTorrentMetadata(video, file) | |
207 | ||
208 | await file.save() | |
209 | } | |
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 | } | |
219 | } |