1 import { remove } from 'fs-extra'
2 import { join } from 'path'
3 import { OrderItem, Transaction } from 'sequelize'
17 } from 'sequelize-typescript'
18 import { buildRemoteVideoBaseUrl } from '@server/helpers/activitypub'
19 import { MVideoAccountLight, 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',
51 fields: [ 'videoId', 'language' ],
56 export class VideoCaptionModel extends Model {
64 @Is('VideoCaptionLanguage', value => throwIfNotValid(value, isVideoCaptionLanguageValid, 'language'))
69 @Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
72 @ForeignKey(() => VideoModel)
76 @BelongsTo(() => VideoModel, {
85 static async removeFiles (instance: VideoCaptionModel) {
86 if (!instance.Video) {
87 instance.Video = await instance.$get('Video')
90 if (instance.isOwned()) {
91 logger.info('Removing captions %s of video %s.', instance.Video.uuid, instance.language)
94 await instance.removeCaptionFile()
96 logger.error('Cannot remove caption file of video %s.', instance.Video.uuid)
103 static loadByVideoIdAndLanguage (videoId: string | number, language: string): Promise<MVideoCaptionVideo> {
104 const videoInclude = {
105 model: VideoModel.unscoped(),
106 attributes: [ 'id', 'remote', 'uuid' ],
107 where: buildWhereIdOrUUID(videoId)
119 return VideoCaptionModel.findOne(query)
122 static insertOrReplaceLanguage (videoId: number, language: string, fileUrl: string, transaction: Transaction) {
129 return VideoCaptionModel.upsert(values, { transaction, returning: true })
130 .then(([ caption ]) => caption)
133 static listVideoCaptions (videoId: number): Promise<MVideoCaptionVideo[]> {
135 order: [ [ 'language', 'ASC' ] ] as OrderItem[],
141 return VideoCaptionModel.scope(ScopeNames.WITH_VIDEO_UUID_AND_REMOTE).findAll(query)
144 static getLanguageLabel (language: string) {
145 return VIDEO_LANGUAGES[language] || 'Unknown'
148 static deleteAllCaptionsOfRemoteVideo (videoId: number, transaction: Transaction) {
156 return VideoCaptionModel.destroy(query)
160 return this.Video.remote === false
163 toFormattedJSON (this: MVideoCaptionFormattable): VideoCaption {
167 label: VideoCaptionModel.getLanguageLabel(this.language)
169 captionPath: this.getCaptionStaticPath()
173 getCaptionStaticPath (this: MVideoCaptionFormattable) {
174 return join(LAZY_STATIC_PATHS.VIDEO_CAPTIONS, this.getCaptionName())
177 getCaptionName (this: MVideoCaptionFormattable) {
178 return `${this.Video.uuid}-${this.language}.vtt`
181 removeCaptionFile (this: MVideoCaptionFormattable) {
182 return remove(CONFIG.STORAGE.CAPTIONS_DIR + this.getCaptionName())
185 getFileUrl (video: MVideoAccountLight) {
186 if (!this.Video) this.Video = video as VideoModel
188 if (video.isOwned()) return WEBSERVER.URL + this.getCaptionStaticPath()
189 if (this.fileUrl) return this.fileUrl
191 // Fallback if we don't have a file URL
192 return buildRemoteVideoBaseUrl(video, this.getCaptionStaticPath())