2019-04-18 05:28:17 -04:00
|
|
|
import { OrderItem, Transaction } from 'sequelize'
|
2018-07-12 13:02:00 -04:00
|
|
|
import {
|
|
|
|
AllowNull,
|
|
|
|
BeforeDestroy,
|
|
|
|
BelongsTo,
|
|
|
|
Column,
|
|
|
|
CreatedAt,
|
|
|
|
ForeignKey,
|
|
|
|
Is,
|
|
|
|
Model,
|
|
|
|
Scopes,
|
|
|
|
Table,
|
|
|
|
UpdatedAt
|
|
|
|
} from 'sequelize-typescript'
|
2019-04-23 03:50:57 -04:00
|
|
|
import { buildWhereIdOrUUID, throwIfNotValid } from '../utils'
|
2018-07-12 13:02:00 -04:00
|
|
|
import { VideoModel } from './video'
|
|
|
|
import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions'
|
2018-08-14 08:59:53 -04:00
|
|
|
import { VideoCaption } from '../../../shared/models/videos/caption/video-caption.model'
|
2019-04-11 08:26:41 -04:00
|
|
|
import { STATIC_PATHS, VIDEO_LANGUAGES } from '../../initializers/constants'
|
2018-07-12 13:02:00 -04:00
|
|
|
import { join } from 'path'
|
|
|
|
import { logger } from '../../helpers/logger'
|
2018-08-27 10:23:34 -04:00
|
|
|
import { remove } from 'fs-extra'
|
2019-04-11 05:33:44 -04:00
|
|
|
import { CONFIG } from '../../initializers/config'
|
2018-07-12 13:02:00 -04:00
|
|
|
|
|
|
|
export enum ScopeNames {
|
|
|
|
WITH_VIDEO_UUID_AND_REMOTE = 'WITH_VIDEO_UUID_AND_REMOTE'
|
|
|
|
}
|
|
|
|
|
2019-04-23 03:50:57 -04:00
|
|
|
@Scopes(() => ({
|
2018-07-12 13:02:00 -04:00
|
|
|
[ScopeNames.WITH_VIDEO_UUID_AND_REMOTE]: {
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
attributes: [ 'uuid', 'remote' ],
|
2019-04-23 03:50:57 -04:00
|
|
|
model: VideoModel.unscoped(),
|
2018-07-12 13:02:00 -04:00
|
|
|
required: true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2019-04-23 03:50:57 -04:00
|
|
|
}))
|
2018-07-12 13:02:00 -04:00
|
|
|
|
|
|
|
@Table({
|
|
|
|
tableName: 'videoCaption',
|
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
fields: [ 'videoId' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'videoId', 'language' ],
|
|
|
|
unique: true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
|
|
|
export class VideoCaptionModel extends Model<VideoCaptionModel> {
|
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
|
|
|
|
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Is('VideoCaptionLanguage', value => throwIfNotValid(value, isVideoCaptionLanguageValid, 'language'))
|
|
|
|
@Column
|
|
|
|
language: string
|
|
|
|
|
|
|
|
@ForeignKey(() => VideoModel)
|
|
|
|
@Column
|
|
|
|
videoId: number
|
|
|
|
|
|
|
|
@BelongsTo(() => VideoModel, {
|
|
|
|
foreignKey: {
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'CASCADE'
|
|
|
|
})
|
|
|
|
Video: VideoModel
|
|
|
|
|
|
|
|
@BeforeDestroy
|
|
|
|
static async removeFiles (instance: VideoCaptionModel) {
|
2018-07-16 08:22:16 -04:00
|
|
|
if (!instance.Video) {
|
|
|
|
instance.Video = await instance.$get('Video') as VideoModel
|
|
|
|
}
|
2018-07-12 13:02:00 -04:00
|
|
|
|
|
|
|
if (instance.isOwned()) {
|
2018-07-30 11:02:40 -04:00
|
|
|
logger.info('Removing captions %s of video %s.', instance.Video.uuid, instance.language)
|
2018-07-16 08:22:16 -04:00
|
|
|
|
|
|
|
try {
|
|
|
|
await instance.removeCaptionFile()
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot remove caption file of video %s.', instance.Video.uuid)
|
|
|
|
}
|
2018-07-12 13:02:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
static loadByVideoIdAndLanguage (videoId: string | number, language: string) {
|
|
|
|
const videoInclude = {
|
|
|
|
model: VideoModel.unscoped(),
|
|
|
|
attributes: [ 'id', 'remote', 'uuid' ],
|
2019-04-23 03:50:57 -04:00
|
|
|
where: buildWhereIdOrUUID(videoId)
|
2018-07-12 13:02:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
language
|
|
|
|
},
|
|
|
|
include: [
|
|
|
|
videoInclude
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
return VideoCaptionModel.findOne(query)
|
|
|
|
}
|
|
|
|
|
2019-04-18 05:28:17 -04:00
|
|
|
static insertOrReplaceLanguage (videoId: number, language: string, transaction: Transaction) {
|
2018-07-12 13:02:00 -04:00
|
|
|
const values = {
|
|
|
|
videoId,
|
|
|
|
language
|
|
|
|
}
|
|
|
|
|
2019-04-18 05:28:17 -04:00
|
|
|
return (VideoCaptionModel.upsert<VideoCaptionModel>(values, { transaction, returning: true }) as any) // FIXME: typings
|
2018-10-01 09:34:31 -04:00
|
|
|
.then(([ caption ]) => caption)
|
2018-07-12 13:02:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static listVideoCaptions (videoId: number) {
|
|
|
|
const query = {
|
2019-04-18 05:28:17 -04:00
|
|
|
order: [ [ 'language', 'ASC' ] ] as OrderItem[],
|
2018-07-12 13:02:00 -04:00
|
|
|
where: {
|
|
|
|
videoId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return VideoCaptionModel.scope(ScopeNames.WITH_VIDEO_UUID_AND_REMOTE).findAll(query)
|
|
|
|
}
|
|
|
|
|
|
|
|
static getLanguageLabel (language: string) {
|
|
|
|
return VIDEO_LANGUAGES[language] || 'Unknown'
|
|
|
|
}
|
|
|
|
|
2019-04-18 05:28:17 -04:00
|
|
|
static deleteAllCaptionsOfRemoteVideo (videoId: number, transaction: Transaction) {
|
2018-07-12 13:02:00 -04:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
videoId
|
|
|
|
},
|
|
|
|
transaction
|
|
|
|
}
|
|
|
|
|
|
|
|
return VideoCaptionModel.destroy(query)
|
|
|
|
}
|
|
|
|
|
|
|
|
isOwned () {
|
|
|
|
return this.Video.remote === false
|
|
|
|
}
|
|
|
|
|
|
|
|
toFormattedJSON (): VideoCaption {
|
|
|
|
return {
|
|
|
|
language: {
|
|
|
|
id: this.language,
|
|
|
|
label: VideoCaptionModel.getLanguageLabel(this.language)
|
|
|
|
},
|
|
|
|
captionPath: this.getCaptionStaticPath()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getCaptionStaticPath () {
|
|
|
|
return join(STATIC_PATHS.VIDEO_CAPTIONS, this.getCaptionName())
|
|
|
|
}
|
|
|
|
|
|
|
|
getCaptionName () {
|
|
|
|
return `${this.Video.uuid}-${this.language}.vtt`
|
|
|
|
}
|
|
|
|
|
|
|
|
removeCaptionFile () {
|
2018-08-27 10:23:34 -04:00
|
|
|
return remove(CONFIG.STORAGE.CAPTIONS_DIR + this.getCaptionName())
|
2018-07-12 13:02:00 -04:00
|
|
|
}
|
|
|
|
}
|