1 import { OrderItem, Transaction } from 'sequelize'
14 } from 'sequelize-typescript'
15 import { buildWhereIdOrUUID, throwIfNotValid } from '../utils'
16 import { VideoModel } from './video'
17 import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions'
18 import { VideoCaption } from '../../../shared/models/videos/caption/video-caption.model'
19 import { LAZY_STATIC_PATHS, VIDEO_LANGUAGES } from '../../initializers/constants'
20 import { join } from 'path'
21 import { logger } from '../../helpers/logger'
22 import { remove } from 'fs-extra'
23 import { CONFIG } from '../../initializers/config'
24 import * as Bluebird from 'bluebird'
25 import { MVideoCaptionFormattable, MVideoCaptionVideo } from '@server/typings/models'
27 export enum ScopeNames {
28 WITH_VIDEO_UUID_AND_REMOTE = 'WITH_VIDEO_UUID_AND_REMOTE'
32 [ScopeNames.WITH_VIDEO_UUID_AND_REMOTE]: {
35 attributes: [ 'id', 'uuid', 'remote' ],
36 model: VideoModel.unscoped(),
44 tableName: 'videoCaption',
50 fields: [ 'videoId', 'language' ],
55 export class VideoCaptionModel extends Model<VideoCaptionModel> {
63 @Is('VideoCaptionLanguage', value => throwIfNotValid(value, isVideoCaptionLanguageValid, 'language'))
67 @ForeignKey(() => VideoModel)
71 @BelongsTo(() => VideoModel, {
80 static async removeFiles (instance: VideoCaptionModel) {
81 if (!instance.Video) {
82 instance.Video = await instance.$get('Video') as VideoModel
85 if (instance.isOwned()) {
86 logger.info('Removing captions %s of video %s.', instance.Video.uuid, instance.language)
89 await instance.removeCaptionFile()
91 logger.error('Cannot remove caption file of video %s.', instance.Video.uuid)
98 static loadByVideoIdAndLanguage (videoId: string | number, language: string): Bluebird<MVideoCaptionVideo> {
99 const videoInclude = {
100 model: VideoModel.unscoped(),
101 attributes: [ 'id', 'remote', 'uuid' ],
102 where: buildWhereIdOrUUID(videoId)
114 return VideoCaptionModel.findOne(query)
117 static insertOrReplaceLanguage (videoId: number, language: string, transaction: Transaction) {
123 return (VideoCaptionModel.upsert<VideoCaptionModel>(values, { transaction, returning: true }) as any) // FIXME: typings
124 .then(([ caption ]) => caption)
127 static listVideoCaptions (videoId: number): Bluebird<MVideoCaptionVideo[]> {
129 order: [ [ 'language', 'ASC' ] ] as OrderItem[],
135 return VideoCaptionModel.scope(ScopeNames.WITH_VIDEO_UUID_AND_REMOTE).findAll(query)
138 static getLanguageLabel (language: string) {
139 return VIDEO_LANGUAGES[language] || 'Unknown'
142 static deleteAllCaptionsOfRemoteVideo (videoId: number, transaction: Transaction) {
150 return VideoCaptionModel.destroy(query)
154 return this.Video.remote === false
157 toFormattedJSON (this: MVideoCaptionFormattable): VideoCaption {
161 label: VideoCaptionModel.getLanguageLabel(this.language)
163 captionPath: this.getCaptionStaticPath()
167 getCaptionStaticPath (this: MVideoCaptionFormattable) {
168 return join(LAZY_STATIC_PATHS.VIDEO_CAPTIONS, this.getCaptionName())
171 getCaptionName (this: MVideoCaptionFormattable) {
172 return `${this.Video.uuid}-${this.language}.vtt`
175 removeCaptionFile (this: MVideoCaptionFormattable) {
176 return remove(CONFIG.STORAGE.CAPTIONS_DIR + this.getCaptionName())