aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers')
-rw-r--r--server/controllers/api/videos/captions.ts28
-rw-r--r--server/controllers/api/videos/import.ts32
-rw-r--r--server/controllers/lazy-static.ts7
3 files changed, 31 insertions, 36 deletions
diff --git a/server/controllers/api/videos/captions.ts b/server/controllers/api/videos/captions.ts
index bf82e2c19..ad7423a31 100644
--- a/server/controllers/api/videos/captions.ts
+++ b/server/controllers/api/videos/captions.ts
@@ -1,17 +1,17 @@
1import * as express from 'express' 1import * as express from 'express'
2import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares' 2import { MVideoCaption } from '@server/types/models'
3import { addVideoCaptionValidator, deleteVideoCaptionValidator, listVideoCaptionsValidator } from '../../../middlewares/validators' 3import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
4import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils'
4import { createReqFiles } from '../../../helpers/express-utils' 5import { createReqFiles } from '../../../helpers/express-utils'
5import { MIMETYPES } from '../../../initializers/constants'
6import { getFormattedObjects } from '../../../helpers/utils'
7import { VideoCaptionModel } from '../../../models/video/video-caption'
8import { logger } from '../../../helpers/logger' 6import { logger } from '../../../helpers/logger'
9import { federateVideoIfNeeded } from '../../../lib/activitypub/videos' 7import { getFormattedObjects } from '../../../helpers/utils'
10import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils'
11import { CONFIG } from '../../../initializers/config' 8import { CONFIG } from '../../../initializers/config'
9import { MIMETYPES } from '../../../initializers/constants'
12import { sequelizeTypescript } from '../../../initializers/database' 10import { sequelizeTypescript } from '../../../initializers/database'
13import { MVideoCaptionVideo } from '@server/types/models' 11import { federateVideoIfNeeded } from '../../../lib/activitypub/videos'
14import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' 12import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares'
13import { addVideoCaptionValidator, deleteVideoCaptionValidator, listVideoCaptionsValidator } from '../../../middlewares/validators'
14import { VideoCaptionModel } from '../../../models/video/video-caption'
15 15
16const reqVideoCaptionAdd = createReqFiles( 16const reqVideoCaptionAdd = createReqFiles(
17 [ 'captionfile' ], 17 [ 'captionfile' ],
@@ -57,17 +57,19 @@ async function addVideoCaption (req: express.Request, res: express.Response) {
57 const videoCaptionPhysicalFile = req.files['captionfile'][0] 57 const videoCaptionPhysicalFile = req.files['captionfile'][0]
58 const video = res.locals.videoAll 58 const video = res.locals.videoAll
59 59
60 const captionLanguage = req.params.captionLanguage
61
60 const videoCaption = new VideoCaptionModel({ 62 const videoCaption = new VideoCaptionModel({
61 videoId: video.id, 63 videoId: video.id,
62 language: req.params.captionLanguage 64 filename: VideoCaptionModel.generateCaptionName(captionLanguage),
63 }) as MVideoCaptionVideo 65 language: captionLanguage
64 videoCaption.Video = video 66 }) as MVideoCaption
65 67
66 // Move physical file 68 // Move physical file
67 await moveAndProcessCaptionFile(videoCaptionPhysicalFile, videoCaption) 69 await moveAndProcessCaptionFile(videoCaptionPhysicalFile, videoCaption)
68 70
69 await sequelizeTypescript.transaction(async t => { 71 await sequelizeTypescript.transaction(async t => {
70 await VideoCaptionModel.insertOrReplaceLanguage(video.id, req.params.captionLanguage, null, t) 72 await VideoCaptionModel.insertOrReplaceLanguage(videoCaption, t)
71 73
72 // Update video update 74 // Update video update
73 await federateVideoIfNeeded(video, false, t) 75 await federateVideoIfNeeded(video, false, t)
diff --git a/server/controllers/api/videos/import.ts b/server/controllers/api/videos/import.ts
index 01f41e7bc..c689cb6f9 100644
--- a/server/controllers/api/videos/import.ts
+++ b/server/controllers/api/videos/import.ts
@@ -9,9 +9,9 @@ import {
9 MThumbnail, 9 MThumbnail,
10 MUser, 10 MUser,
11 MVideoAccountDefault, 11 MVideoAccountDefault,
12 MVideoCaptionVideo, 12 MVideoCaption,
13 MVideoTag, 13 MVideoTag,
14 MVideoThumbnailAccountDefault, 14 MVideoThumbnail,
15 MVideoWithBlacklistLight 15 MVideoWithBlacklistLight
16} from '@server/types/models' 16} from '@server/types/models'
17import { MVideoImport, MVideoImportFormattable } from '@server/types/models/video/video-import' 17import { MVideoImport, MVideoImportFormattable } from '@server/types/models/video/video-import'
@@ -154,20 +154,16 @@ async function addYoutubeDLImport (req: express.Request, res: express.Response)
154 154
155 const video = buildVideo(res.locals.videoChannel.id, body, youtubeDLInfo) 155 const video = buildVideo(res.locals.videoChannel.id, body, youtubeDLInfo)
156 156
157 let thumbnailModel: MThumbnail
158
159 // Process video thumbnail from request.files 157 // Process video thumbnail from request.files
160 thumbnailModel = await processThumbnail(req, video) 158 let thumbnailModel = await processThumbnail(req, video)
161 159
162 // Process video thumbnail from url if processing from request.files failed 160 // Process video thumbnail from url if processing from request.files failed
163 if (!thumbnailModel && youtubeDLInfo.thumbnailUrl) { 161 if (!thumbnailModel && youtubeDLInfo.thumbnailUrl) {
164 thumbnailModel = await processThumbnailFromUrl(youtubeDLInfo.thumbnailUrl, video) 162 thumbnailModel = await processThumbnailFromUrl(youtubeDLInfo.thumbnailUrl, video)
165 } 163 }
166 164
167 let previewModel: MThumbnail
168
169 // Process video preview from request.files 165 // Process video preview from request.files
170 previewModel = await processPreview(req, video) 166 let previewModel = await processPreview(req, video)
171 167
172 // Process video preview from url if processing from request.files failed 168 // Process video preview from url if processing from request.files failed
173 if (!previewModel && youtubeDLInfo.thumbnailUrl) { 169 if (!previewModel && youtubeDLInfo.thumbnailUrl) {
@@ -199,15 +195,15 @@ async function addYoutubeDLImport (req: express.Request, res: express.Response)
199 for (const subtitle of subtitles) { 195 for (const subtitle of subtitles) {
200 const videoCaption = new VideoCaptionModel({ 196 const videoCaption = new VideoCaptionModel({
201 videoId: video.id, 197 videoId: video.id,
202 language: subtitle.language 198 language: subtitle.language,
203 }) as MVideoCaptionVideo 199 filename: VideoCaptionModel.generateCaptionName(subtitle.language)
204 videoCaption.Video = video 200 }) as MVideoCaption
205 201
206 // Move physical file 202 // Move physical file
207 await moveAndProcessCaptionFile(subtitle, videoCaption) 203 await moveAndProcessCaptionFile(subtitle, videoCaption)
208 204
209 await sequelizeTypescript.transaction(async t => { 205 await sequelizeTypescript.transaction(async t => {
210 await VideoCaptionModel.insertOrReplaceLanguage(video.id, subtitle.language, null, t) 206 await VideoCaptionModel.insertOrReplaceLanguage(videoCaption, t)
211 }) 207 })
212 } 208 }
213 } catch (err) { 209 } catch (err) {
@@ -227,7 +223,7 @@ async function addYoutubeDLImport (req: express.Request, res: express.Response)
227 return res.json(videoImport.toFormattedJSON()).end() 223 return res.json(videoImport.toFormattedJSON()).end()
228} 224}
229 225
230function buildVideo (channelId: number, body: VideoImportCreate, importData: YoutubeDLInfo) { 226function buildVideo (channelId: number, body: VideoImportCreate, importData: YoutubeDLInfo): MVideoThumbnail {
231 const videoData = { 227 const videoData = {
232 name: body.name || importData.name || 'Unknown name', 228 name: body.name || importData.name || 'Unknown name',
233 remote: false, 229 remote: false,
@@ -252,7 +248,7 @@ function buildVideo (channelId: number, body: VideoImportCreate, importData: You
252 return video 248 return video
253} 249}
254 250
255async function processThumbnail (req: express.Request, video: VideoModel) { 251async function processThumbnail (req: express.Request, video: MVideoThumbnail) {
256 const thumbnailField = req.files ? req.files['thumbnailfile'] : undefined 252 const thumbnailField = req.files ? req.files['thumbnailfile'] : undefined
257 if (thumbnailField) { 253 if (thumbnailField) {
258 const thumbnailPhysicalFile = thumbnailField[0] 254 const thumbnailPhysicalFile = thumbnailField[0]
@@ -268,7 +264,7 @@ async function processThumbnail (req: express.Request, video: VideoModel) {
268 return undefined 264 return undefined
269} 265}
270 266
271async function processPreview (req: express.Request, video: VideoModel) { 267async function processPreview (req: express.Request, video: MVideoThumbnail): Promise<MThumbnail> {
272 const previewField = req.files ? req.files['previewfile'] : undefined 268 const previewField = req.files ? req.files['previewfile'] : undefined
273 if (previewField) { 269 if (previewField) {
274 const previewPhysicalFile = previewField[0] 270 const previewPhysicalFile = previewField[0]
@@ -284,7 +280,7 @@ async function processPreview (req: express.Request, video: VideoModel) {
284 return undefined 280 return undefined
285} 281}
286 282
287async function processThumbnailFromUrl (url: string, video: VideoModel) { 283async function processThumbnailFromUrl (url: string, video: MVideoThumbnail) {
288 try { 284 try {
289 return createVideoMiniatureFromUrl(url, video, ThumbnailType.MINIATURE) 285 return createVideoMiniatureFromUrl(url, video, ThumbnailType.MINIATURE)
290 } catch (err) { 286 } catch (err) {
@@ -293,7 +289,7 @@ async function processThumbnailFromUrl (url: string, video: VideoModel) {
293 } 289 }
294} 290}
295 291
296async function processPreviewFromUrl (url: string, video: VideoModel) { 292async function processPreviewFromUrl (url: string, video: MVideoThumbnail) {
297 try { 293 try {
298 return createVideoMiniatureFromUrl(url, video, ThumbnailType.PREVIEW) 294 return createVideoMiniatureFromUrl(url, video, ThumbnailType.PREVIEW)
299 } catch (err) { 295 } catch (err) {
@@ -303,7 +299,7 @@ async function processPreviewFromUrl (url: string, video: VideoModel) {
303} 299}
304 300
305function insertIntoDB (parameters: { 301function insertIntoDB (parameters: {
306 video: MVideoThumbnailAccountDefault 302 video: MVideoThumbnail
307 thumbnailModel: MThumbnail 303 thumbnailModel: MThumbnail
308 previewModel: MThumbnail 304 previewModel: MThumbnail
309 videoChannel: MChannelAccountDefault 305 videoChannel: MChannelAccountDefault
diff --git a/server/controllers/lazy-static.ts b/server/controllers/lazy-static.ts
index 847d24fd4..656dea223 100644
--- a/server/controllers/lazy-static.ts
+++ b/server/controllers/lazy-static.ts
@@ -23,7 +23,7 @@ lazyStaticRouter.use(
23) 23)
24 24
25lazyStaticRouter.use( 25lazyStaticRouter.use(
26 LAZY_STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt', 26 LAZY_STATIC_PATHS.VIDEO_CAPTIONS + ':filename',
27 asyncMiddleware(getVideoCaption) 27 asyncMiddleware(getVideoCaption)
28) 28)
29 29
@@ -78,10 +78,7 @@ async function getPreview (req: express.Request, res: express.Response) {
78} 78}
79 79
80async function getVideoCaption (req: express.Request, res: express.Response) { 80async function getVideoCaption (req: express.Request, res: express.Response) {
81 const result = await VideosCaptionCache.Instance.getFilePath({ 81 const result = await VideosCaptionCache.Instance.getFilePath(req.params.filename)
82 videoId: req.params.videoId,
83 language: req.params.captionLanguage
84 })
85 if (!result) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) 82 if (!result) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
86 83
87 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.SERVER }) 84 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.SERVER })