diff options
Diffstat (limited to 'server/helpers')
-rw-r--r-- | server/helpers/captions-utils.ts | 47 | ||||
-rw-r--r-- | server/helpers/custom-validators/video-captions.ts | 11 |
2 files changed, 51 insertions, 7 deletions
diff --git a/server/helpers/captions-utils.ts b/server/helpers/captions-utils.ts new file mode 100644 index 000000000..8b04f878d --- /dev/null +++ b/server/helpers/captions-utils.ts | |||
@@ -0,0 +1,47 @@ | |||
1 | import { renamePromise, unlinkPromise } from './core-utils' | ||
2 | import { join } from 'path' | ||
3 | import { CONFIG } from '../initializers' | ||
4 | import { VideoCaptionModel } from '../models/video/video-caption' | ||
5 | import * as srt2vtt from 'srt-to-vtt' | ||
6 | import { createReadStream, createWriteStream } from 'fs' | ||
7 | |||
8 | async function moveAndProcessCaptionFile (physicalFile: { filename: string, path: string }, videoCaption: VideoCaptionModel) { | ||
9 | const videoCaptionsDir = CONFIG.STORAGE.CAPTIONS_DIR | ||
10 | const destination = join(videoCaptionsDir, videoCaption.getCaptionName()) | ||
11 | |||
12 | // Convert this srt file to vtt | ||
13 | if (physicalFile.path.endsWith('.srt')) { | ||
14 | await convertSrtToVtt(physicalFile.path, destination) | ||
15 | await unlinkPromise(physicalFile.path) | ||
16 | } else { // Just move the vtt file | ||
17 | await renamePromise(physicalFile.path, destination) | ||
18 | } | ||
19 | |||
20 | // This is important in case if there is another attempt in the retry process | ||
21 | physicalFile.filename = videoCaption.getCaptionName() | ||
22 | physicalFile.path = destination | ||
23 | } | ||
24 | |||
25 | // --------------------------------------------------------------------------- | ||
26 | |||
27 | export { | ||
28 | moveAndProcessCaptionFile | ||
29 | } | ||
30 | |||
31 | // --------------------------------------------------------------------------- | ||
32 | |||
33 | function convertSrtToVtt (source: string, destination: string) { | ||
34 | return new Promise((res, rej) => { | ||
35 | const file = createReadStream(source) | ||
36 | const converter = srt2vtt() | ||
37 | const writer = createWriteStream(destination) | ||
38 | |||
39 | for (const s of [ file, converter, writer ]) { | ||
40 | s.on('error', err => rej(err)) | ||
41 | } | ||
42 | |||
43 | return file.pipe(converter) | ||
44 | .pipe(writer) | ||
45 | .on('finish', () => res()) | ||
46 | }) | ||
47 | } | ||
diff --git a/server/helpers/custom-validators/video-captions.ts b/server/helpers/custom-validators/video-captions.ts index fd4dc740b..6a9c6d75c 100644 --- a/server/helpers/custom-validators/video-captions.ts +++ b/server/helpers/custom-validators/video-captions.ts | |||
@@ -1,4 +1,4 @@ | |||
1 | import { CONSTRAINTS_FIELDS, VIDEO_LANGUAGES } from '../../initializers' | 1 | import { CONSTRAINTS_FIELDS, VIDEO_CAPTIONS_MIMETYPE_EXT, VIDEO_LANGUAGES, VIDEO_MIMETYPE_EXT } from '../../initializers' |
2 | import { exists, isFileValid } from './misc' | 2 | import { exists, isFileValid } from './misc' |
3 | import { Response } from 'express' | 3 | import { Response } from 'express' |
4 | import { VideoModel } from '../../models/video/video' | 4 | import { VideoModel } from '../../models/video/video' |
@@ -8,13 +8,10 @@ function isVideoCaptionLanguageValid (value: any) { | |||
8 | return exists(value) && VIDEO_LANGUAGES[ value ] !== undefined | 8 | return exists(value) && VIDEO_LANGUAGES[ value ] !== undefined |
9 | } | 9 | } |
10 | 10 | ||
11 | const videoCaptionTypes = CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME | 11 | const videoCaptionTypes = Object.keys(VIDEO_CAPTIONS_MIMETYPE_EXT).map(m => `(${m})`) |
12 | .map(v => v.replace('.', '')) | 12 | const videoCaptionTypesRegex = videoCaptionTypes.join('|') |
13 | .join('|') | ||
14 | const videoCaptionsTypesRegex = `text/(${videoCaptionTypes})` | ||
15 | |||
16 | function isVideoCaptionFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) { | 13 | function isVideoCaptionFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) { |
17 | return isFileValid(files, videoCaptionsTypesRegex, field, CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max) | 14 | return isFileValid(files, videoCaptionTypesRegex, field, CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max) |
18 | } | 15 | } |
19 | 16 | ||
20 | async function isVideoCaptionExist (video: VideoModel, language: string, res: Response) { | 17 | async function isVideoCaptionExist (video: VideoModel, language: string, res: Response) { |