]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/captions-utils.ts
Prevent video import on non unicast ips
[github/Chocobozzz/PeerTube.git] / server / helpers / captions-utils.ts
CommitLineData
6302d599 1import { createReadStream, createWriteStream, move, remove } from 'fs-extra'
f4001cf4 2import { join } from 'path'
41fb13c3
C
3import srt2vtt from 'srt-to-vtt'
4import { Transform } from 'stream'
6302d599
C
5import { MVideoCaption } from '@server/types/models'
6import { CONFIG } from '../initializers/config'
c9ae74d6 7import { pipelinePromise } from './core-utils'
f4001cf4 8
6302d599 9async function moveAndProcessCaptionFile (physicalFile: { filename: string, path: string }, videoCaption: MVideoCaption) {
f4001cf4 10 const videoCaptionsDir = CONFIG.STORAGE.CAPTIONS_DIR
6302d599 11 const destination = join(videoCaptionsDir, videoCaption.filename)
f4001cf4
C
12
13 // Convert this srt file to vtt
14 if (physicalFile.path.endsWith('.srt')) {
15 await convertSrtToVtt(physicalFile.path, destination)
62689b94 16 await remove(physicalFile.path)
3f6b7aa1 17 } else if (physicalFile.path !== destination) { // Just move the vtt file
44848a51 18 await move(physicalFile.path, destination, { overwrite: true })
f4001cf4
C
19 }
20
21 // This is important in case if there is another attempt in the retry process
6302d599 22 physicalFile.filename = videoCaption.filename
f4001cf4
C
23 physicalFile.path = destination
24}
25
26// ---------------------------------------------------------------------------
27
28export {
29 moveAndProcessCaptionFile
30}
31
32// ---------------------------------------------------------------------------
33
34function convertSrtToVtt (source: string, destination: string) {
c9ae74d6
C
35 const fixVTT = new Transform({
36 transform: (chunk, _encoding, cb) => {
37 let block: string = chunk.toString()
f4001cf4 38
c9ae74d6
C
39 block = block.replace(/(\d\d:\d\d:\d\d)(\s)/g, '$1.000$2')
40 .replace(/(\d\d:\d\d:\d\d),(\d)(\s)/g, '$1.00$2$3')
41 .replace(/(\d\d:\d\d:\d\d),(\d\d)(\s)/g, '$1.0$2$3')
f4001cf4 42
c9ae74d6
C
43 return cb(undefined, block)
44 }
f4001cf4 45 })
c9ae74d6
C
46
47 return pipelinePromise(
48 createReadStream(source),
49 srt2vtt(),
50 fixVTT,
51 createWriteStream(destination)
52 )
f4001cf4 53}