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 { AttributesOnly } from '@shared/core-utils'
21 import { VideoCaption } from '../../../shared/models/videos/caption/video-caption.model'
22 import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions'
23 import { logger } from '../../helpers/logger'
24 import { CONFIG } from '../../initializers/config'
25 import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, VIDEO_LANGUAGES, WEBSERVER } from '../../initializers/constants'
26 import { buildWhereIdOrUUID, throwIfNotValid } from '../utils'
27 import { VideoModel } from './video'
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',
49 fields: [ 'filename' ],
56 fields: [ 'videoId', 'language' ],
61 export class VideoCaptionModel extends Model<Partial<AttributesOnly<VideoCaptionModel>>> {
69 @Is('VideoCaptionLanguage', value => throwIfNotValid(value, isVideoCaptionLanguageValid, 'language'))
78 @Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
81 @ForeignKey(() => VideoModel)
85 @BelongsTo(() => VideoModel, {
94 static async removeFiles (instance: VideoCaptionModel) {
95 if (!instance.Video) {
96 instance.Video = await instance.$get('Video')
99 if (instance.isOwned()) {
100 logger.info('Removing caption %s.', instance.filename)
103 await instance.removeCaptionFile()
105 logger.error('Cannot remove caption file %s.', instance.filename)
112 static loadByVideoIdAndLanguage (videoId: string | number, language: string): Promise<MVideoCaptionVideo> {
113 const videoInclude = {
114 model: VideoModel.unscoped(),
115 attributes: [ 'id', 'remote', 'uuid' ],
116 where: buildWhereIdOrUUID(videoId)
128 return VideoCaptionModel.findOne(query)
131 static loadWithVideoByFilename (filename: string): Promise<MVideoCaptionVideo> {
138 model: VideoModel.unscoped(),
139 attributes: [ 'id', 'remote', 'uuid' ]
144 return VideoCaptionModel.findOne(query)
147 static async insertOrReplaceLanguage (caption: MVideoCaption, transaction: Transaction) {
148 const existing = await VideoCaptionModel.loadByVideoIdAndLanguage(caption.videoId, caption.language)
149 // Delete existing file
150 if (existing) await existing.destroy({ transaction })
152 return caption.save({ transaction })
155 static listVideoCaptions (videoId: number): Promise<MVideoCaptionVideo[]> {
157 order: [ [ 'language', 'ASC' ] ] as OrderItem[],
163 return VideoCaptionModel.scope(ScopeNames.WITH_VIDEO_UUID_AND_REMOTE).findAll(query)
166 static getLanguageLabel (language: string) {
167 return VIDEO_LANGUAGES[language] || 'Unknown'
170 static deleteAllCaptionsOfRemoteVideo (videoId: number, transaction: Transaction) {
178 return VideoCaptionModel.destroy(query)
181 static generateCaptionName (language: string) {
182 return `${uuidv4()}-${language}.vtt`
186 return this.Video.remote === false
189 toFormattedJSON (this: MVideoCaptionFormattable): VideoCaption {
193 label: VideoCaptionModel.getLanguageLabel(this.language)
195 captionPath: this.getCaptionStaticPath()
199 getCaptionStaticPath (this: MVideoCaption) {
200 return join(LAZY_STATIC_PATHS.VIDEO_CAPTIONS, this.filename)
203 removeCaptionFile (this: MVideoCaption) {
204 return remove(CONFIG.STORAGE.CAPTIONS_DIR + this.filename)
207 getFileUrl (video: MVideo) {
208 if (!this.Video) this.Video = video as VideoModel
210 if (video.isOwned()) return WEBSERVER.URL + this.getCaptionStaticPath()