diff options
Diffstat (limited to 'server/controllers')
-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 | ||||
-rw-r--r-- | server/controllers/download.ts | 28 | ||||
-rw-r--r-- | server/controllers/feeds.ts | 78 | ||||
-rw-r--r-- | server/controllers/index.ts | 12 | ||||
-rw-r--r-- | server/controllers/live.ts | 32 | ||||
-rw-r--r-- | server/controllers/object-storage-proxy.ts | 86 | ||||
-rw-r--r-- | server/controllers/services.ts | 5 | ||||
-rw-r--r-- | server/controllers/static.ts | 43 | ||||
-rw-r--r-- | server/controllers/well-known.ts | 7 |
20 files changed, 398 insertions, 147 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 | } | ||
diff --git a/server/controllers/download.ts b/server/controllers/download.ts index a270180c0..65b9a1d1b 100644 --- a/server/controllers/download.ts +++ b/server/controllers/download.ts | |||
@@ -5,9 +5,10 @@ import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache | |||
5 | import { Hooks } from '@server/lib/plugins/hooks' | 5 | import { Hooks } from '@server/lib/plugins/hooks' |
6 | import { VideoPathManager } from '@server/lib/video-path-manager' | 6 | import { VideoPathManager } from '@server/lib/video-path-manager' |
7 | import { MStreamingPlaylist, MVideo, MVideoFile, MVideoFullLight } from '@server/types/models' | 7 | import { MStreamingPlaylist, MVideo, MVideoFile, MVideoFullLight } from '@server/types/models' |
8 | import { addQueryParams, forceNumber } from '@shared/core-utils' | ||
8 | import { HttpStatusCode, VideoStorage, VideoStreamingPlaylistType } from '@shared/models' | 9 | import { HttpStatusCode, VideoStorage, VideoStreamingPlaylistType } from '@shared/models' |
9 | import { STATIC_DOWNLOAD_PATHS } from '../initializers/constants' | 10 | import { STATIC_DOWNLOAD_PATHS } from '../initializers/constants' |
10 | import { asyncMiddleware, videosDownloadValidator } from '../middlewares' | 11 | import { asyncMiddleware, optionalAuthenticate, videosDownloadValidator } from '../middlewares' |
11 | 12 | ||
12 | const downloadRouter = express.Router() | 13 | const downloadRouter = express.Router() |
13 | 14 | ||
@@ -20,12 +21,14 @@ downloadRouter.use( | |||
20 | 21 | ||
21 | downloadRouter.use( | 22 | downloadRouter.use( |
22 | STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension', | 23 | STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension', |
24 | optionalAuthenticate, | ||
23 | asyncMiddleware(videosDownloadValidator), | 25 | asyncMiddleware(videosDownloadValidator), |
24 | asyncMiddleware(downloadVideoFile) | 26 | asyncMiddleware(downloadVideoFile) |
25 | ) | 27 | ) |
26 | 28 | ||
27 | downloadRouter.use( | 29 | downloadRouter.use( |
28 | STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + ':id-:resolution([0-9]+)-fragmented.:extension', | 30 | STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + ':id-:resolution([0-9]+)-fragmented.:extension', |
31 | optionalAuthenticate, | ||
29 | asyncMiddleware(videosDownloadValidator), | 32 | asyncMiddleware(videosDownloadValidator), |
30 | asyncMiddleware(downloadHLSVideoFile) | 33 | asyncMiddleware(downloadHLSVideoFile) |
31 | ) | 34 | ) |
@@ -82,7 +85,7 @@ async function downloadVideoFile (req: express.Request, res: express.Response) { | |||
82 | if (!checkAllowResult(res, allowParameters, allowedResult)) return | 85 | if (!checkAllowResult(res, allowParameters, allowedResult)) return |
83 | 86 | ||
84 | if (videoFile.storage === VideoStorage.OBJECT_STORAGE) { | 87 | if (videoFile.storage === VideoStorage.OBJECT_STORAGE) { |
85 | return res.redirect(videoFile.getObjectStorageUrl()) | 88 | return redirectToObjectStorage({ req, res, video, file: videoFile }) |
86 | } | 89 | } |
87 | 90 | ||
88 | await VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(video), path => { | 91 | await VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(video), path => { |
@@ -118,7 +121,7 @@ async function downloadHLSVideoFile (req: express.Request, res: express.Response | |||
118 | if (!checkAllowResult(res, allowParameters, allowedResult)) return | 121 | if (!checkAllowResult(res, allowParameters, allowedResult)) return |
119 | 122 | ||
120 | if (videoFile.storage === VideoStorage.OBJECT_STORAGE) { | 123 | if (videoFile.storage === VideoStorage.OBJECT_STORAGE) { |
121 | return res.redirect(videoFile.getObjectStorageUrl()) | 124 | return redirectToObjectStorage({ req, res, video, file: videoFile }) |
122 | } | 125 | } |
123 | 126 | ||
124 | await VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(streamingPlaylist), path => { | 127 | await VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(streamingPlaylist), path => { |
@@ -129,7 +132,7 @@ async function downloadHLSVideoFile (req: express.Request, res: express.Response | |||
129 | } | 132 | } |
130 | 133 | ||
131 | function getVideoFile (req: express.Request, files: MVideoFile[]) { | 134 | function getVideoFile (req: express.Request, files: MVideoFile[]) { |
132 | const resolution = parseInt(req.params.resolution, 10) | 135 | const resolution = forceNumber(req.params.resolution) |
133 | return files.find(f => f.resolution === resolution) | 136 | return files.find(f => f.resolution === resolution) |
134 | } | 137 | } |
135 | 138 | ||
@@ -172,3 +175,20 @@ function checkAllowResult (res: express.Response, allowParameters: any, result?: | |||
172 | 175 | ||
173 | return true | 176 | return true |
174 | } | 177 | } |
178 | |||
179 | function redirectToObjectStorage (options: { | ||
180 | req: express.Request | ||
181 | res: express.Response | ||
182 | video: MVideo | ||
183 | file: MVideoFile | ||
184 | }) { | ||
185 | const { req, res, video, file } = options | ||
186 | |||
187 | const baseUrl = file.getObjectStorageUrl(video) | ||
188 | |||
189 | const url = video.hasPrivateStaticPath() && req.query.videoFileToken | ||
190 | ? addQueryParams(baseUrl, { videoFileToken: req.query.videoFileToken }) | ||
191 | : baseUrl | ||
192 | |||
193 | return res.redirect(url) | ||
194 | } | ||
diff --git a/server/controllers/feeds.ts b/server/controllers/feeds.ts index 241715fb9..772fe734d 100644 --- a/server/controllers/feeds.ts +++ b/server/controllers/feeds.ts | |||
@@ -4,7 +4,8 @@ import { Feed } from '@peertube/feed' | |||
4 | import { mdToOneLinePlainText, toSafeHtml } from '@server/helpers/markdown' | 4 | import { mdToOneLinePlainText, toSafeHtml } from '@server/helpers/markdown' |
5 | import { getServerActor } from '@server/models/application/application' | 5 | import { getServerActor } from '@server/models/application/application' |
6 | import { getCategoryLabel } from '@server/models/video/formatter/video-format-utils' | 6 | import { getCategoryLabel } from '@server/models/video/formatter/video-format-utils' |
7 | import { VideoInclude } from '@shared/models' | 7 | import { MAccountDefault, MChannelBannerAccountDefault, MVideoFullLight } from '@server/types/models' |
8 | import { ActorImageType, VideoInclude } from '@shared/models' | ||
8 | import { buildNSFWFilter } from '../helpers/express-utils' | 9 | import { buildNSFWFilter } from '../helpers/express-utils' |
9 | import { CONFIG } from '../initializers/config' | 10 | import { CONFIG } from '../initializers/config' |
10 | import { MIMETYPES, PREVIEWS_SIZE, ROUTE_CACHE_LIFETIME, WEBSERVER } from '../initializers/constants' | 11 | import { MIMETYPES, PREVIEWS_SIZE, ROUTE_CACHE_LIFETIME, WEBSERVER } from '../initializers/constants' |
@@ -82,22 +83,12 @@ async function generateVideoCommentsFeed (req: express.Request, res: express.Res | |||
82 | videoChannelId: videoChannel ? videoChannel.id : undefined | 83 | videoChannelId: videoChannel ? videoChannel.id : undefined |
83 | }) | 84 | }) |
84 | 85 | ||
85 | let name: string | 86 | const { name, description, imageUrl } = buildFeedMetadata({ video, account, videoChannel }) |
86 | let description: string | ||
87 | 87 | ||
88 | if (videoChannel) { | ||
89 | name = videoChannel.getDisplayName() | ||
90 | description = videoChannel.description | ||
91 | } else if (account) { | ||
92 | name = account.getDisplayName() | ||
93 | description = account.description | ||
94 | } else { | ||
95 | name = video ? video.name : CONFIG.INSTANCE.NAME | ||
96 | description = video ? video.description : CONFIG.INSTANCE.DESCRIPTION | ||
97 | } | ||
98 | const feed = initFeed({ | 88 | const feed = initFeed({ |
99 | name, | 89 | name, |
100 | description, | 90 | description, |
91 | imageUrl, | ||
101 | resourceType: 'video-comments', | 92 | resourceType: 'video-comments', |
102 | queryString: new URL(WEBSERVER.URL + req.originalUrl).search | 93 | queryString: new URL(WEBSERVER.URL + req.originalUrl).search |
103 | }) | 94 | }) |
@@ -137,23 +128,12 @@ async function generateVideoFeed (req: express.Request, res: express.Response) { | |||
137 | const videoChannel = res.locals.videoChannel | 128 | const videoChannel = res.locals.videoChannel |
138 | const nsfw = buildNSFWFilter(res, req.query.nsfw) | 129 | const nsfw = buildNSFWFilter(res, req.query.nsfw) |
139 | 130 | ||
140 | let name: string | 131 | const { name, description, imageUrl } = buildFeedMetadata({ videoChannel, account }) |
141 | let description: string | ||
142 | |||
143 | if (videoChannel) { | ||
144 | name = videoChannel.getDisplayName() | ||
145 | description = videoChannel.description | ||
146 | } else if (account) { | ||
147 | name = account.getDisplayName() | ||
148 | description = account.description | ||
149 | } else { | ||
150 | name = CONFIG.INSTANCE.NAME | ||
151 | description = CONFIG.INSTANCE.DESCRIPTION | ||
152 | } | ||
153 | 132 | ||
154 | const feed = initFeed({ | 133 | const feed = initFeed({ |
155 | name, | 134 | name, |
156 | description, | 135 | description, |
136 | imageUrl, | ||
157 | resourceType: 'videos', | 137 | resourceType: 'videos', |
158 | queryString: new URL(WEBSERVER.URL + req.url).search | 138 | queryString: new URL(WEBSERVER.URL + req.url).search |
159 | }) | 139 | }) |
@@ -190,12 +170,13 @@ async function generateVideoFeedForSubscriptions (req: express.Request, res: exp | |||
190 | const start = 0 | 170 | const start = 0 |
191 | const account = res.locals.account | 171 | const account = res.locals.account |
192 | const nsfw = buildNSFWFilter(res, req.query.nsfw) | 172 | const nsfw = buildNSFWFilter(res, req.query.nsfw) |
193 | const name = account.getDisplayName() | 173 | |
194 | const description = account.description | 174 | const { name, description, imageUrl } = buildFeedMetadata({ account }) |
195 | 175 | ||
196 | const feed = initFeed({ | 176 | const feed = initFeed({ |
197 | name, | 177 | name, |
198 | description, | 178 | description, |
179 | imageUrl, | ||
199 | resourceType: 'videos', | 180 | resourceType: 'videos', |
200 | queryString: new URL(WEBSERVER.URL + req.url).search | 181 | queryString: new URL(WEBSERVER.URL + req.url).search |
201 | }) | 182 | }) |
@@ -229,11 +210,12 @@ async function generateVideoFeedForSubscriptions (req: express.Request, res: exp | |||
229 | function initFeed (parameters: { | 210 | function initFeed (parameters: { |
230 | name: string | 211 | name: string |
231 | description: string | 212 | description: string |
213 | imageUrl: string | ||
232 | resourceType?: 'videos' | 'video-comments' | 214 | resourceType?: 'videos' | 'video-comments' |
233 | queryString?: string | 215 | queryString?: string |
234 | }) { | 216 | }) { |
235 | const webserverUrl = WEBSERVER.URL | 217 | const webserverUrl = WEBSERVER.URL |
236 | const { name, description, resourceType, queryString } = parameters | 218 | const { name, description, resourceType, queryString, imageUrl } = parameters |
237 | 219 | ||
238 | return new Feed({ | 220 | return new Feed({ |
239 | title: name, | 221 | title: name, |
@@ -241,7 +223,7 @@ function initFeed (parameters: { | |||
241 | // updated: TODO: somehowGetLatestUpdate, // optional, default = today | 223 | // updated: TODO: somehowGetLatestUpdate, // optional, default = today |
242 | id: webserverUrl, | 224 | id: webserverUrl, |
243 | link: webserverUrl, | 225 | link: webserverUrl, |
244 | image: webserverUrl + '/client/assets/images/icons/icon-96x96.png', | 226 | image: imageUrl, |
245 | favicon: webserverUrl + '/client/assets/images/favicon.png', | 227 | favicon: webserverUrl + '/client/assets/images/favicon.png', |
246 | copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` + | 228 | copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` + |
247 | ` and potential licenses granted by each content's rightholder.`, | 229 | ` and potential licenses granted by each content's rightholder.`, |
@@ -369,3 +351,39 @@ function sendFeed (feed: Feed, req: express.Request, res: express.Response) { | |||
369 | 351 | ||
370 | return res.send(feed.rss2()).end() | 352 | return res.send(feed.rss2()).end() |
371 | } | 353 | } |
354 | |||
355 | function buildFeedMetadata (options: { | ||
356 | videoChannel?: MChannelBannerAccountDefault | ||
357 | account?: MAccountDefault | ||
358 | video?: MVideoFullLight | ||
359 | }) { | ||
360 | const { video, videoChannel, account } = options | ||
361 | |||
362 | let imageUrl = WEBSERVER.URL + '/client/assets/images/icons/icon-96x96.png' | ||
363 | let name: string | ||
364 | let description: string | ||
365 | |||
366 | if (videoChannel) { | ||
367 | name = videoChannel.getDisplayName() | ||
368 | description = videoChannel.description | ||
369 | |||
370 | if (videoChannel.Actor.hasImage(ActorImageType.AVATAR)) { | ||
371 | imageUrl = WEBSERVER.URL + videoChannel.Actor.Avatars[0].getStaticPath() | ||
372 | } | ||
373 | } else if (account) { | ||
374 | name = account.getDisplayName() | ||
375 | description = account.description | ||
376 | |||
377 | if (account.Actor.hasImage(ActorImageType.AVATAR)) { | ||
378 | imageUrl = WEBSERVER.URL + account.Actor.Avatars[0].getStaticPath() | ||
379 | } | ||
380 | } else if (video) { | ||
381 | name = video.name | ||
382 | description = video.description | ||
383 | } else { | ||
384 | name = CONFIG.INSTANCE.NAME | ||
385 | description = CONFIG.INSTANCE.DESCRIPTION | ||
386 | } | ||
387 | |||
388 | return { name, description, imageUrl } | ||
389 | } | ||
diff --git a/server/controllers/index.ts b/server/controllers/index.ts index e8833d58c..eaa2dd7c8 100644 --- a/server/controllers/index.ts +++ b/server/controllers/index.ts | |||
@@ -1,15 +1,15 @@ | |||
1 | export * from './activitypub' | 1 | export * from './activitypub' |
2 | export * from './api' | 2 | export * from './api' |
3 | export * from './bots' | ||
3 | export * from './client' | 4 | export * from './client' |
4 | export * from './download' | 5 | export * from './download' |
5 | export * from './feeds' | 6 | export * from './feeds' |
6 | export * from './services' | ||
7 | export * from './static' | ||
8 | export * from './lazy-static' | 7 | export * from './lazy-static' |
9 | export * from './live' | ||
10 | export * from './misc' | 8 | export * from './misc' |
11 | export * from './webfinger' | 9 | export * from './object-storage-proxy' |
12 | export * from './tracker' | ||
13 | export * from './bots' | ||
14 | export * from './plugins' | 10 | export * from './plugins' |
11 | export * from './services' | ||
12 | export * from './static' | ||
13 | export * from './tracker' | ||
14 | export * from './webfinger' | ||
15 | export * from './well-known' | 15 | export * from './well-known' |
diff --git a/server/controllers/live.ts b/server/controllers/live.ts deleted file mode 100644 index 81008f120..000000000 --- a/server/controllers/live.ts +++ /dev/null | |||
@@ -1,32 +0,0 @@ | |||
1 | import cors from 'cors' | ||
2 | import express from 'express' | ||
3 | import { mapToJSON } from '@server/helpers/core-utils' | ||
4 | import { LiveSegmentShaStore } from '@server/lib/live' | ||
5 | import { HttpStatusCode } from '@shared/models' | ||
6 | |||
7 | const liveRouter = express.Router() | ||
8 | |||
9 | liveRouter.use('/segments-sha256/:videoUUID', | ||
10 | cors(), | ||
11 | getSegmentsSha256 | ||
12 | ) | ||
13 | |||
14 | // --------------------------------------------------------------------------- | ||
15 | |||
16 | export { | ||
17 | liveRouter | ||
18 | } | ||
19 | |||
20 | // --------------------------------------------------------------------------- | ||
21 | |||
22 | function getSegmentsSha256 (req: express.Request, res: express.Response) { | ||
23 | const videoUUID = req.params.videoUUID | ||
24 | |||
25 | const result = LiveSegmentShaStore.Instance.getSegmentsSha256(videoUUID) | ||
26 | |||
27 | if (!result) { | ||
28 | return res.status(HttpStatusCode.NOT_FOUND_404).end() | ||
29 | } | ||
30 | |||
31 | return res.json(mapToJSON(result)) | ||
32 | } | ||
diff --git a/server/controllers/object-storage-proxy.ts b/server/controllers/object-storage-proxy.ts new file mode 100644 index 000000000..3ce279671 --- /dev/null +++ b/server/controllers/object-storage-proxy.ts | |||
@@ -0,0 +1,86 @@ | |||
1 | import cors from 'cors' | ||
2 | import express from 'express' | ||
3 | import { logger } from '@server/helpers/logger' | ||
4 | import { OBJECT_STORAGE_PROXY_PATHS } from '@server/initializers/constants' | ||
5 | import { getHLSFileReadStream, getWebTorrentFileReadStream } from '@server/lib/object-storage' | ||
6 | import { | ||
7 | asyncMiddleware, | ||
8 | ensureCanAccessPrivateVideoHLSFiles, | ||
9 | ensureCanAccessVideoPrivateWebTorrentFiles, | ||
10 | ensurePrivateObjectStorageProxyIsEnabled, | ||
11 | optionalAuthenticate | ||
12 | } from '@server/middlewares' | ||
13 | import { HttpStatusCode } from '@shared/models' | ||
14 | |||
15 | const objectStorageProxyRouter = express.Router() | ||
16 | |||
17 | objectStorageProxyRouter.use(cors()) | ||
18 | |||
19 | objectStorageProxyRouter.get(OBJECT_STORAGE_PROXY_PATHS.PRIVATE_WEBSEED + ':filename', | ||
20 | ensurePrivateObjectStorageProxyIsEnabled, | ||
21 | optionalAuthenticate, | ||
22 | asyncMiddleware(ensureCanAccessVideoPrivateWebTorrentFiles), | ||
23 | asyncMiddleware(proxifyWebTorrent) | ||
24 | ) | ||
25 | |||
26 | objectStorageProxyRouter.get(OBJECT_STORAGE_PROXY_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS + ':videoUUID/:filename', | ||
27 | ensurePrivateObjectStorageProxyIsEnabled, | ||
28 | optionalAuthenticate, | ||
29 | asyncMiddleware(ensureCanAccessPrivateVideoHLSFiles), | ||
30 | asyncMiddleware(proxifyHLS) | ||
31 | ) | ||
32 | |||
33 | // --------------------------------------------------------------------------- | ||
34 | |||
35 | export { | ||
36 | objectStorageProxyRouter | ||
37 | } | ||
38 | |||
39 | async function proxifyWebTorrent (req: express.Request, res: express.Response) { | ||
40 | const filename = req.params.filename | ||
41 | |||
42 | logger.debug('Proxifying WebTorrent file %s from object storage.', filename) | ||
43 | |||
44 | try { | ||
45 | const stream = await getWebTorrentFileReadStream({ | ||
46 | filename, | ||
47 | rangeHeader: req.header('range') | ||
48 | }) | ||
49 | |||
50 | return stream.pipe(res) | ||
51 | } catch (err) { | ||
52 | return handleObjectStorageFailure(res, err) | ||
53 | } | ||
54 | } | ||
55 | |||
56 | async function proxifyHLS (req: express.Request, res: express.Response) { | ||
57 | const playlist = res.locals.videoStreamingPlaylist | ||
58 | const video = res.locals.onlyVideo | ||
59 | const filename = req.params.filename | ||
60 | |||
61 | logger.debug('Proxifying HLS file %s from object storage.', filename) | ||
62 | |||
63 | try { | ||
64 | const stream = await getHLSFileReadStream({ | ||
65 | playlist: playlist.withVideo(video), | ||
66 | filename, | ||
67 | rangeHeader: req.header('range') | ||
68 | }) | ||
69 | |||
70 | return stream.pipe(res) | ||
71 | } catch (err) { | ||
72 | return handleObjectStorageFailure(res, err) | ||
73 | } | ||
74 | } | ||
75 | |||
76 | function handleObjectStorageFailure (res: express.Response, err: Error) { | ||
77 | if (err.name === 'NoSuchKey') { | ||
78 | return res.sendStatus(HttpStatusCode.NOT_FOUND_404) | ||
79 | } | ||
80 | |||
81 | return res.fail({ | ||
82 | status: HttpStatusCode.INTERNAL_SERVER_ERROR_500, | ||
83 | message: err.message, | ||
84 | type: err.name | ||
85 | }) | ||
86 | } | ||
diff --git a/server/controllers/services.ts b/server/controllers/services.ts index cabcbc00b..7c7ca1ff3 100644 --- a/server/controllers/services.ts +++ b/server/controllers/services.ts | |||
@@ -4,6 +4,7 @@ import { escapeHTML } from '@shared/core-utils/renderer' | |||
4 | import { EMBED_SIZE, PREVIEWS_SIZE, THUMBNAILS_SIZE, WEBSERVER } from '../initializers/constants' | 4 | import { EMBED_SIZE, PREVIEWS_SIZE, THUMBNAILS_SIZE, WEBSERVER } from '../initializers/constants' |
5 | import { asyncMiddleware, oembedValidator } from '../middlewares' | 5 | import { asyncMiddleware, oembedValidator } from '../middlewares' |
6 | import { accountNameWithHostGetValidator } from '../middlewares/validators' | 6 | import { accountNameWithHostGetValidator } from '../middlewares/validators' |
7 | import { forceNumber } from '@shared/core-utils' | ||
7 | 8 | ||
8 | const servicesRouter = express.Router() | 9 | const servicesRouter = express.Router() |
9 | 10 | ||
@@ -108,8 +109,8 @@ function buildOEmbed (options: { | |||
108 | const { req, previewSize, previewPath, title, channel, embedPath } = options | 109 | const { req, previewSize, previewPath, title, channel, embedPath } = options |
109 | 110 | ||
110 | const webserverUrl = WEBSERVER.URL | 111 | const webserverUrl = WEBSERVER.URL |
111 | const maxHeight = parseInt(req.query.maxheight, 10) | 112 | const maxHeight = forceNumber(req.query.maxheight) |
112 | const maxWidth = parseInt(req.query.maxwidth, 10) | 113 | const maxWidth = forceNumber(req.query.maxwidth) |
113 | 114 | ||
114 | const embedUrl = webserverUrl + embedPath | 115 | const embedUrl = webserverUrl + embedPath |
115 | const embedTitle = escapeHTML(title) | 116 | const embedTitle = escapeHTML(title) |
diff --git a/server/controllers/static.ts b/server/controllers/static.ts index 33c429eb1..6ef9154b9 100644 --- a/server/controllers/static.ts +++ b/server/controllers/static.ts | |||
@@ -1,30 +1,63 @@ | |||
1 | import cors from 'cors' | 1 | import cors from 'cors' |
2 | import express from 'express' | 2 | import express from 'express' |
3 | import { handleStaticError } from '@server/middlewares' | 3 | import { |
4 | asyncMiddleware, | ||
5 | ensureCanAccessPrivateVideoHLSFiles, | ||
6 | ensureCanAccessVideoPrivateWebTorrentFiles, | ||
7 | handleStaticError, | ||
8 | optionalAuthenticate | ||
9 | } from '@server/middlewares' | ||
4 | import { CONFIG } from '../initializers/config' | 10 | import { CONFIG } from '../initializers/config' |
5 | import { HLS_STREAMING_PLAYLIST_DIRECTORY, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers/constants' | 11 | import { DIRECTORIES, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers/constants' |
6 | 12 | ||
7 | const staticRouter = express.Router() | 13 | const staticRouter = express.Router() |
8 | 14 | ||
9 | // Cors is very important to let other servers access torrent and video files | 15 | // Cors is very important to let other servers access torrent and video files |
10 | staticRouter.use(cors()) | 16 | staticRouter.use(cors()) |
11 | 17 | ||
12 | // Videos path for webseed | 18 | // --------------------------------------------------------------------------- |
19 | // WebTorrent/Classic videos | ||
20 | // --------------------------------------------------------------------------- | ||
21 | |||
22 | const privateWebTorrentStaticMiddlewares = CONFIG.STATIC_FILES.PRIVATE_FILES_REQUIRE_AUTH === true | ||
23 | ? [ optionalAuthenticate, asyncMiddleware(ensureCanAccessVideoPrivateWebTorrentFiles) ] | ||
24 | : [] | ||
25 | |||
26 | staticRouter.use( | ||
27 | STATIC_PATHS.PRIVATE_WEBSEED, | ||
28 | ...privateWebTorrentStaticMiddlewares, | ||
29 | express.static(DIRECTORIES.VIDEOS.PRIVATE, { fallthrough: false }), | ||
30 | handleStaticError | ||
31 | ) | ||
13 | staticRouter.use( | 32 | staticRouter.use( |
14 | STATIC_PATHS.WEBSEED, | 33 | STATIC_PATHS.WEBSEED, |
15 | express.static(CONFIG.STORAGE.VIDEOS_DIR, { fallthrough: false }), | 34 | express.static(DIRECTORIES.VIDEOS.PUBLIC, { fallthrough: false }), |
16 | handleStaticError | 35 | handleStaticError |
17 | ) | 36 | ) |
37 | |||
18 | staticRouter.use( | 38 | staticRouter.use( |
19 | STATIC_PATHS.REDUNDANCY, | 39 | STATIC_PATHS.REDUNDANCY, |
20 | express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }), | 40 | express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }), |
21 | handleStaticError | 41 | handleStaticError |
22 | ) | 42 | ) |
23 | 43 | ||
44 | // --------------------------------------------------------------------------- | ||
24 | // HLS | 45 | // HLS |
46 | // --------------------------------------------------------------------------- | ||
47 | |||
48 | const privateHLSStaticMiddlewares = CONFIG.STATIC_FILES.PRIVATE_FILES_REQUIRE_AUTH === true | ||
49 | ? [ optionalAuthenticate, asyncMiddleware(ensureCanAccessPrivateVideoHLSFiles) ] | ||
50 | : [] | ||
51 | |||
52 | staticRouter.use( | ||
53 | STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS, | ||
54 | ...privateHLSStaticMiddlewares, | ||
55 | express.static(DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE, { fallthrough: false }), | ||
56 | handleStaticError | ||
57 | ) | ||
25 | staticRouter.use( | 58 | staticRouter.use( |
26 | STATIC_PATHS.STREAMING_PLAYLISTS.HLS, | 59 | STATIC_PATHS.STREAMING_PLAYLISTS.HLS, |
27 | express.static(HLS_STREAMING_PLAYLIST_DIRECTORY, { fallthrough: false }), | 60 | express.static(DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC, { fallthrough: false }), |
28 | handleStaticError | 61 | handleStaticError |
29 | ) | 62 | ) |
30 | 63 | ||
diff --git a/server/controllers/well-known.ts b/server/controllers/well-known.ts index f467bd629..ce5883571 100644 --- a/server/controllers/well-known.ts +++ b/server/controllers/well-known.ts | |||
@@ -5,6 +5,7 @@ import { root } from '@shared/core-utils' | |||
5 | import { CONFIG } from '../initializers/config' | 5 | import { CONFIG } from '../initializers/config' |
6 | import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../initializers/constants' | 6 | import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../initializers/constants' |
7 | import { cacheRoute } from '../middlewares/cache/cache' | 7 | import { cacheRoute } from '../middlewares/cache/cache' |
8 | import { handleStaticError } from '@server/middlewares' | ||
8 | 9 | ||
9 | const wellKnownRouter = express.Router() | 10 | const wellKnownRouter = express.Router() |
10 | 11 | ||
@@ -69,6 +70,12 @@ wellKnownRouter.use('/.well-known/host-meta', | |||
69 | } | 70 | } |
70 | ) | 71 | ) |
71 | 72 | ||
73 | wellKnownRouter.use('/.well-known/', | ||
74 | cacheRoute(ROUTE_CACHE_LIFETIME.WELL_KNOWN), | ||
75 | express.static(CONFIG.STORAGE.WELL_KNOWN_DIR, { fallthrough: false }), | ||
76 | handleStaticError | ||
77 | ) | ||
78 | |||
72 | // --------------------------------------------------------------------------- | 79 | // --------------------------------------------------------------------------- |
73 | 80 | ||
74 | export { | 81 | export { |