1 import { remove } from 'fs-extra'
2 import { join } from 'path'
3 import { OrderItem, Transaction } from 'sequelize'
17 } from 'sequelize-typescript'
18 import { v4 as uuidv4 } from 'uuid'
19 import { MVideo, MVideoCaption, MVideoCaptionFormattable, MVideoCaptionVideo } from '@server/types/models'
20 import { VideoCaption } from '../../../shared/models/videos/caption/video-caption.model'
21 import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions'
22 import { logger } from '../../helpers/logger'
23 import { CONFIG } from '../../initializers/config'
24 import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, VIDEO_LANGUAGES, WEBSERVER } from '../../initializers/constants'
25 import { buildWhereIdOrUUID, throwIfNotValid } from '../utils'
26 import { VideoModel } from './video'
28 export enum ScopeNames {
29 WITH_VIDEO_UUID_AND_REMOTE = 'WITH_VIDEO_UUID_AND_REMOTE'
33 [ScopeNames.WITH_VIDEO_UUID_AND_REMOTE]: {
36 attributes: [ 'id', 'uuid', 'remote' ],
37 model: VideoModel.unscoped(),
45 tableName: 'videoCaption',
48 fields: [ 'filename' ],
55 fields: [ 'videoId', 'language' ],
60 export class VideoCaptionModel extends Model {
68 @Is('VideoCaptionLanguage', value => throwIfNotValid(value, isVideoCaptionLanguageValid, 'language'))
77 @Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
80 @ForeignKey(() => VideoModel)
84 @BelongsTo(() => VideoModel, {
93 static async removeFiles (instance: VideoCaptionModel) {
94 if (!instance.Video) {
95 instance.Video = await instance.$get('Video')
98 if (instance.isOwned()) {
99 logger.info('Removing caption %s.', instance.filename)
102 await instance.removeCaptionFile()
104 logger.error('Cannot remove caption file %s.', instance.filename)
111 static loadByVideoIdAndLanguage (videoId: string | number, language: string): Promise<MVideoCaptionVideo> {
112 const videoInclude = {
113 model: VideoModel.unscoped(),
114 attributes: [ 'id', 'remote', 'uuid' ],
115 where: buildWhereIdOrUUID(videoId)
127 return VideoCaptionModel.findOne(query)
130 static loadWithVideoByFilename (filename: string): Promise<MVideoCaptionVideo> {
137 model: VideoModel.unscoped(),
138 attributes: [ 'id', 'remote', 'uuid' ]
143 return VideoCaptionModel.findOne(query)
146 static async insertOrReplaceLanguage (caption: MVideoCaption, transaction: Transaction) {
147 const existing = await VideoCaptionModel.loadByVideoIdAndLanguage(caption.videoId, caption.language)
148 // Delete existing file
149 if (existing) await existing.destroy({ transaction })
151 return caption.save({ transaction })
154 static listVideoCaptions (videoId: number): Promise<MVideoCaptionVideo[]> {
156 order: [ [ 'language', 'ASC' ] ] as OrderItem[],
162 return VideoCaptionModel.scope(ScopeNames.WITH_VIDEO_UUID_AND_REMOTE).findAll(query)
165 static getLanguageLabel (language: string) {
166 return VIDEO_LANGUAGES[language] || 'Unknown'
169 static deleteAllCaptionsOfRemoteVideo (videoId: number, transaction: Transaction) {
177 return VideoCaptionModel.destroy(query)
180 static generateCaptionName (language: string) {
181 return `${uuidv4()}-${language}.vtt`
185 return this.Video.remote === false
188 toFormattedJSON (this: MVideoCaptionFormattable): VideoCaption {
192 label: VideoCaptionModel.getLanguageLabel(this.language)
194 captionPath: this.getCaptionStaticPath()
198 getCaptionStaticPath (this: MVideoCaption) {
199 return join(LAZY_STATIC_PATHS.VIDEO_CAPTIONS, this.filename)
202 removeCaptionFile (this: MVideoCaption) {
203 return remove(CONFIG.STORAGE.CAPTIONS_DIR + this.filename)
206 getFileUrl (video: MVideo) {
207 if (!this.Video) this.Video = video as VideoModel
209 if (video.isOwned()) return WEBSERVER.URL + this.getCaptionStaticPath()