diff options
Diffstat (limited to 'server/controllers/api')
-rw-r--r-- | server/controllers/api/server/debug.ts | 2 | ||||
-rw-r--r-- | server/controllers/api/users/index.ts | 4 | ||||
-rw-r--r-- | server/controllers/api/users/my-history.ts | 3 | ||||
-rw-r--r-- | server/controllers/api/users/my-video-playlists.ts | 8 | ||||
-rw-r--r-- | server/controllers/api/users/token.ts | 7 | ||||
-rw-r--r-- | server/controllers/api/users/two-factor.ts | 95 | ||||
-rw-r--r-- | server/controllers/api/video-playlist.ts | 11 | ||||
-rw-r--r-- | server/controllers/api/videos/import.ts | 2 | ||||
-rw-r--r-- | server/controllers/api/videos/index.ts | 2 | ||||
-rw-r--r-- | server/controllers/api/videos/token.ts | 33 | ||||
-rw-r--r-- | server/controllers/api/videos/transcoding.ts | 8 | ||||
-rw-r--r-- | server/controllers/api/videos/update.ts | 79 |
12 files changed, 186 insertions, 68 deletions
diff --git a/server/controllers/api/server/debug.ts b/server/controllers/api/server/debug.ts index 4e5333782..f3792bfc8 100644 --- a/server/controllers/api/server/debug.ts +++ b/server/controllers/api/server/debug.ts | |||
@@ -8,6 +8,7 @@ import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' | |||
8 | import { UserRight } from '../../../../shared/models/users' | 8 | import { UserRight } from '../../../../shared/models/users' |
9 | import { authenticate, ensureUserHasRight } from '../../../middlewares' | 9 | import { authenticate, ensureUserHasRight } from '../../../middlewares' |
10 | import { VideoChannelSyncLatestScheduler } from '@server/lib/schedulers/video-channel-sync-latest-scheduler' | 10 | import { VideoChannelSyncLatestScheduler } from '@server/lib/schedulers/video-channel-sync-latest-scheduler' |
11 | import { UpdateVideosScheduler } from '@server/lib/schedulers/update-videos-scheduler' | ||
11 | 12 | ||
12 | const debugRouter = express.Router() | 13 | const debugRouter = express.Router() |
13 | 14 | ||
@@ -45,6 +46,7 @@ async function runCommand (req: express.Request, res: express.Response) { | |||
45 | 'remove-dandling-resumable-uploads': () => RemoveDanglingResumableUploadsScheduler.Instance.execute(), | 46 | 'remove-dandling-resumable-uploads': () => RemoveDanglingResumableUploadsScheduler.Instance.execute(), |
46 | 'process-video-views-buffer': () => VideoViewsBufferScheduler.Instance.execute(), | 47 | 'process-video-views-buffer': () => VideoViewsBufferScheduler.Instance.execute(), |
47 | 'process-video-viewers': () => VideoViewsManager.Instance.processViewerStats(), | 48 | 'process-video-viewers': () => VideoViewsManager.Instance.processViewerStats(), |
49 | 'process-update-videos-scheduler': () => UpdateVideosScheduler.Instance.execute(), | ||
48 | 'process-video-channel-sync-latest': () => VideoChannelSyncLatestScheduler.Instance.execute() | 50 | 'process-video-channel-sync-latest': () => VideoChannelSyncLatestScheduler.Instance.execute() |
49 | } | 51 | } |
50 | 52 | ||
diff --git a/server/controllers/api/users/index.ts b/server/controllers/api/users/index.ts index 0b27d5277..a8677a1d3 100644 --- a/server/controllers/api/users/index.ts +++ b/server/controllers/api/users/index.ts | |||
@@ -51,6 +51,7 @@ import { myVideosHistoryRouter } from './my-history' | |||
51 | import { myNotificationsRouter } from './my-notifications' | 51 | import { myNotificationsRouter } from './my-notifications' |
52 | import { mySubscriptionsRouter } from './my-subscriptions' | 52 | import { mySubscriptionsRouter } from './my-subscriptions' |
53 | import { myVideoPlaylistsRouter } from './my-video-playlists' | 53 | import { myVideoPlaylistsRouter } from './my-video-playlists' |
54 | import { twoFactorRouter } from './two-factor' | ||
54 | 55 | ||
55 | const auditLogger = auditLoggerFactory('users') | 56 | const auditLogger = auditLoggerFactory('users') |
56 | 57 | ||
@@ -66,6 +67,7 @@ const askSendEmailLimiter = buildRateLimiter({ | |||
66 | }) | 67 | }) |
67 | 68 | ||
68 | const usersRouter = express.Router() | 69 | const usersRouter = express.Router() |
70 | usersRouter.use('/', twoFactorRouter) | ||
69 | usersRouter.use('/', tokensRouter) | 71 | usersRouter.use('/', tokensRouter) |
70 | usersRouter.use('/', myNotificationsRouter) | 72 | usersRouter.use('/', myNotificationsRouter) |
71 | usersRouter.use('/', mySubscriptionsRouter) | 73 | usersRouter.use('/', mySubscriptionsRouter) |
@@ -343,7 +345,7 @@ async function askResetUserPassword (req: express.Request, res: express.Response | |||
343 | 345 | ||
344 | const verificationString = await Redis.Instance.setResetPasswordVerificationString(user.id) | 346 | const verificationString = await Redis.Instance.setResetPasswordVerificationString(user.id) |
345 | const url = WEBSERVER.URL + '/reset-password?userId=' + user.id + '&verificationString=' + verificationString | 347 | const url = WEBSERVER.URL + '/reset-password?userId=' + user.id + '&verificationString=' + verificationString |
346 | await Emailer.Instance.addPasswordResetEmailJob(user.username, user.email, url) | 348 | Emailer.Instance.addPasswordResetEmailJob(user.username, user.email, url) |
347 | 349 | ||
348 | return res.status(HttpStatusCode.NO_CONTENT_204).end() | 350 | return res.status(HttpStatusCode.NO_CONTENT_204).end() |
349 | } | 351 | } |
diff --git a/server/controllers/api/users/my-history.ts b/server/controllers/api/users/my-history.ts index bc5b40f59..e6d3e86ac 100644 --- a/server/controllers/api/users/my-history.ts +++ b/server/controllers/api/users/my-history.ts | |||
@@ -1,3 +1,4 @@ | |||
1 | import { forceNumber } from '@shared/core-utils' | ||
1 | import express from 'express' | 2 | import express from 'express' |
2 | import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' | 3 | import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' |
3 | import { getFormattedObjects } from '../../../helpers/utils' | 4 | import { getFormattedObjects } from '../../../helpers/utils' |
@@ -55,7 +56,7 @@ async function listMyVideosHistory (req: express.Request, res: express.Response) | |||
55 | async function removeUserHistoryElement (req: express.Request, res: express.Response) { | 56 | async function removeUserHistoryElement (req: express.Request, res: express.Response) { |
56 | const user = res.locals.oauth.token.User | 57 | const user = res.locals.oauth.token.User |
57 | 58 | ||
58 | await UserVideoHistoryModel.removeUserHistoryElement(user, parseInt(req.params.videoId + '')) | 59 | await UserVideoHistoryModel.removeUserHistoryElement(user, forceNumber(req.params.videoId)) |
59 | 60 | ||
60 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | 61 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) |
61 | } | 62 | } |
diff --git a/server/controllers/api/users/my-video-playlists.ts b/server/controllers/api/users/my-video-playlists.ts index f55ea2ec4..fbdbb7e50 100644 --- a/server/controllers/api/users/my-video-playlists.ts +++ b/server/controllers/api/users/my-video-playlists.ts | |||
@@ -1,4 +1,6 @@ | |||
1 | import express from 'express' | 1 | import express from 'express' |
2 | import { forceNumber } from '@shared/core-utils' | ||
3 | import { uuidToShort } from '@shared/extra-utils' | ||
2 | import { VideosExistInPlaylists } from '../../../../shared/models/videos/playlist/video-exist-in-playlist.model' | 4 | import { VideosExistInPlaylists } from '../../../../shared/models/videos/playlist/video-exist-in-playlist.model' |
3 | import { asyncMiddleware, authenticate } from '../../../middlewares' | 5 | import { asyncMiddleware, authenticate } from '../../../middlewares' |
4 | import { doVideosInPlaylistExistValidator } from '../../../middlewares/validators/videos/video-playlists' | 6 | import { doVideosInPlaylistExistValidator } from '../../../middlewares/validators/videos/video-playlists' |
@@ -21,10 +23,10 @@ export { | |||
21 | // --------------------------------------------------------------------------- | 23 | // --------------------------------------------------------------------------- |
22 | 24 | ||
23 | async function doVideosInPlaylistExist (req: express.Request, res: express.Response) { | 25 | async function doVideosInPlaylistExist (req: express.Request, res: express.Response) { |
24 | const videoIds = req.query.videoIds.map(i => parseInt(i + '', 10)) | 26 | const videoIds = req.query.videoIds.map(i => forceNumber(i)) |
25 | const user = res.locals.oauth.token.User | 27 | const user = res.locals.oauth.token.User |
26 | 28 | ||
27 | const results = await VideoPlaylistModel.listPlaylistIdsOf(user.Account.id, videoIds) | 29 | const results = await VideoPlaylistModel.listPlaylistSummariesOf(user.Account.id, videoIds) |
28 | 30 | ||
29 | const existObject: VideosExistInPlaylists = {} | 31 | const existObject: VideosExistInPlaylists = {} |
30 | 32 | ||
@@ -37,6 +39,8 @@ async function doVideosInPlaylistExist (req: express.Request, res: express.Respo | |||
37 | existObject[element.videoId].push({ | 39 | existObject[element.videoId].push({ |
38 | playlistElementId: element.id, | 40 | playlistElementId: element.id, |
39 | playlistId: result.id, | 41 | playlistId: result.id, |
42 | playlistDisplayName: result.name, | ||
43 | playlistShortUUID: uuidToShort(result.uuid), | ||
40 | startTimestamp: element.startTimestamp, | 44 | startTimestamp: element.startTimestamp, |
41 | stopTimestamp: element.stopTimestamp | 45 | stopTimestamp: element.stopTimestamp |
42 | }) | 46 | }) |
diff --git a/server/controllers/api/users/token.ts b/server/controllers/api/users/token.ts index 012a49791..c6afea67c 100644 --- a/server/controllers/api/users/token.ts +++ b/server/controllers/api/users/token.ts | |||
@@ -1,8 +1,9 @@ | |||
1 | import express from 'express' | 1 | import express from 'express' |
2 | import { logger } from '@server/helpers/logger' | 2 | import { logger } from '@server/helpers/logger' |
3 | import { CONFIG } from '@server/initializers/config' | 3 | import { CONFIG } from '@server/initializers/config' |
4 | import { OTP } from '@server/initializers/constants' | ||
4 | import { getAuthNameFromRefreshGrant, getBypassFromExternalAuth, getBypassFromPasswordGrant } from '@server/lib/auth/external-auth' | 5 | import { getAuthNameFromRefreshGrant, getBypassFromExternalAuth, getBypassFromPasswordGrant } from '@server/lib/auth/external-auth' |
5 | import { handleOAuthToken } from '@server/lib/auth/oauth' | 6 | import { handleOAuthToken, MissingTwoFactorError } from '@server/lib/auth/oauth' |
6 | import { BypassLogin, revokeToken } from '@server/lib/auth/oauth-model' | 7 | import { BypassLogin, revokeToken } from '@server/lib/auth/oauth-model' |
7 | import { Hooks } from '@server/lib/plugins/hooks' | 8 | import { Hooks } from '@server/lib/plugins/hooks' |
8 | import { asyncMiddleware, authenticate, buildRateLimiter, openapiOperationDoc } from '@server/middlewares' | 9 | import { asyncMiddleware, authenticate, buildRateLimiter, openapiOperationDoc } from '@server/middlewares' |
@@ -79,6 +80,10 @@ async function handleToken (req: express.Request, res: express.Response, next: e | |||
79 | } catch (err) { | 80 | } catch (err) { |
80 | logger.warn('Login error', { err }) | 81 | logger.warn('Login error', { err }) |
81 | 82 | ||
83 | if (err instanceof MissingTwoFactorError) { | ||
84 | res.set(OTP.HEADER_NAME, OTP.HEADER_REQUIRED_VALUE) | ||
85 | } | ||
86 | |||
82 | return res.fail({ | 87 | return res.fail({ |
83 | status: err.code, | 88 | status: err.code, |
84 | message: err.message, | 89 | message: err.message, |
diff --git a/server/controllers/api/users/two-factor.ts b/server/controllers/api/users/two-factor.ts new file mode 100644 index 000000000..e6ae9e4dd --- /dev/null +++ b/server/controllers/api/users/two-factor.ts | |||
@@ -0,0 +1,95 @@ | |||
1 | import express from 'express' | ||
2 | import { generateOTPSecret, isOTPValid } from '@server/helpers/otp' | ||
3 | import { encrypt } from '@server/helpers/peertube-crypto' | ||
4 | import { CONFIG } from '@server/initializers/config' | ||
5 | import { Redis } from '@server/lib/redis' | ||
6 | import { asyncMiddleware, authenticate, usersCheckCurrentPasswordFactory } from '@server/middlewares' | ||
7 | import { | ||
8 | confirmTwoFactorValidator, | ||
9 | disableTwoFactorValidator, | ||
10 | requestOrConfirmTwoFactorValidator | ||
11 | } from '@server/middlewares/validators/two-factor' | ||
12 | import { HttpStatusCode, TwoFactorEnableResult } from '@shared/models' | ||
13 | |||
14 | const twoFactorRouter = express.Router() | ||
15 | |||
16 | twoFactorRouter.post('/:id/two-factor/request', | ||
17 | authenticate, | ||
18 | asyncMiddleware(usersCheckCurrentPasswordFactory(req => req.params.id)), | ||
19 | asyncMiddleware(requestOrConfirmTwoFactorValidator), | ||
20 | asyncMiddleware(requestTwoFactor) | ||
21 | ) | ||
22 | |||
23 | twoFactorRouter.post('/:id/two-factor/confirm-request', | ||
24 | authenticate, | ||
25 | asyncMiddleware(requestOrConfirmTwoFactorValidator), | ||
26 | confirmTwoFactorValidator, | ||
27 | asyncMiddleware(confirmRequestTwoFactor) | ||
28 | ) | ||
29 | |||
30 | twoFactorRouter.post('/:id/two-factor/disable', | ||
31 | authenticate, | ||
32 | asyncMiddleware(usersCheckCurrentPasswordFactory(req => req.params.id)), | ||
33 | asyncMiddleware(disableTwoFactorValidator), | ||
34 | asyncMiddleware(disableTwoFactor) | ||
35 | ) | ||
36 | |||
37 | // --------------------------------------------------------------------------- | ||
38 | |||
39 | export { | ||
40 | twoFactorRouter | ||
41 | } | ||
42 | |||
43 | // --------------------------------------------------------------------------- | ||
44 | |||
45 | async function requestTwoFactor (req: express.Request, res: express.Response) { | ||
46 | const user = res.locals.user | ||
47 | |||
48 | const { secret, uri } = generateOTPSecret(user.email) | ||
49 | |||
50 | const encryptedSecret = await encrypt(secret, CONFIG.SECRETS.PEERTUBE) | ||
51 | const requestToken = await Redis.Instance.setTwoFactorRequest(user.id, encryptedSecret) | ||
52 | |||
53 | return res.json({ | ||
54 | otpRequest: { | ||
55 | requestToken, | ||
56 | secret, | ||
57 | uri | ||
58 | } | ||
59 | } as TwoFactorEnableResult) | ||
60 | } | ||
61 | |||
62 | async function confirmRequestTwoFactor (req: express.Request, res: express.Response) { | ||
63 | const requestToken = req.body.requestToken | ||
64 | const otpToken = req.body.otpToken | ||
65 | const user = res.locals.user | ||
66 | |||
67 | const encryptedSecret = await Redis.Instance.getTwoFactorRequestToken(user.id, requestToken) | ||
68 | if (!encryptedSecret) { | ||
69 | return res.fail({ | ||
70 | message: 'Invalid request token', | ||
71 | status: HttpStatusCode.FORBIDDEN_403 | ||
72 | }) | ||
73 | } | ||
74 | |||
75 | if (await isOTPValid({ encryptedSecret, token: otpToken }) !== true) { | ||
76 | return res.fail({ | ||
77 | message: 'Invalid OTP token', | ||
78 | status: HttpStatusCode.FORBIDDEN_403 | ||
79 | }) | ||
80 | } | ||
81 | |||
82 | user.otpSecret = encryptedSecret | ||
83 | await user.save() | ||
84 | |||
85 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | ||
86 | } | ||
87 | |||
88 | async function disableTwoFactor (req: express.Request, res: express.Response) { | ||
89 | const user = res.locals.user | ||
90 | |||
91 | user.otpSecret = null | ||
92 | await user.save() | ||
93 | |||
94 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | ||
95 | } | ||
diff --git a/server/controllers/api/video-playlist.ts b/server/controllers/api/video-playlist.ts index 1255d14c6..f8a607170 100644 --- a/server/controllers/api/video-playlist.ts +++ b/server/controllers/api/video-playlist.ts | |||
@@ -4,6 +4,7 @@ import { scheduleRefreshIfNeeded } from '@server/lib/activitypub/playlists' | |||
4 | import { Hooks } from '@server/lib/plugins/hooks' | 4 | import { Hooks } from '@server/lib/plugins/hooks' |
5 | import { getServerActor } from '@server/models/application/application' | 5 | import { getServerActor } from '@server/models/application/application' |
6 | import { MVideoPlaylistFull, MVideoPlaylistThumbnail, MVideoThumbnail } from '@server/types/models' | 6 | import { MVideoPlaylistFull, MVideoPlaylistThumbnail, MVideoThumbnail } from '@server/types/models' |
7 | import { forceNumber } from '@shared/core-utils' | ||
7 | import { uuidToShort } from '@shared/extra-utils' | 8 | import { uuidToShort } from '@shared/extra-utils' |
8 | import { VideoPlaylistCreateResult, VideoPlaylistElementCreateResult } from '@shared/models' | 9 | import { VideoPlaylistCreateResult, VideoPlaylistElementCreateResult } from '@shared/models' |
9 | import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' | 10 | import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' |
@@ -245,7 +246,7 @@ async function updateVideoPlaylist (req: express.Request, res: express.Response) | |||
245 | if (videoPlaylistInfoToUpdate.description !== undefined) videoPlaylistInstance.description = videoPlaylistInfoToUpdate.description | 246 | if (videoPlaylistInfoToUpdate.description !== undefined) videoPlaylistInstance.description = videoPlaylistInfoToUpdate.description |
246 | 247 | ||
247 | if (videoPlaylistInfoToUpdate.privacy !== undefined) { | 248 | if (videoPlaylistInfoToUpdate.privacy !== undefined) { |
248 | videoPlaylistInstance.privacy = parseInt(videoPlaylistInfoToUpdate.privacy.toString(), 10) | 249 | videoPlaylistInstance.privacy = forceNumber(videoPlaylistInfoToUpdate.privacy) |
249 | 250 | ||
250 | if (wasNotPrivatePlaylist === true && videoPlaylistInstance.privacy === VideoPlaylistPrivacy.PRIVATE) { | 251 | if (wasNotPrivatePlaylist === true && videoPlaylistInstance.privacy === VideoPlaylistPrivacy.PRIVATE) { |
251 | await sendDeleteVideoPlaylist(videoPlaylistInstance, t) | 252 | await sendDeleteVideoPlaylist(videoPlaylistInstance, t) |
@@ -424,7 +425,13 @@ async function reorderVideosPlaylist (req: express.Request, res: express.Respons | |||
424 | 425 | ||
425 | const endOldPosition = oldPosition + reorderLength - 1 | 426 | const endOldPosition = oldPosition + reorderLength - 1 |
426 | // Insert our reordered elements in their place (update) | 427 | // Insert our reordered elements in their place (update) |
427 | await VideoPlaylistElementModel.reassignPositionOf(videoPlaylist.id, oldPosition, endOldPosition, newPosition, t) | 428 | await VideoPlaylistElementModel.reassignPositionOf({ |
429 | videoPlaylistId: videoPlaylist.id, | ||
430 | firstPosition: oldPosition, | ||
431 | endPosition: endOldPosition, | ||
432 | newPosition, | ||
433 | transaction: t | ||
434 | }) | ||
428 | 435 | ||
429 | // Decrease positions of elements after the old position of our ordered elements (decrease) | 436 | // Decrease positions of elements after the old position of our ordered elements (decrease) |
430 | await VideoPlaylistElementModel.increasePositionOf(videoPlaylist.id, oldPosition, -reorderLength, t) | 437 | await VideoPlaylistElementModel.increasePositionOf(videoPlaylist.id, oldPosition, -reorderLength, t) |
diff --git a/server/controllers/api/videos/import.ts b/server/controllers/api/videos/import.ts index 9d7b0260b..6a50aaf4e 100644 --- a/server/controllers/api/videos/import.ts +++ b/server/controllers/api/videos/import.ts | |||
@@ -3,7 +3,7 @@ import { move, readFile } from 'fs-extra' | |||
3 | import { decode } from 'magnet-uri' | 3 | import { decode } from 'magnet-uri' |
4 | import parseTorrent, { Instance } from 'parse-torrent' | 4 | import parseTorrent, { Instance } from 'parse-torrent' |
5 | import { join } from 'path' | 5 | import { join } from 'path' |
6 | import { buildYoutubeDLImport, buildVideoFromImport, insertFromImportIntoDB, YoutubeDlImportError } from '@server/lib/video-import' | 6 | import { buildYoutubeDLImport, buildVideoFromImport, insertFromImportIntoDB, YoutubeDlImportError } from '@server/lib/video-pre-import' |
7 | import { MThumbnail, MVideoThumbnail } from '@server/types/models' | 7 | import { MThumbnail, MVideoThumbnail } from '@server/types/models' |
8 | import { HttpStatusCode, ServerErrorCode, ThumbnailType, VideoImportCreate, VideoImportPayload, VideoImportState } from '@shared/models' | 8 | import { HttpStatusCode, ServerErrorCode, ThumbnailType, VideoImportCreate, VideoImportPayload, VideoImportState } from '@shared/models' |
9 | import { auditLoggerFactory, getAuditIdFromRes, VideoImportAuditView } from '../../../helpers/audit-logger' | 9 | import { auditLoggerFactory, getAuditIdFromRes, VideoImportAuditView } from '../../../helpers/audit-logger' |
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index b301515df..ea081e5ab 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts | |||
@@ -41,6 +41,7 @@ import { ownershipVideoRouter } from './ownership' | |||
41 | import { rateVideoRouter } from './rate' | 41 | import { rateVideoRouter } from './rate' |
42 | import { statsRouter } from './stats' | 42 | import { statsRouter } from './stats' |
43 | import { studioRouter } from './studio' | 43 | import { studioRouter } from './studio' |
44 | import { tokenRouter } from './token' | ||
44 | import { transcodingRouter } from './transcoding' | 45 | import { transcodingRouter } from './transcoding' |
45 | import { updateRouter } from './update' | 46 | import { updateRouter } from './update' |
46 | import { uploadRouter } from './upload' | 47 | import { uploadRouter } from './upload' |
@@ -63,6 +64,7 @@ videosRouter.use('/', uploadRouter) | |||
63 | videosRouter.use('/', updateRouter) | 64 | videosRouter.use('/', updateRouter) |
64 | videosRouter.use('/', filesRouter) | 65 | videosRouter.use('/', filesRouter) |
65 | videosRouter.use('/', transcodingRouter) | 66 | videosRouter.use('/', transcodingRouter) |
67 | videosRouter.use('/', tokenRouter) | ||
66 | 68 | ||
67 | videosRouter.get('/categories', | 69 | videosRouter.get('/categories', |
68 | openapiOperationDoc({ operationId: 'getCategories' }), | 70 | openapiOperationDoc({ operationId: 'getCategories' }), |
diff --git a/server/controllers/api/videos/token.ts b/server/controllers/api/videos/token.ts new file mode 100644 index 000000000..009b6dfb6 --- /dev/null +++ b/server/controllers/api/videos/token.ts | |||
@@ -0,0 +1,33 @@ | |||
1 | import express from 'express' | ||
2 | import { VideoTokensManager } from '@server/lib/video-tokens-manager' | ||
3 | import { VideoToken } from '@shared/models' | ||
4 | import { asyncMiddleware, authenticate, videosCustomGetValidator } from '../../../middlewares' | ||
5 | |||
6 | const tokenRouter = express.Router() | ||
7 | |||
8 | tokenRouter.post('/:id/token', | ||
9 | authenticate, | ||
10 | asyncMiddleware(videosCustomGetValidator('only-video')), | ||
11 | generateToken | ||
12 | ) | ||
13 | |||
14 | // --------------------------------------------------------------------------- | ||
15 | |||
16 | export { | ||
17 | tokenRouter | ||
18 | } | ||
19 | |||
20 | // --------------------------------------------------------------------------- | ||
21 | |||
22 | function generateToken (req: express.Request, res: express.Response) { | ||
23 | const video = res.locals.onlyVideo | ||
24 | |||
25 | const { token, expires } = VideoTokensManager.Instance.create(video.uuid) | ||
26 | |||
27 | return res.json({ | ||
28 | files: { | ||
29 | token, | ||
30 | expires | ||
31 | } | ||
32 | } as VideoToken) | ||
33 | } | ||
diff --git a/server/controllers/api/videos/transcoding.ts b/server/controllers/api/videos/transcoding.ts index 9aca761c1..8c9a5322b 100644 --- a/server/controllers/api/videos/transcoding.ts +++ b/server/controllers/api/videos/transcoding.ts | |||
@@ -32,9 +32,10 @@ async function createTranscoding (req: express.Request, res: express.Response) { | |||
32 | 32 | ||
33 | const body: VideoTranscodingCreate = req.body | 33 | const body: VideoTranscodingCreate = req.body |
34 | 34 | ||
35 | const { resolution: maxResolution, audioStream } = await video.probeMaxQualityFile() | 35 | const { resolution: maxResolution, hasAudio } = await video.probeMaxQualityFile() |
36 | |||
36 | const resolutions = await Hooks.wrapObject( | 37 | const resolutions = await Hooks.wrapObject( |
37 | computeResolutionsToTranscode({ input: maxResolution, type: 'vod', includeInput: true, strictLower: false }), | 38 | computeResolutionsToTranscode({ input: maxResolution, type: 'vod', includeInput: true, strictLower: false, hasAudio }), |
38 | 'filter:transcoding.manual.resolutions-to-transcode.result', | 39 | 'filter:transcoding.manual.resolutions-to-transcode.result', |
39 | body | 40 | body |
40 | ) | 41 | ) |
@@ -46,9 +47,10 @@ async function createTranscoding (req: express.Request, res: express.Response) { | |||
46 | video.state = VideoState.TO_TRANSCODE | 47 | video.state = VideoState.TO_TRANSCODE |
47 | await video.save() | 48 | await video.save() |
48 | 49 | ||
49 | const hasAudio = !!audioStream | ||
50 | const childrenResolutions = resolutions.filter(r => r !== maxResolution) | 50 | const childrenResolutions = resolutions.filter(r => r !== maxResolution) |
51 | 51 | ||
52 | logger.info('Manually creating transcoding jobs for %s.', body.transcodingType, { childrenResolutions, maxResolution }) | ||
53 | |||
52 | const children = await Bluebird.mapSeries(childrenResolutions, resolution => { | 54 | const children = await Bluebird.mapSeries(childrenResolutions, resolution => { |
53 | if (body.transcodingType === 'hls') { | 55 | if (body.transcodingType === 'hls') { |
54 | return buildHLSJobOption({ | 56 | return buildHLSJobOption({ |
diff --git a/server/controllers/api/videos/update.ts b/server/controllers/api/videos/update.ts index ab1a23d9a..260dee2b9 100644 --- a/server/controllers/api/videos/update.ts +++ b/server/controllers/api/videos/update.ts | |||
@@ -1,12 +1,12 @@ | |||
1 | import express from 'express' | 1 | import express from 'express' |
2 | import { Transaction } from 'sequelize/types' | 2 | import { Transaction } from 'sequelize/types' |
3 | import { changeVideoChannelShare } from '@server/lib/activitypub/share' | 3 | import { changeVideoChannelShare } from '@server/lib/activitypub/share' |
4 | import { CreateJobArgument, JobQueue } from '@server/lib/job-queue' | 4 | import { addVideoJobsAfterUpdate, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video' |
5 | import { buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video' | 5 | import { setVideoPrivacy } from '@server/lib/video-privacy' |
6 | import { openapiOperationDoc } from '@server/middlewares/doc' | 6 | import { openapiOperationDoc } from '@server/middlewares/doc' |
7 | import { FilteredModelAttributes } from '@server/types' | 7 | import { FilteredModelAttributes } from '@server/types' |
8 | import { MVideoFullLight } from '@server/types/models' | 8 | import { MVideoFullLight } from '@server/types/models' |
9 | import { HttpStatusCode, ManageVideoTorrentPayload, VideoUpdate } from '@shared/models' | 9 | import { HttpStatusCode, VideoUpdate } from '@shared/models' |
10 | import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' | 10 | import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' |
11 | import { resetSequelizeInstance } from '../../../helpers/database-utils' | 11 | import { resetSequelizeInstance } from '../../../helpers/database-utils' |
12 | import { createReqFiles } from '../../../helpers/express-utils' | 12 | import { createReqFiles } from '../../../helpers/express-utils' |
@@ -18,6 +18,8 @@ import { autoBlacklistVideoIfNeeded } from '../../../lib/video-blacklist' | |||
18 | import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videosUpdateValidator } from '../../../middlewares' | 18 | import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videosUpdateValidator } from '../../../middlewares' |
19 | import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update' | 19 | import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update' |
20 | import { VideoModel } from '../../../models/video/video' | 20 | import { VideoModel } from '../../../models/video/video' |
21 | import { VideoPathManager } from '@server/lib/video-path-manager' | ||
22 | import { forceNumber } from '@shared/core-utils' | ||
21 | 23 | ||
22 | const lTags = loggerTagsFactory('api', 'video') | 24 | const lTags = loggerTagsFactory('api', 'video') |
23 | const auditLogger = auditLoggerFactory('videos') | 25 | const auditLogger = auditLoggerFactory('videos') |
@@ -47,8 +49,8 @@ async function updateVideo (req: express.Request, res: express.Response) { | |||
47 | const oldVideoAuditView = new VideoAuditView(videoFromReq.toFormattedDetailsJSON()) | 49 | const oldVideoAuditView = new VideoAuditView(videoFromReq.toFormattedDetailsJSON()) |
48 | const videoInfoToUpdate: VideoUpdate = req.body | 50 | const videoInfoToUpdate: VideoUpdate = req.body |
49 | 51 | ||
50 | const wasConfidentialVideo = videoFromReq.isConfidential() | ||
51 | const hadPrivacyForFederation = videoFromReq.hasPrivacyForFederation() | 52 | const hadPrivacyForFederation = videoFromReq.hasPrivacyForFederation() |
53 | const oldPrivacy = videoFromReq.privacy | ||
52 | 54 | ||
53 | const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({ | 55 | const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({ |
54 | video: videoFromReq, | 56 | video: videoFromReq, |
@@ -57,12 +59,13 @@ async function updateVideo (req: express.Request, res: express.Response) { | |||
57 | automaticallyGenerated: false | 59 | automaticallyGenerated: false |
58 | }) | 60 | }) |
59 | 61 | ||
62 | const videoFileLockReleaser = await VideoPathManager.Instance.lockFiles(videoFromReq.uuid) | ||
63 | |||
60 | try { | 64 | try { |
61 | const { videoInstanceUpdated, isNewVideo } = await sequelizeTypescript.transaction(async t => { | 65 | const { videoInstanceUpdated, isNewVideo } = await sequelizeTypescript.transaction(async t => { |
62 | // Refresh video since thumbnails to prevent concurrent updates | 66 | // Refresh video since thumbnails to prevent concurrent updates |
63 | const video = await VideoModel.loadFull(videoFromReq.id, t) | 67 | const video = await VideoModel.loadFull(videoFromReq.id, t) |
64 | 68 | ||
65 | const sequelizeOptions = { transaction: t } | ||
66 | const oldVideoChannel = video.VideoChannel | 69 | const oldVideoChannel = video.VideoChannel |
67 | 70 | ||
68 | const keysToUpdate: (keyof VideoUpdate & FilteredModelAttributes<VideoModel>)[] = [ | 71 | const keysToUpdate: (keyof VideoUpdate & FilteredModelAttributes<VideoModel>)[] = [ |
@@ -97,7 +100,7 @@ async function updateVideo (req: express.Request, res: express.Response) { | |||
97 | await video.setAsRefreshed(t) | 100 | await video.setAsRefreshed(t) |
98 | } | 101 | } |
99 | 102 | ||
100 | const videoInstanceUpdated = await video.save(sequelizeOptions) as MVideoFullLight | 103 | const videoInstanceUpdated = await video.save({ transaction: t }) as MVideoFullLight |
101 | 104 | ||
102 | // Thumbnail & preview updates? | 105 | // Thumbnail & preview updates? |
103 | if (thumbnailModel) await videoInstanceUpdated.addAndSaveThumbnail(thumbnailModel, t) | 106 | if (thumbnailModel) await videoInstanceUpdated.addAndSaveThumbnail(thumbnailModel, t) |
@@ -113,7 +116,9 @@ async function updateVideo (req: express.Request, res: express.Response) { | |||
113 | await videoInstanceUpdated.$set('VideoChannel', res.locals.videoChannel, { transaction: t }) | 116 | await videoInstanceUpdated.$set('VideoChannel', res.locals.videoChannel, { transaction: t }) |
114 | videoInstanceUpdated.VideoChannel = res.locals.videoChannel | 117 | videoInstanceUpdated.VideoChannel = res.locals.videoChannel |
115 | 118 | ||
116 | if (hadPrivacyForFederation === true) await changeVideoChannelShare(videoInstanceUpdated, oldVideoChannel, t) | 119 | if (hadPrivacyForFederation === true) { |
120 | await changeVideoChannelShare(videoInstanceUpdated, oldVideoChannel, t) | ||
121 | } | ||
117 | } | 122 | } |
118 | 123 | ||
119 | // Schedule an update in the future? | 124 | // Schedule an update in the future? |
@@ -139,7 +144,12 @@ async function updateVideo (req: express.Request, res: express.Response) { | |||
139 | 144 | ||
140 | Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body, req, res }) | 145 | Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body, req, res }) |
141 | 146 | ||
142 | await addVideoJobsAfterUpdate({ video: videoInstanceUpdated, videoInfoToUpdate, wasConfidentialVideo, isNewVideo }) | 147 | await addVideoJobsAfterUpdate({ |
148 | video: videoInstanceUpdated, | ||
149 | nameChanged: !!videoInfoToUpdate.name, | ||
150 | oldPrivacy, | ||
151 | isNewVideo | ||
152 | }) | ||
143 | } catch (err) { | 153 | } catch (err) { |
144 | // Force fields we want to update | 154 | // Force fields we want to update |
145 | // If the transaction is retried, sequelize will think the object has not changed | 155 | // If the transaction is retried, sequelize will think the object has not changed |
@@ -147,6 +157,8 @@ async function updateVideo (req: express.Request, res: express.Response) { | |||
147 | resetSequelizeInstance(videoFromReq, videoFieldsSave) | 157 | resetSequelizeInstance(videoFromReq, videoFieldsSave) |
148 | 158 | ||
149 | throw err | 159 | throw err |
160 | } finally { | ||
161 | videoFileLockReleaser() | ||
150 | } | 162 | } |
151 | 163 | ||
152 | return res.type('json') | 164 | return res.type('json') |
@@ -163,8 +175,8 @@ async function updateVideoPrivacy (options: { | |||
163 | const { videoInstance, videoInfoToUpdate, hadPrivacyForFederation, transaction } = options | 175 | const { videoInstance, videoInfoToUpdate, hadPrivacyForFederation, transaction } = options |
164 | const isNewVideo = videoInstance.isNewVideo(videoInfoToUpdate.privacy) | 176 | const isNewVideo = videoInstance.isNewVideo(videoInfoToUpdate.privacy) |
165 | 177 | ||
166 | const newPrivacy = parseInt(videoInfoToUpdate.privacy.toString(), 10) | 178 | const newPrivacy = forceNumber(videoInfoToUpdate.privacy) |
167 | videoInstance.setPrivacy(newPrivacy) | 179 | setVideoPrivacy(videoInstance, newPrivacy) |
168 | 180 | ||
169 | // Unfederate the video if the new privacy is not compatible with federation | 181 | // Unfederate the video if the new privacy is not compatible with federation |
170 | if (hadPrivacyForFederation && !videoInstance.hasPrivacyForFederation()) { | 182 | if (hadPrivacyForFederation && !videoInstance.hasPrivacyForFederation()) { |
@@ -185,50 +197,3 @@ function updateSchedule (videoInstance: MVideoFullLight, videoInfoToUpdate: Vide | |||
185 | return ScheduleVideoUpdateModel.deleteByVideoId(videoInstance.id, transaction) | 197 | return ScheduleVideoUpdateModel.deleteByVideoId(videoInstance.id, transaction) |
186 | } | 198 | } |
187 | } | 199 | } |
188 | |||
189 | async function addVideoJobsAfterUpdate (options: { | ||
190 | video: MVideoFullLight | ||
191 | videoInfoToUpdate: VideoUpdate | ||
192 | wasConfidentialVideo: boolean | ||
193 | isNewVideo: boolean | ||
194 | }) { | ||
195 | const { video, videoInfoToUpdate, wasConfidentialVideo, isNewVideo } = options | ||
196 | const jobs: CreateJobArgument[] = [] | ||
197 | |||
198 | if (!video.isLive && videoInfoToUpdate.name) { | ||
199 | |||
200 | for (const file of (video.VideoFiles || [])) { | ||
201 | const payload: ManageVideoTorrentPayload = { action: 'update-metadata', videoId: video.id, videoFileId: file.id } | ||
202 | |||
203 | jobs.push({ type: 'manage-video-torrent', payload }) | ||
204 | } | ||
205 | |||
206 | const hls = video.getHLSPlaylist() | ||
207 | |||
208 | for (const file of (hls?.VideoFiles || [])) { | ||
209 | const payload: ManageVideoTorrentPayload = { action: 'update-metadata', streamingPlaylistId: hls.id, videoFileId: file.id } | ||
210 | |||
211 | jobs.push({ type: 'manage-video-torrent', payload }) | ||
212 | } | ||
213 | } | ||
214 | |||
215 | jobs.push({ | ||
216 | type: 'federate-video', | ||
217 | payload: { | ||
218 | videoUUID: video.uuid, | ||
219 | isNewVideo | ||
220 | } | ||
221 | }) | ||
222 | |||
223 | if (wasConfidentialVideo) { | ||
224 | jobs.push({ | ||
225 | type: 'notify', | ||
226 | payload: { | ||
227 | action: 'new-video', | ||
228 | videoUUID: video.uuid | ||
229 | } | ||
230 | }) | ||
231 | } | ||
232 | |||
233 | return JobQueue.Instance.createSequentialJobFlow(...jobs) | ||
234 | } | ||