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

214 lines
4.9 KiB
TypeScript
Raw Normal View History

2020-12-08 14:30:29 +01:00
import { remove } from 'fs-extra'
import { join } from 'path'
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'
import { v4 as uuidv4 } from 'uuid'
2021-02-18 11:28:00 +01:00
import { MVideo, MVideoCaption, MVideoCaptionFormattable, MVideoCaptionVideo } from '@server/types/models'
2018-08-14 14:59:53 +02:00
import { VideoCaption } from '../../../shared/models/videos/caption/video-caption.model'
2020-12-08 14:30:29 +01:00
import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions'
2018-07-12 19:02:00 +02:00
import { logger } from '../../helpers/logger'
2019-04-11 11:33:44 +02:00
import { CONFIG } from '../../initializers/config'
2020-12-08 14:30:29 +01:00
import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, VIDEO_LANGUAGES, WEBSERVER } from '../../initializers/constants'
import { buildWhereIdOrUUID, throwIfNotValid } from '../utils'
import { VideoModel } from './video'
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: [
2021-02-15 14:08:16 +01:00
{
fields: [ 'filename' ],
unique: true
},
2018-07-12 19:02:00 +02:00
{
fields: [ 'videoId' ]
},
{
fields: [ 'videoId', 'language' ],
unique: true
}
]
})
2020-12-08 14:30:29 +01:00
export class VideoCaptionModel extends Model {
2018-07-12 19:02:00 +02:00
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AllowNull(false)
@Is('VideoCaptionLanguage', value => throwIfNotValid(value, isVideoCaptionLanguageValid, 'language'))
@Column
language: string
2021-02-15 14:08:16 +01:00
@AllowNull(false)
@Column
filename: 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()) {
2021-02-15 14:08:16 +01:00
logger.info('Removing caption %s.', instance.filename)
2018-07-16 14:22:16 +02:00
try {
await instance.removeCaptionFile()
} catch (err) {
2021-02-15 14:08:16 +01:00
logger.error('Cannot remove caption file %s.', instance.filename)
2018-07-16 14:22:16 +02:00
}
2018-07-12 19:02:00 +02:00
}
return undefined
}
2020-12-08 14:30:29 +01:00
static loadByVideoIdAndLanguage (videoId: string | number, language: string): Promise<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)
}
2021-02-15 14:08:16 +01:00
static loadWithVideoByFilename (filename: string): Promise<MVideoCaptionVideo> {
const query = {
where: {
filename
},
include: [
{
model: VideoModel.unscoped(),
attributes: [ 'id', 'remote', 'uuid' ]
}
]
2018-07-12 19:02:00 +02:00
}
2021-02-15 14:08:16 +01:00
return VideoCaptionModel.findOne(query)
}
static async insertOrReplaceLanguage (caption: MVideoCaption, transaction: Transaction) {
const existing = await VideoCaptionModel.loadByVideoIdAndLanguage(caption.videoId, caption.language)
// Delete existing file
if (existing) await existing.destroy({ transaction })
return caption.save({ transaction })
2018-07-12 19:02:00 +02:00
}
2020-12-08 14:30:29 +01:00
static listVideoCaptions (videoId: number): Promise<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)
}
2021-02-15 14:08:16 +01:00
static generateCaptionName (language: string) {
return `${uuidv4()}-${language}.vtt`
}
2018-07-12 19:02:00 +02:00
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()
}
}
2021-02-15 14:08:16 +01:00
getCaptionStaticPath (this: MVideoCaption) {
return join(LAZY_STATIC_PATHS.VIDEO_CAPTIONS, this.filename)
2018-07-12 19:02:00 +02:00
}
2021-02-15 14:08:16 +01:00
removeCaptionFile (this: MVideoCaption) {
return remove(CONFIG.STORAGE.CAPTIONS_DIR + this.filename)
2018-07-12 19:02:00 +02:00
}
2021-02-18 11:28:00 +01:00
getFileUrl (video: MVideo) {
if (!this.Video) this.Video = video as VideoModel
if (video.isOwned()) return WEBSERVER.URL + this.getCaptionStaticPath()
2021-02-18 10:15:11 +01:00
return this.fileUrl
}
2018-07-12 19:02:00 +02:00
}