aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers')
-rw-r--r--server/controllers/api/server/debug.ts2
-rw-r--r--server/controllers/api/videos/index.ts2
-rw-r--r--server/controllers/api/videos/token.ts33
-rw-r--r--server/controllers/api/videos/update.ts76
-rw-r--r--server/controllers/download.ts4
-rw-r--r--server/controllers/static.ts31
6 files changed, 86 insertions, 62 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'
8import { UserRight } from '../../../../shared/models/users' 8import { UserRight } from '../../../../shared/models/users'
9import { authenticate, ensureUserHasRight } from '../../../middlewares' 9import { authenticate, ensureUserHasRight } from '../../../middlewares'
10import { VideoChannelSyncLatestScheduler } from '@server/lib/schedulers/video-channel-sync-latest-scheduler' 10import { VideoChannelSyncLatestScheduler } from '@server/lib/schedulers/video-channel-sync-latest-scheduler'
11import { UpdateVideosScheduler } from '@server/lib/schedulers/update-videos-scheduler'
11 12
12const debugRouter = express.Router() 13const 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/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'
41import { rateVideoRouter } from './rate' 41import { rateVideoRouter } from './rate'
42import { statsRouter } from './stats' 42import { statsRouter } from './stats'
43import { studioRouter } from './studio' 43import { studioRouter } from './studio'
44import { tokenRouter } from './token'
44import { transcodingRouter } from './transcoding' 45import { transcodingRouter } from './transcoding'
45import { updateRouter } from './update' 46import { updateRouter } from './update'
46import { uploadRouter } from './upload' 47import { uploadRouter } from './upload'
@@ -63,6 +64,7 @@ videosRouter.use('/', uploadRouter)
63videosRouter.use('/', updateRouter) 64videosRouter.use('/', updateRouter)
64videosRouter.use('/', filesRouter) 65videosRouter.use('/', filesRouter)
65videosRouter.use('/', transcodingRouter) 66videosRouter.use('/', transcodingRouter)
67videosRouter.use('/', tokenRouter)
66 68
67videosRouter.get('/categories', 69videosRouter.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 @@
1import express from 'express'
2import { VideoTokensManager } from '@server/lib/video-tokens-manager'
3import { VideoToken } from '@shared/models'
4import { asyncMiddleware, authenticate, videosCustomGetValidator } from '../../../middlewares'
5
6const tokenRouter = express.Router()
7
8tokenRouter.post('/:id/token',
9 authenticate,
10 asyncMiddleware(videosCustomGetValidator('only-video')),
11 generateToken
12)
13
14// ---------------------------------------------------------------------------
15
16export {
17 tokenRouter
18}
19
20// ---------------------------------------------------------------------------
21
22function 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/update.ts b/server/controllers/api/videos/update.ts
index ab1a23d9a..0a910379a 100644
--- a/server/controllers/api/videos/update.ts
+++ b/server/controllers/api/videos/update.ts
@@ -1,12 +1,12 @@
1import express from 'express' 1import express from 'express'
2import { Transaction } from 'sequelize/types' 2import { Transaction } from 'sequelize/types'
3import { changeVideoChannelShare } from '@server/lib/activitypub/share' 3import { changeVideoChannelShare } from '@server/lib/activitypub/share'
4import { CreateJobArgument, JobQueue } from '@server/lib/job-queue' 4import { addVideoJobsAfterUpdate, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video'
5import { buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video' 5import { setVideoPrivacy } from '@server/lib/video-privacy'
6import { openapiOperationDoc } from '@server/middlewares/doc' 6import { openapiOperationDoc } from '@server/middlewares/doc'
7import { FilteredModelAttributes } from '@server/types' 7import { FilteredModelAttributes } from '@server/types'
8import { MVideoFullLight } from '@server/types/models' 8import { MVideoFullLight } from '@server/types/models'
9import { HttpStatusCode, ManageVideoTorrentPayload, VideoUpdate } from '@shared/models' 9import { HttpStatusCode, VideoUpdate } from '@shared/models'
10import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' 10import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger'
11import { resetSequelizeInstance } from '../../../helpers/database-utils' 11import { resetSequelizeInstance } from '../../../helpers/database-utils'
12import { createReqFiles } from '../../../helpers/express-utils' 12import { createReqFiles } from '../../../helpers/express-utils'
@@ -18,6 +18,7 @@ import { autoBlacklistVideoIfNeeded } from '../../../lib/video-blacklist'
18import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videosUpdateValidator } from '../../../middlewares' 18import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videosUpdateValidator } from '../../../middlewares'
19import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update' 19import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update'
20import { VideoModel } from '../../../models/video/video' 20import { VideoModel } from '../../../models/video/video'
21import { VideoPathManager } from '@server/lib/video-path-manager'
21 22
22const lTags = loggerTagsFactory('api', 'video') 23const lTags = loggerTagsFactory('api', 'video')
23const auditLogger = auditLoggerFactory('videos') 24const auditLogger = auditLoggerFactory('videos')
@@ -47,8 +48,8 @@ async function updateVideo (req: express.Request, res: express.Response) {
47 const oldVideoAuditView = new VideoAuditView(videoFromReq.toFormattedDetailsJSON()) 48 const oldVideoAuditView = new VideoAuditView(videoFromReq.toFormattedDetailsJSON())
48 const videoInfoToUpdate: VideoUpdate = req.body 49 const videoInfoToUpdate: VideoUpdate = req.body
49 50
50 const wasConfidentialVideo = videoFromReq.isConfidential()
51 const hadPrivacyForFederation = videoFromReq.hasPrivacyForFederation() 51 const hadPrivacyForFederation = videoFromReq.hasPrivacyForFederation()
52 const oldPrivacy = videoFromReq.privacy
52 53
53 const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({ 54 const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({
54 video: videoFromReq, 55 video: videoFromReq,
@@ -57,12 +58,13 @@ async function updateVideo (req: express.Request, res: express.Response) {
57 automaticallyGenerated: false 58 automaticallyGenerated: false
58 }) 59 })
59 60
61 const videoFileLockReleaser = await VideoPathManager.Instance.lockFiles(videoFromReq.uuid)
62
60 try { 63 try {
61 const { videoInstanceUpdated, isNewVideo } = await sequelizeTypescript.transaction(async t => { 64 const { videoInstanceUpdated, isNewVideo } = await sequelizeTypescript.transaction(async t => {
62 // Refresh video since thumbnails to prevent concurrent updates 65 // Refresh video since thumbnails to prevent concurrent updates
63 const video = await VideoModel.loadFull(videoFromReq.id, t) 66 const video = await VideoModel.loadFull(videoFromReq.id, t)
64 67
65 const sequelizeOptions = { transaction: t }
66 const oldVideoChannel = video.VideoChannel 68 const oldVideoChannel = video.VideoChannel
67 69
68 const keysToUpdate: (keyof VideoUpdate & FilteredModelAttributes<VideoModel>)[] = [ 70 const keysToUpdate: (keyof VideoUpdate & FilteredModelAttributes<VideoModel>)[] = [
@@ -97,7 +99,7 @@ async function updateVideo (req: express.Request, res: express.Response) {
97 await video.setAsRefreshed(t) 99 await video.setAsRefreshed(t)
98 } 100 }
99 101
100 const videoInstanceUpdated = await video.save(sequelizeOptions) as MVideoFullLight 102 const videoInstanceUpdated = await video.save({ transaction: t }) as MVideoFullLight
101 103
102 // Thumbnail & preview updates? 104 // Thumbnail & preview updates?
103 if (thumbnailModel) await videoInstanceUpdated.addAndSaveThumbnail(thumbnailModel, t) 105 if (thumbnailModel) await videoInstanceUpdated.addAndSaveThumbnail(thumbnailModel, t)
@@ -113,7 +115,9 @@ async function updateVideo (req: express.Request, res: express.Response) {
113 await videoInstanceUpdated.$set('VideoChannel', res.locals.videoChannel, { transaction: t }) 115 await videoInstanceUpdated.$set('VideoChannel', res.locals.videoChannel, { transaction: t })
114 videoInstanceUpdated.VideoChannel = res.locals.videoChannel 116 videoInstanceUpdated.VideoChannel = res.locals.videoChannel
115 117
116 if (hadPrivacyForFederation === true) await changeVideoChannelShare(videoInstanceUpdated, oldVideoChannel, t) 118 if (hadPrivacyForFederation === true) {
119 await changeVideoChannelShare(videoInstanceUpdated, oldVideoChannel, t)
120 }
117 } 121 }
118 122
119 // Schedule an update in the future? 123 // Schedule an update in the future?
@@ -139,7 +143,12 @@ async function updateVideo (req: express.Request, res: express.Response) {
139 143
140 Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body, req, res }) 144 Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body, req, res })
141 145
142 await addVideoJobsAfterUpdate({ video: videoInstanceUpdated, videoInfoToUpdate, wasConfidentialVideo, isNewVideo }) 146 await addVideoJobsAfterUpdate({
147 video: videoInstanceUpdated,
148 nameChanged: !!videoInfoToUpdate.name,
149 oldPrivacy,
150 isNewVideo
151 })
143 } catch (err) { 152 } catch (err) {
144 // Force fields we want to update 153 // Force fields we want to update
145 // If the transaction is retried, sequelize will think the object has not changed 154 // If the transaction is retried, sequelize will think the object has not changed
@@ -147,6 +156,8 @@ async function updateVideo (req: express.Request, res: express.Response) {
147 resetSequelizeInstance(videoFromReq, videoFieldsSave) 156 resetSequelizeInstance(videoFromReq, videoFieldsSave)
148 157
149 throw err 158 throw err
159 } finally {
160 videoFileLockReleaser()
150 } 161 }
151 162
152 return res.type('json') 163 return res.type('json')
@@ -164,7 +175,7 @@ async function updateVideoPrivacy (options: {
164 const isNewVideo = videoInstance.isNewVideo(videoInfoToUpdate.privacy) 175 const isNewVideo = videoInstance.isNewVideo(videoInfoToUpdate.privacy)
165 176
166 const newPrivacy = parseInt(videoInfoToUpdate.privacy.toString(), 10) 177 const newPrivacy = parseInt(videoInfoToUpdate.privacy.toString(), 10)
167 videoInstance.setPrivacy(newPrivacy) 178 setVideoPrivacy(videoInstance, newPrivacy)
168 179
169 // Unfederate the video if the new privacy is not compatible with federation 180 // Unfederate the video if the new privacy is not compatible with federation
170 if (hadPrivacyForFederation && !videoInstance.hasPrivacyForFederation()) { 181 if (hadPrivacyForFederation && !videoInstance.hasPrivacyForFederation()) {
@@ -185,50 +196,3 @@ function updateSchedule (videoInstance: MVideoFullLight, videoInfoToUpdate: Vide
185 return ScheduleVideoUpdateModel.deleteByVideoId(videoInstance.id, transaction) 196 return ScheduleVideoUpdateModel.deleteByVideoId(videoInstance.id, transaction)
186 } 197 }
187} 198}
188
189async 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..abd1df26f 100644
--- a/server/controllers/download.ts
+++ b/server/controllers/download.ts
@@ -7,7 +7,7 @@ import { VideoPathManager } from '@server/lib/video-path-manager'
7import { MStreamingPlaylist, MVideo, MVideoFile, MVideoFullLight } from '@server/types/models' 7import { MStreamingPlaylist, MVideo, MVideoFile, MVideoFullLight } from '@server/types/models'
8import { HttpStatusCode, VideoStorage, VideoStreamingPlaylistType } from '@shared/models' 8import { HttpStatusCode, VideoStorage, VideoStreamingPlaylistType } from '@shared/models'
9import { STATIC_DOWNLOAD_PATHS } from '../initializers/constants' 9import { STATIC_DOWNLOAD_PATHS } from '../initializers/constants'
10import { asyncMiddleware, videosDownloadValidator } from '../middlewares' 10import { asyncMiddleware, optionalAuthenticate, videosDownloadValidator } from '../middlewares'
11 11
12const downloadRouter = express.Router() 12const downloadRouter = express.Router()
13 13
@@ -20,12 +20,14 @@ downloadRouter.use(
20 20
21downloadRouter.use( 21downloadRouter.use(
22 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension', 22 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
23 optionalAuthenticate,
23 asyncMiddleware(videosDownloadValidator), 24 asyncMiddleware(videosDownloadValidator),
24 asyncMiddleware(downloadVideoFile) 25 asyncMiddleware(downloadVideoFile)
25) 26)
26 27
27downloadRouter.use( 28downloadRouter.use(
28 STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + ':id-:resolution([0-9]+)-fragmented.:extension', 29 STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + ':id-:resolution([0-9]+)-fragmented.:extension',
30 optionalAuthenticate,
29 asyncMiddleware(videosDownloadValidator), 31 asyncMiddleware(videosDownloadValidator),
30 asyncMiddleware(downloadHLSVideoFile) 32 asyncMiddleware(downloadHLSVideoFile)
31) 33)
diff --git a/server/controllers/static.ts b/server/controllers/static.ts
index 33c429eb1..dc091455a 100644
--- a/server/controllers/static.ts
+++ b/server/controllers/static.ts
@@ -1,20 +1,34 @@
1import cors from 'cors' 1import cors from 'cors'
2import express from 'express' 2import express from 'express'
3import { handleStaticError } from '@server/middlewares' 3import {
4 asyncMiddleware,
5 ensureCanAccessPrivateVideoHLSFiles,
6 ensureCanAccessVideoPrivateWebTorrentFiles,
7 handleStaticError,
8 optionalAuthenticate
9} from '@server/middlewares'
4import { CONFIG } from '../initializers/config' 10import { CONFIG } from '../initializers/config'
5import { HLS_STREAMING_PLAYLIST_DIRECTORY, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers/constants' 11import { DIRECTORIES, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers/constants'
6 12
7const staticRouter = express.Router() 13const 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
10staticRouter.use(cors()) 16staticRouter.use(cors())
11 17
12// Videos path for webseed 18// WebTorrent/Classic videos
19staticRouter.use(
20 STATIC_PATHS.PRIVATE_WEBSEED,
21 optionalAuthenticate,
22 asyncMiddleware(ensureCanAccessVideoPrivateWebTorrentFiles),
23 express.static(DIRECTORIES.VIDEOS.PRIVATE, { fallthrough: false }),
24 handleStaticError
25)
13staticRouter.use( 26staticRouter.use(
14 STATIC_PATHS.WEBSEED, 27 STATIC_PATHS.WEBSEED,
15 express.static(CONFIG.STORAGE.VIDEOS_DIR, { fallthrough: false }), 28 express.static(DIRECTORIES.VIDEOS.PUBLIC, { fallthrough: false }),
16 handleStaticError 29 handleStaticError
17) 30)
31
18staticRouter.use( 32staticRouter.use(
19 STATIC_PATHS.REDUNDANCY, 33 STATIC_PATHS.REDUNDANCY,
20 express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }), 34 express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }),
@@ -23,8 +37,15 @@ staticRouter.use(
23 37
24// HLS 38// HLS
25staticRouter.use( 39staticRouter.use(
40 STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS,
41 optionalAuthenticate,
42 asyncMiddleware(ensureCanAccessPrivateVideoHLSFiles),
43 express.static(DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE, { fallthrough: false }),
44 handleStaticError
45)
46staticRouter.use(
26 STATIC_PATHS.STREAMING_PLAYLISTS.HLS, 47 STATIC_PATHS.STREAMING_PLAYLISTS.HLS,
27 express.static(HLS_STREAMING_PLAYLIST_DIRECTORY, { fallthrough: false }), 48 express.static(DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC, { fallthrough: false }),
28 handleStaticError 49 handleStaticError
29) 50)
30 51