PeerTube/server/models/video/video-caption.ts

196 lines
4.8 KiB
TypeScript
Raw Normal View History

2019-04-18 11:28:17 +02:00
import { OrderItem, Transaction } from 'sequelize'
2018-07-12 19:02:00 +02:00
import {
AllowNull,
BeforeDestroy,
BelongsTo,
Column,
2020-01-31 16:56:52 +01:00
CreatedAt,
DataType,
2018-07-12 19:02:00 +02:00
ForeignKey,
Is,
Model,
Scopes,
Table,
UpdatedAt
} from 'sequelize-typescript'
2019-04-23 09:50:57 +02:00
import { buildWhereIdOrUUID, throwIfNotValid } from '../utils'
2018-07-12 19:02:00 +02:00
import { VideoModel } from './video'
import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions'
2018-08-14 14:59:53 +02:00
import { VideoCaption } from '../../../shared/models/videos/caption/video-caption.model'
2020-01-31 16:56:52 +01:00
import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, VIDEO_LANGUAGES, WEBSERVER } from '../../initializers/constants'
2018-07-12 19:02:00 +02:00
import { join } from 'path'
import { logger } from '../../helpers/logger'
2018-08-27 16:23:34 +02:00
import { remove } from 'fs-extra'
2019-04-11 11:33:44 +02:00
import { CONFIG } from '../../initializers/config'
2019-08-15 11:53:26 +02:00
import * as Bluebird from 'bluebird'
2020-06-18 10:45:25 +02:00
import { MVideoAccountLight, MVideoCaptionFormattable, MVideoCaptionVideo } from '@server/types/models'
import { buildRemoteVideoBaseUrl } from '@server/helpers/activitypub'
2018-07-12 19:02:00 +02:00
export enum ScopeNames {
WITH_VIDEO_UUID_AND_REMOTE = 'WITH_VIDEO_UUID_AND_REMOTE'
}
2019-04-23 09:50:57 +02:00
@Scopes(() => ({
2018-07-12 19:02:00 +02:00
[ScopeNames.WITH_VIDEO_UUID_AND_REMOTE]: {
include: [
{
2019-08-15 11:53:26 +02:00
attributes: [ 'id', 'uuid', 'remote' ],
2019-04-23 09:50:57 +02:00
model: VideoModel.unscoped(),
2018-07-12 19:02:00 +02:00
required: true
}
]
}
2019-04-23 09:50:57 +02:00
}))
2018-07-12 19:02:00 +02: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
@AllowNull(true)
@Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
fileUrl: string
2018-07-12 19:02:00 +02:00
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
Video: VideoModel
@BeforeDestroy
static async removeFiles (instance: VideoCaptionModel) {
2018-07-16 14:22:16 +02:00
if (!instance.Video) {
instance.Video = await instance.$get('Video')
2018-07-16 14:22:16 +02:00
}
2018-07-12 19:02:00 +02:00
if (instance.isOwned()) {
2018-07-30 17:02:40 +02:00
logger.info('Removing captions %s of video %s.', instance.Video.uuid, instance.language)
2018-07-16 14:22:16 +02:00
try {
await instance.removeCaptionFile()
} catch (err) {
logger.error('Cannot remove caption file of video %s.', instance.Video.uuid)
}
2018-07-12 19:02:00 +02:00
}
return undefined
}
2019-08-15 11:53:26 +02:00
static loadByVideoIdAndLanguage (videoId: string | number, language: string): Bluebird<MVideoCaptionVideo> {
2018-07-12 19:02:00 +02:00
const videoInclude = {
model: VideoModel.unscoped(),
attributes: [ 'id', 'remote', 'uuid' ],
2019-04-23 09:50:57 +02:00
where: buildWhereIdOrUUID(videoId)
2018-07-12 19:02:00 +02:00
}
const query = {
where: {
language
},
include: [
videoInclude
]
}
return VideoCaptionModel.findOne(query)
}
static insertOrReplaceLanguage (videoId: number, language: string, fileUrl: string, transaction: Transaction) {
2018-07-12 19:02:00 +02:00
const values = {
videoId,
language,
fileUrl
2018-07-12 19:02:00 +02:00
}
2020-01-28 14:45:17 +01:00
return VideoCaptionModel.upsert(values, { transaction, returning: true })
.then(([ caption ]) => caption)
2018-07-12 19:02:00 +02:00
}
2019-08-15 11:53:26 +02:00
static listVideoCaptions (videoId: number): Bluebird<MVideoCaptionVideo[]> {
2018-07-12 19:02:00 +02:00
const query = {
2019-04-18 11:28:17 +02:00
order: [ [ 'language', 'ASC' ] ] as OrderItem[],
2018-07-12 19:02:00 +02: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 11:28:17 +02:00
static deleteAllCaptionsOfRemoteVideo (videoId: number, transaction: Transaction) {
2018-07-12 19:02:00 +02:00
const query = {
where: {
videoId
},
transaction
}
return VideoCaptionModel.destroy(query)
}
isOwned () {
return this.Video.remote === false
}
2019-08-20 19:05:31 +02:00
toFormattedJSON (this: MVideoCaptionFormattable): VideoCaption {
2018-07-12 19:02:00 +02:00
return {
language: {
id: this.language,
label: VideoCaptionModel.getLanguageLabel(this.language)
},
captionPath: this.getCaptionStaticPath()
}
}
2019-08-20 19:05:31 +02:00
getCaptionStaticPath (this: MVideoCaptionFormattable) {
2019-08-09 11:32:40 +02:00
return join(LAZY_STATIC_PATHS.VIDEO_CAPTIONS, this.getCaptionName())
2018-07-12 19:02:00 +02:00
}
2019-08-20 19:05:31 +02:00
getCaptionName (this: MVideoCaptionFormattable) {
2018-07-12 19:02:00 +02:00
return `${this.Video.uuid}-${this.language}.vtt`
}
2019-08-20 19:05:31 +02:00
removeCaptionFile (this: MVideoCaptionFormattable) {
2018-08-27 16:23:34 +02:00
return remove(CONFIG.STORAGE.CAPTIONS_DIR + this.getCaptionName())
2018-07-12 19:02:00 +02:00
}
getFileUrl (video: MVideoAccountLight) {
if (!this.Video) this.Video = video as VideoModel
if (video.isOwned()) return WEBSERVER.URL + this.getCaptionStaticPath()
if (this.fileUrl) return this.fileUrl
// Fallback if we don't have a file URL
return buildRemoteVideoBaseUrl(video, this.getCaptionStaticPath())
}
2018-07-12 19:02:00 +02:00
}