1 import { remove } from 'fs-extra'
2 import { join } from 'path'
3 import { OrderItem, Transaction } from 'sequelize'
17 } from 'sequelize-typescript'
18 import { MVideo, MVideoCaption, MVideoCaptionFormattable, MVideoCaptionVideo } from '@server/types/models'
19 import { buildUUID } from '@shared/core-utils'
20 import { AttributesOnly } from '@shared/typescript-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, options) {
95 if (!instance.Video) {
96 instance.Video = await instance.$get('Video', { transaction: options.transaction })
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, transaction?: Transaction): Promise<MVideoCaptionVideo> {
113 const videoInclude = {
114 model: VideoModel.unscoped(),
115 attributes: [ 'id', 'remote', 'uuid' ],
116 where: buildWhereIdOrUUID(videoId)
129 return VideoCaptionModel.findOne(query)
132 static loadWithVideoByFilename (filename: string): Promise<MVideoCaptionVideo> {
139 model: VideoModel.unscoped(),
140 attributes: [ 'id', 'remote', 'uuid' ]
145 return VideoCaptionModel.findOne(query)
148 static async insertOrReplaceLanguage (caption: MVideoCaption, transaction: Transaction) {
149 const existing = await VideoCaptionModel.loadByVideoIdAndLanguage(caption.videoId, caption.language, transaction)
151 // Delete existing file
152 if (existing) await existing.destroy({ transaction })
154 return caption.save({ transaction })
157 static listVideoCaptions (videoId: number, transaction?: Transaction): Promise<MVideoCaptionVideo[]> {
159 order: [ [ 'language', 'ASC' ] ] as OrderItem[],
166 return VideoCaptionModel.scope(ScopeNames.WITH_VIDEO_UUID_AND_REMOTE).findAll(query)
169 static getLanguageLabel (language: string) {
170 return VIDEO_LANGUAGES[language] || 'Unknown'
173 static deleteAllCaptionsOfRemoteVideo (videoId: number, transaction: Transaction) {
181 return VideoCaptionModel.destroy(query)
184 static generateCaptionName (language: string) {
185 return `${buildUUID()}-${language}.vtt`
189 return this.Video.remote === false
192 toFormattedJSON (this: MVideoCaptionFormattable): VideoCaption {
196 label: VideoCaptionModel.getLanguageLabel(this.language)
198 captionPath: this.getCaptionStaticPath()
202 getCaptionStaticPath (this: MVideoCaption) {
203 return join(LAZY_STATIC_PATHS.VIDEO_CAPTIONS, this.filename)
206 removeCaptionFile (this: MVideoCaption) {
207 return remove(CONFIG.STORAGE.CAPTIONS_DIR + this.filename)
210 getFileUrl (video: MVideo) {
211 if (!this.Video) this.Video = video as VideoModel
213 if (video.isOwned()) return WEBSERVER.URL + this.getCaptionStaticPath()
218 isEqual (this: MVideoCaption, other: MVideoCaption) {
219 if (this.fileUrl) return this.fileUrl === other.fileUrl
221 return this.filename === other.filename