diff options
Diffstat (limited to 'server/controllers/api/videos/index.ts')
-rw-r--r-- | server/controllers/api/videos/index.ts | 348 |
1 files changed, 20 insertions, 328 deletions
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index 6ec6478e4..6483d2e8a 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts | |||
@@ -1,41 +1,20 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { move } from 'fs-extra' | ||
3 | import { extname } from 'path' | ||
4 | import toInt from 'validator/lib/toInt' | 2 | import toInt from 'validator/lib/toInt' |
5 | import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' | ||
6 | import { changeVideoChannelShare } from '@server/lib/activitypub/share' | ||
7 | import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url' | ||
8 | import { LiveManager } from '@server/lib/live-manager' | 3 | import { LiveManager } from '@server/lib/live-manager' |
9 | import { addOptimizeOrMergeAudioJob, buildLocalVideoFromReq, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video' | ||
10 | import { generateVideoFilename, getVideoFilePath } from '@server/lib/video-paths' | ||
11 | import { getServerActor } from '@server/models/application/application' | 4 | import { getServerActor } from '@server/models/application/application' |
12 | import { MVideo, MVideoFile, MVideoFullLight } from '@server/types/models' | 5 | import { VideosCommonQuery } from '../../../../shared' |
13 | import { VideoCreate, VideosCommonQuery, VideoState, VideoUpdate } from '../../../../shared' | 6 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs' |
14 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' | ||
15 | import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' | 7 | import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' |
16 | import { resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers/database-utils' | 8 | import { buildNSFWFilter, getCountVideos } from '../../../helpers/express-utils' |
17 | import { buildNSFWFilter, createReqFiles, getCountVideos } from '../../../helpers/express-utils' | 9 | import { logger } from '../../../helpers/logger' |
18 | import { getMetadataFromFile, getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffprobe-utils' | ||
19 | import { logger, loggerTagsFactory } from '../../../helpers/logger' | ||
20 | import { getFormattedObjects } from '../../../helpers/utils' | 10 | import { getFormattedObjects } from '../../../helpers/utils' |
21 | import { CONFIG } from '../../../initializers/config' | 11 | import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../initializers/constants' |
22 | import { | ||
23 | DEFAULT_AUDIO_RESOLUTION, | ||
24 | MIMETYPES, | ||
25 | VIDEO_CATEGORIES, | ||
26 | VIDEO_LANGUAGES, | ||
27 | VIDEO_LICENCES, | ||
28 | VIDEO_PRIVACIES | ||
29 | } from '../../../initializers/constants' | ||
30 | import { sequelizeTypescript } from '../../../initializers/database' | 12 | import { sequelizeTypescript } from '../../../initializers/database' |
31 | import { sendView } from '../../../lib/activitypub/send/send-view' | 13 | import { sendView } from '../../../lib/activitypub/send/send-view' |
32 | import { federateVideoIfNeeded, fetchRemoteVideoDescription } from '../../../lib/activitypub/videos' | 14 | import { fetchRemoteVideoDescription } from '../../../lib/activitypub/videos' |
33 | import { JobQueue } from '../../../lib/job-queue' | 15 | import { JobQueue } from '../../../lib/job-queue' |
34 | import { Notifier } from '../../../lib/notifier' | ||
35 | import { Hooks } from '../../../lib/plugins/hooks' | 16 | import { Hooks } from '../../../lib/plugins/hooks' |
36 | import { Redis } from '../../../lib/redis' | 17 | import { Redis } from '../../../lib/redis' |
37 | import { generateVideoMiniature } from '../../../lib/thumbnail' | ||
38 | import { autoBlacklistVideoIfNeeded } from '../../../lib/video-blacklist' | ||
39 | import { | 18 | import { |
40 | asyncMiddleware, | 19 | asyncMiddleware, |
41 | asyncRetryTransactionMiddleware, | 20 | asyncRetryTransactionMiddleware, |
@@ -47,14 +26,11 @@ import { | |||
47 | setDefaultPagination, | 26 | setDefaultPagination, |
48 | setDefaultVideosSort, | 27 | setDefaultVideosSort, |
49 | videoFileMetadataGetValidator, | 28 | videoFileMetadataGetValidator, |
50 | videosAddValidator, | ||
51 | videosCustomGetValidator, | 29 | videosCustomGetValidator, |
52 | videosGetValidator, | 30 | videosGetValidator, |
53 | videosRemoveValidator, | 31 | videosRemoveValidator, |
54 | videosSortValidator, | 32 | videosSortValidator |
55 | videosUpdateValidator | ||
56 | } from '../../../middlewares' | 33 | } from '../../../middlewares' |
57 | import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update' | ||
58 | import { VideoModel } from '../../../models/video/video' | 34 | import { VideoModel } from '../../../models/video/video' |
59 | import { VideoFileModel } from '../../../models/video/video-file' | 35 | import { VideoFileModel } from '../../../models/video/video-file' |
60 | import { blacklistRouter } from './blacklist' | 36 | import { blacklistRouter } from './blacklist' |
@@ -64,30 +40,13 @@ import { videoImportsRouter } from './import' | |||
64 | import { liveRouter } from './live' | 40 | import { liveRouter } from './live' |
65 | import { ownershipVideoRouter } from './ownership' | 41 | import { ownershipVideoRouter } from './ownership' |
66 | import { rateVideoRouter } from './rate' | 42 | import { rateVideoRouter } from './rate' |
43 | import { updateRouter } from './update' | ||
44 | import { uploadRouter } from './upload' | ||
67 | import { watchingRouter } from './watching' | 45 | import { watchingRouter } from './watching' |
68 | 46 | ||
69 | const lTags = loggerTagsFactory('api', 'video') | ||
70 | const auditLogger = auditLoggerFactory('videos') | 47 | const auditLogger = auditLoggerFactory('videos') |
71 | const videosRouter = express.Router() | 48 | const videosRouter = express.Router() |
72 | 49 | ||
73 | const reqVideoFileAdd = createReqFiles( | ||
74 | [ 'videofile', 'thumbnailfile', 'previewfile' ], | ||
75 | Object.assign({}, MIMETYPES.VIDEO.MIMETYPE_EXT, MIMETYPES.IMAGE.MIMETYPE_EXT), | ||
76 | { | ||
77 | videofile: CONFIG.STORAGE.TMP_DIR, | ||
78 | thumbnailfile: CONFIG.STORAGE.TMP_DIR, | ||
79 | previewfile: CONFIG.STORAGE.TMP_DIR | ||
80 | } | ||
81 | ) | ||
82 | const reqVideoFileUpdate = createReqFiles( | ||
83 | [ 'thumbnailfile', 'previewfile' ], | ||
84 | MIMETYPES.IMAGE.MIMETYPE_EXT, | ||
85 | { | ||
86 | thumbnailfile: CONFIG.STORAGE.TMP_DIR, | ||
87 | previewfile: CONFIG.STORAGE.TMP_DIR | ||
88 | } | ||
89 | ) | ||
90 | |||
91 | videosRouter.use('/', blacklistRouter) | 50 | videosRouter.use('/', blacklistRouter) |
92 | videosRouter.use('/', rateVideoRouter) | 51 | videosRouter.use('/', rateVideoRouter) |
93 | videosRouter.use('/', videoCommentRouter) | 52 | videosRouter.use('/', videoCommentRouter) |
@@ -96,6 +55,8 @@ videosRouter.use('/', videoImportsRouter) | |||
96 | videosRouter.use('/', ownershipVideoRouter) | 55 | videosRouter.use('/', ownershipVideoRouter) |
97 | videosRouter.use('/', watchingRouter) | 56 | videosRouter.use('/', watchingRouter) |
98 | videosRouter.use('/', liveRouter) | 57 | videosRouter.use('/', liveRouter) |
58 | videosRouter.use('/', uploadRouter) | ||
59 | videosRouter.use('/', updateRouter) | ||
99 | 60 | ||
100 | videosRouter.get('/categories', listVideoCategories) | 61 | videosRouter.get('/categories', listVideoCategories) |
101 | videosRouter.get('/licences', listVideoLicences) | 62 | videosRouter.get('/licences', listVideoLicences) |
@@ -111,18 +72,6 @@ videosRouter.get('/', | |||
111 | commonVideosFiltersValidator, | 72 | commonVideosFiltersValidator, |
112 | asyncMiddleware(listVideos) | 73 | asyncMiddleware(listVideos) |
113 | ) | 74 | ) |
114 | videosRouter.put('/:id', | ||
115 | authenticate, | ||
116 | reqVideoFileUpdate, | ||
117 | asyncMiddleware(videosUpdateValidator), | ||
118 | asyncRetryTransactionMiddleware(updateVideo) | ||
119 | ) | ||
120 | videosRouter.post('/upload', | ||
121 | authenticate, | ||
122 | reqVideoFileAdd, | ||
123 | asyncMiddleware(videosAddValidator), | ||
124 | asyncRetryTransactionMiddleware(addVideo) | ||
125 | ) | ||
126 | 75 | ||
127 | videosRouter.get('/:id/description', | 76 | videosRouter.get('/:id/description', |
128 | asyncMiddleware(videosGetValidator), | 77 | asyncMiddleware(videosGetValidator), |
@@ -157,263 +106,23 @@ export { | |||
157 | 106 | ||
158 | // --------------------------------------------------------------------------- | 107 | // --------------------------------------------------------------------------- |
159 | 108 | ||
160 | function listVideoCategories (req: express.Request, res: express.Response) { | 109 | function listVideoCategories (_req: express.Request, res: express.Response) { |
161 | res.json(VIDEO_CATEGORIES) | 110 | res.json(VIDEO_CATEGORIES) |
162 | } | 111 | } |
163 | 112 | ||
164 | function listVideoLicences (req: express.Request, res: express.Response) { | 113 | function listVideoLicences (_req: express.Request, res: express.Response) { |
165 | res.json(VIDEO_LICENCES) | 114 | res.json(VIDEO_LICENCES) |
166 | } | 115 | } |
167 | 116 | ||
168 | function listVideoLanguages (req: express.Request, res: express.Response) { | 117 | function listVideoLanguages (_req: express.Request, res: express.Response) { |
169 | res.json(VIDEO_LANGUAGES) | 118 | res.json(VIDEO_LANGUAGES) |
170 | } | 119 | } |
171 | 120 | ||
172 | function listVideoPrivacies (req: express.Request, res: express.Response) { | 121 | function listVideoPrivacies (_req: express.Request, res: express.Response) { |
173 | res.json(VIDEO_PRIVACIES) | 122 | res.json(VIDEO_PRIVACIES) |
174 | } | 123 | } |
175 | 124 | ||
176 | async function addVideo (req: express.Request, res: express.Response) { | 125 | async function getVideo (_req: express.Request, res: express.Response) { |
177 | // Uploading the video could be long | ||
178 | // Set timeout to 10 minutes, as Express's default is 2 minutes | ||
179 | req.setTimeout(1000 * 60 * 10, () => { | ||
180 | logger.error('Upload video has timed out.') | ||
181 | return res.sendStatus(HttpStatusCode.REQUEST_TIMEOUT_408) | ||
182 | }) | ||
183 | |||
184 | const videoPhysicalFile = req.files['videofile'][0] | ||
185 | const videoInfo: VideoCreate = req.body | ||
186 | |||
187 | const videoData = buildLocalVideoFromReq(videoInfo, res.locals.videoChannel.id) | ||
188 | videoData.state = CONFIG.TRANSCODING.ENABLED ? VideoState.TO_TRANSCODE : VideoState.PUBLISHED | ||
189 | videoData.duration = videoPhysicalFile['duration'] // duration was added by a previous middleware | ||
190 | |||
191 | const video = new VideoModel(videoData) as MVideoFullLight | ||
192 | video.VideoChannel = res.locals.videoChannel | ||
193 | video.url = getLocalVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object | ||
194 | |||
195 | const videoFile = new VideoFileModel({ | ||
196 | extname: extname(videoPhysicalFile.filename), | ||
197 | size: videoPhysicalFile.size, | ||
198 | videoStreamingPlaylistId: null, | ||
199 | metadata: await getMetadataFromFile(videoPhysicalFile.path) | ||
200 | }) | ||
201 | |||
202 | if (videoFile.isAudio()) { | ||
203 | videoFile.resolution = DEFAULT_AUDIO_RESOLUTION | ||
204 | } else { | ||
205 | videoFile.fps = await getVideoFileFPS(videoPhysicalFile.path) | ||
206 | videoFile.resolution = (await getVideoFileResolution(videoPhysicalFile.path)).videoFileResolution | ||
207 | } | ||
208 | |||
209 | videoFile.filename = generateVideoFilename(video, false, videoFile.resolution, videoFile.extname) | ||
210 | |||
211 | // Move physical file | ||
212 | const destination = getVideoFilePath(video, videoFile) | ||
213 | await move(videoPhysicalFile.path, destination) | ||
214 | // This is important in case if there is another attempt in the retry process | ||
215 | videoPhysicalFile.filename = getVideoFilePath(video, videoFile) | ||
216 | videoPhysicalFile.path = destination | ||
217 | |||
218 | const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({ | ||
219 | video, | ||
220 | files: req.files, | ||
221 | fallback: type => generateVideoMiniature({ video, videoFile, type }) | ||
222 | }) | ||
223 | |||
224 | const { videoCreated } = await sequelizeTypescript.transaction(async t => { | ||
225 | const sequelizeOptions = { transaction: t } | ||
226 | |||
227 | const videoCreated = await video.save(sequelizeOptions) as MVideoFullLight | ||
228 | |||
229 | await videoCreated.addAndSaveThumbnail(thumbnailModel, t) | ||
230 | await videoCreated.addAndSaveThumbnail(previewModel, t) | ||
231 | |||
232 | // Do not forget to add video channel information to the created video | ||
233 | videoCreated.VideoChannel = res.locals.videoChannel | ||
234 | |||
235 | videoFile.videoId = video.id | ||
236 | await videoFile.save(sequelizeOptions) | ||
237 | |||
238 | video.VideoFiles = [ videoFile ] | ||
239 | |||
240 | await setVideoTags({ video, tags: videoInfo.tags, transaction: t }) | ||
241 | |||
242 | // Schedule an update in the future? | ||
243 | if (videoInfo.scheduleUpdate) { | ||
244 | await ScheduleVideoUpdateModel.create({ | ||
245 | videoId: video.id, | ||
246 | updateAt: videoInfo.scheduleUpdate.updateAt, | ||
247 | privacy: videoInfo.scheduleUpdate.privacy || null | ||
248 | }, { transaction: t }) | ||
249 | } | ||
250 | |||
251 | await autoBlacklistVideoIfNeeded({ | ||
252 | video, | ||
253 | user: res.locals.oauth.token.User, | ||
254 | isRemote: false, | ||
255 | isNew: true, | ||
256 | transaction: t | ||
257 | }) | ||
258 | |||
259 | auditLogger.create(getAuditIdFromRes(res), new VideoAuditView(videoCreated.toFormattedDetailsJSON())) | ||
260 | logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoCreated.uuid, lTags(videoCreated.uuid)) | ||
261 | |||
262 | return { videoCreated } | ||
263 | }) | ||
264 | |||
265 | // Create the torrent file in async way because it could be long | ||
266 | createTorrentAndSetInfoHashAsync(video, videoFile) | ||
267 | .catch(err => logger.error('Cannot create torrent file for video %s', video.url, { err, ...lTags(video.uuid) })) | ||
268 | .then(() => VideoModel.loadAndPopulateAccountAndServerAndTags(video.id)) | ||
269 | .then(refreshedVideo => { | ||
270 | if (!refreshedVideo) return | ||
271 | |||
272 | // Only federate and notify after the torrent creation | ||
273 | Notifier.Instance.notifyOnNewVideoIfNeeded(refreshedVideo) | ||
274 | |||
275 | return retryTransactionWrapper(() => { | ||
276 | return sequelizeTypescript.transaction(t => federateVideoIfNeeded(refreshedVideo, true, t)) | ||
277 | }) | ||
278 | }) | ||
279 | .catch(err => logger.error('Cannot federate or notify video creation %s', video.url, { err, ...lTags(video.uuid) })) | ||
280 | |||
281 | if (video.state === VideoState.TO_TRANSCODE) { | ||
282 | await addOptimizeOrMergeAudioJob(videoCreated, videoFile, res.locals.oauth.token.User) | ||
283 | } | ||
284 | |||
285 | Hooks.runAction('action:api.video.uploaded', { video: videoCreated }) | ||
286 | |||
287 | return res.json({ | ||
288 | video: { | ||
289 | id: videoCreated.id, | ||
290 | uuid: videoCreated.uuid | ||
291 | } | ||
292 | }) | ||
293 | } | ||
294 | |||
295 | async function updateVideo (req: express.Request, res: express.Response) { | ||
296 | const videoInstance = res.locals.videoAll | ||
297 | const videoFieldsSave = videoInstance.toJSON() | ||
298 | const oldVideoAuditView = new VideoAuditView(videoInstance.toFormattedDetailsJSON()) | ||
299 | const videoInfoToUpdate: VideoUpdate = req.body | ||
300 | |||
301 | const wasConfidentialVideo = videoInstance.isConfidential() | ||
302 | const hadPrivacyForFederation = videoInstance.hasPrivacyForFederation() | ||
303 | |||
304 | const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({ | ||
305 | video: videoInstance, | ||
306 | files: req.files, | ||
307 | fallback: () => Promise.resolve(undefined), | ||
308 | automaticallyGenerated: false | ||
309 | }) | ||
310 | |||
311 | try { | ||
312 | const videoInstanceUpdated = await sequelizeTypescript.transaction(async t => { | ||
313 | const sequelizeOptions = { transaction: t } | ||
314 | const oldVideoChannel = videoInstance.VideoChannel | ||
315 | |||
316 | if (videoInfoToUpdate.name !== undefined) videoInstance.name = videoInfoToUpdate.name | ||
317 | if (videoInfoToUpdate.category !== undefined) videoInstance.category = videoInfoToUpdate.category | ||
318 | if (videoInfoToUpdate.licence !== undefined) videoInstance.licence = videoInfoToUpdate.licence | ||
319 | if (videoInfoToUpdate.language !== undefined) videoInstance.language = videoInfoToUpdate.language | ||
320 | if (videoInfoToUpdate.nsfw !== undefined) videoInstance.nsfw = videoInfoToUpdate.nsfw | ||
321 | if (videoInfoToUpdate.waitTranscoding !== undefined) videoInstance.waitTranscoding = videoInfoToUpdate.waitTranscoding | ||
322 | if (videoInfoToUpdate.support !== undefined) videoInstance.support = videoInfoToUpdate.support | ||
323 | if (videoInfoToUpdate.description !== undefined) videoInstance.description = videoInfoToUpdate.description | ||
324 | if (videoInfoToUpdate.commentsEnabled !== undefined) videoInstance.commentsEnabled = videoInfoToUpdate.commentsEnabled | ||
325 | if (videoInfoToUpdate.downloadEnabled !== undefined) videoInstance.downloadEnabled = videoInfoToUpdate.downloadEnabled | ||
326 | |||
327 | if (videoInfoToUpdate.originallyPublishedAt !== undefined && videoInfoToUpdate.originallyPublishedAt !== null) { | ||
328 | videoInstance.originallyPublishedAt = new Date(videoInfoToUpdate.originallyPublishedAt) | ||
329 | } | ||
330 | |||
331 | let isNewVideo = false | ||
332 | if (videoInfoToUpdate.privacy !== undefined) { | ||
333 | isNewVideo = videoInstance.isNewVideo(videoInfoToUpdate.privacy) | ||
334 | |||
335 | const newPrivacy = parseInt(videoInfoToUpdate.privacy.toString(), 10) | ||
336 | videoInstance.setPrivacy(newPrivacy) | ||
337 | |||
338 | // Unfederate the video if the new privacy is not compatible with federation | ||
339 | if (hadPrivacyForFederation && !videoInstance.hasPrivacyForFederation()) { | ||
340 | await VideoModel.sendDelete(videoInstance, { transaction: t }) | ||
341 | } | ||
342 | } | ||
343 | |||
344 | const videoInstanceUpdated = await videoInstance.save(sequelizeOptions) as MVideoFullLight | ||
345 | |||
346 | if (thumbnailModel) await videoInstanceUpdated.addAndSaveThumbnail(thumbnailModel, t) | ||
347 | if (previewModel) await videoInstanceUpdated.addAndSaveThumbnail(previewModel, t) | ||
348 | |||
349 | // Video tags update? | ||
350 | if (videoInfoToUpdate.tags !== undefined) { | ||
351 | await setVideoTags({ | ||
352 | video: videoInstanceUpdated, | ||
353 | tags: videoInfoToUpdate.tags, | ||
354 | transaction: t | ||
355 | }) | ||
356 | } | ||
357 | |||
358 | // Video channel update? | ||
359 | if (res.locals.videoChannel && videoInstanceUpdated.channelId !== res.locals.videoChannel.id) { | ||
360 | await videoInstanceUpdated.$set('VideoChannel', res.locals.videoChannel, { transaction: t }) | ||
361 | videoInstanceUpdated.VideoChannel = res.locals.videoChannel | ||
362 | |||
363 | if (hadPrivacyForFederation === true) await changeVideoChannelShare(videoInstanceUpdated, oldVideoChannel, t) | ||
364 | } | ||
365 | |||
366 | // Schedule an update in the future? | ||
367 | if (videoInfoToUpdate.scheduleUpdate) { | ||
368 | await ScheduleVideoUpdateModel.upsert({ | ||
369 | videoId: videoInstanceUpdated.id, | ||
370 | updateAt: videoInfoToUpdate.scheduleUpdate.updateAt, | ||
371 | privacy: videoInfoToUpdate.scheduleUpdate.privacy || null | ||
372 | }, { transaction: t }) | ||
373 | } else if (videoInfoToUpdate.scheduleUpdate === null) { | ||
374 | await ScheduleVideoUpdateModel.deleteByVideoId(videoInstanceUpdated.id, t) | ||
375 | } | ||
376 | |||
377 | await autoBlacklistVideoIfNeeded({ | ||
378 | video: videoInstanceUpdated, | ||
379 | user: res.locals.oauth.token.User, | ||
380 | isRemote: false, | ||
381 | isNew: false, | ||
382 | transaction: t | ||
383 | }) | ||
384 | |||
385 | await federateVideoIfNeeded(videoInstanceUpdated, isNewVideo, t) | ||
386 | |||
387 | auditLogger.update( | ||
388 | getAuditIdFromRes(res), | ||
389 | new VideoAuditView(videoInstanceUpdated.toFormattedDetailsJSON()), | ||
390 | oldVideoAuditView | ||
391 | ) | ||
392 | logger.info('Video with name %s and uuid %s updated.', videoInstance.name, videoInstance.uuid, lTags(videoInstance.uuid)) | ||
393 | |||
394 | return videoInstanceUpdated | ||
395 | }) | ||
396 | |||
397 | if (wasConfidentialVideo) { | ||
398 | Notifier.Instance.notifyOnNewVideoIfNeeded(videoInstanceUpdated) | ||
399 | } | ||
400 | |||
401 | Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body }) | ||
402 | } catch (err) { | ||
403 | // Force fields we want to update | ||
404 | // If the transaction is retried, sequelize will think the object has not changed | ||
405 | // So it will skip the SQL request, even if the last one was ROLLBACKed! | ||
406 | resetSequelizeInstance(videoInstance, videoFieldsSave) | ||
407 | |||
408 | throw err | ||
409 | } | ||
410 | |||
411 | return res.type('json') | ||
412 | .status(HttpStatusCode.NO_CONTENT_204) | ||
413 | .end() | ||
414 | } | ||
415 | |||
416 | async function getVideo (req: express.Request, res: express.Response) { | ||
417 | // We need more attributes | 126 | // We need more attributes |
418 | const userId: number = res.locals.oauth ? res.locals.oauth.token.User.id : null | 127 | const userId: number = res.locals.oauth ? res.locals.oauth.token.User.id : null |
419 | 128 | ||
@@ -475,13 +184,10 @@ async function viewVideo (req: express.Request, res: express.Response) { | |||
475 | 184 | ||
476 | async function getVideoDescription (req: express.Request, res: express.Response) { | 185 | async function getVideoDescription (req: express.Request, res: express.Response) { |
477 | const videoInstance = res.locals.videoAll | 186 | const videoInstance = res.locals.videoAll |
478 | let description = '' | ||
479 | 187 | ||
480 | if (videoInstance.isOwned()) { | 188 | const description = videoInstance.isOwned() |
481 | description = videoInstance.description | 189 | ? videoInstance.description |
482 | } else { | 190 | : await fetchRemoteVideoDescription(videoInstance) |
483 | description = await fetchRemoteVideoDescription(videoInstance) | ||
484 | } | ||
485 | 191 | ||
486 | return res.json({ description }) | 192 | return res.json({ description }) |
487 | } | 193 | } |
@@ -523,7 +229,7 @@ async function listVideos (req: express.Request, res: express.Response) { | |||
523 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | 229 | return res.json(getFormattedObjects(resultList.data, resultList.total)) |
524 | } | 230 | } |
525 | 231 | ||
526 | async function removeVideo (req: express.Request, res: express.Response) { | 232 | async function removeVideo (_req: express.Request, res: express.Response) { |
527 | const videoInstance = res.locals.videoAll | 233 | const videoInstance = res.locals.videoAll |
528 | 234 | ||
529 | await sequelizeTypescript.transaction(async t => { | 235 | await sequelizeTypescript.transaction(async t => { |
@@ -539,17 +245,3 @@ async function removeVideo (req: express.Request, res: express.Response) { | |||
539 | .status(HttpStatusCode.NO_CONTENT_204) | 245 | .status(HttpStatusCode.NO_CONTENT_204) |
540 | .end() | 246 | .end() |
541 | } | 247 | } |
542 | |||
543 | async function createTorrentAndSetInfoHashAsync (video: MVideo, fileArg: MVideoFile) { | ||
544 | await createTorrentAndSetInfoHash(video, fileArg) | ||
545 | |||
546 | // Refresh videoFile because the createTorrentAndSetInfoHash could be long | ||
547 | const refreshedFile = await VideoFileModel.loadWithVideo(fileArg.id) | ||
548 | // File does not exist anymore, remove the generated torrent | ||
549 | if (!refreshedFile) return fileArg.removeTorrent() | ||
550 | |||
551 | refreshedFile.infoHash = fileArg.infoHash | ||
552 | refreshedFile.torrentFilename = fileArg.torrentFilename | ||
553 | |||
554 | return refreshedFile.save() | ||
555 | } | ||