1 import { OrderItem, Transaction } from 'sequelize'
15 } from 'sequelize-typescript'
16 import { buildWhereIdOrUUID, throwIfNotValid } from '../utils'
17 import { VideoModel } from './video'
18 import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions'
19 import { VideoCaption } from '../../../shared/models/videos/caption/video-caption.model'
20 import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, VIDEO_LANGUAGES, WEBSERVER } from '../../initializers/constants'
21 import { join } from 'path'
22 import { logger } from '../../helpers/logger'
23 import { remove } from 'fs-extra'
24 import { CONFIG } from '../../initializers/config'
25 import * as Bluebird from 'bluebird'
26 import { MVideoAccountLight, MVideoCaptionFormattable, MVideoCaptionVideo } from '@server/typings/models'
27 import { buildRemoteVideoBaseUrl } from '@server/helpers/activitypub'
29 export enum ScopeNames {
30 WITH_VIDEO_UUID_AND_REMOTE = 'WITH_VIDEO_UUID_AND_REMOTE'
34 [ScopeNames.WITH_VIDEO_UUID_AND_REMOTE]: {
37 attributes: [ 'id', 'uuid', 'remote' ],
38 model: VideoModel.unscoped(),
46 tableName: 'videoCaption',
52 fields: [ 'videoId', 'language' ],
57 export class VideoCaptionModel extends Model<VideoCaptionModel> {
65 @Is('VideoCaptionLanguage', value => throwIfNotValid(value, isVideoCaptionLanguageValid, 'language'))
70 @Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
73 @ForeignKey(() => VideoModel)
77 @BelongsTo(() => VideoModel, {
86 static async removeFiles (instance: VideoCaptionModel) {
87 if (!instance.Video) {
88 instance.Video = await instance.$get('Video')
91 if (instance.isOwned()) {
92 logger.info('Removing captions %s of video %s.', instance.Video.uuid, instance.language)
95 await instance.removeCaptionFile()
97 logger.error('Cannot remove caption file of video %s.', instance.Video.uuid)
104 static loadByVideoIdAndLanguage (videoId: string | number, language: string): Bluebird<MVideoCaptionVideo> {
105 const videoInclude = {
106 model: VideoModel.unscoped(),
107 attributes: [ 'id', 'remote', 'uuid' ],
108 where: buildWhereIdOrUUID(videoId)
120 return VideoCaptionModel.findOne(query)
123 static insertOrReplaceLanguage (videoId: number, language: string, fileUrl: string, transaction: Transaction) {
130 return VideoCaptionModel.upsert(values, { transaction, returning: true })
131 .then(([ caption ]) => caption)
134 static listVideoCaptions (videoId: number): Bluebird<MVideoCaptionVideo[]> {
136 order: [ [ 'language', 'ASC' ] ] as OrderItem[],
142 return VideoCaptionModel.scope(ScopeNames.WITH_VIDEO_UUID_AND_REMOTE).findAll(query)
145 static getLanguageLabel (language: string) {
146 return VIDEO_LANGUAGES[language] || 'Unknown'
149 static deleteAllCaptionsOfRemoteVideo (videoId: number, transaction: Transaction) {
157 return VideoCaptionModel.destroy(query)
161 return this.Video.remote === false
164 toFormattedJSON (this: MVideoCaptionFormattable): VideoCaption {
168 label: VideoCaptionModel.getLanguageLabel(this.language)
170 captionPath: this.getCaptionStaticPath()
174 getCaptionStaticPath (this: MVideoCaptionFormattable) {
175 return join(LAZY_STATIC_PATHS.VIDEO_CAPTIONS, this.getCaptionName())
178 getCaptionName (this: MVideoCaptionFormattable) {
179 return `${this.Video.uuid}-${this.language}.vtt`
182 removeCaptionFile (this: MVideoCaptionFormattable) {
183 return remove(CONFIG.STORAGE.CAPTIONS_DIR + this.getCaptionName())
186 getFileUrl (video: MVideoAccountLight) {
187 if (!this.Video) this.Video = video as VideoModel
189 if (video.isOwned()) return WEBSERVER.URL + this.getCaptionStaticPath()
190 if (this.fileUrl) return this.fileUrl
192 // Fallback if we don't have a file URL
193 return buildRemoteVideoBaseUrl(video, this.getCaptionStaticPath())