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'
|
2021-02-18 11:28:00 +01:00
|
|
|
import { MVideo, MVideoCaption, MVideoCaptionFormattable, MVideoCaptionVideo } from '@server/types/models'
|
2021-12-29 14:44:58 +01:00
|
|
|
import { buildUUID } from '@shared/extra-utils'
|
2021-12-16 18:04:16 +01:00
|
|
|
import { AttributesOnly } from '@shared/typescript-utils'
|
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
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2021-05-12 14:09:04 +02:00
|
|
|
export class VideoCaptionModel extends Model<Partial<AttributesOnly<VideoCaptionModel>>> {
|
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
|
|
|
|
|
2020-01-30 11:53:38 +01:00
|
|
|
@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
|
2021-06-15 09:17:19 +02:00
|
|
|
static async removeFiles (instance: VideoCaptionModel, options) {
|
2018-07-16 14:22:16 +02:00
|
|
|
if (!instance.Video) {
|
2021-06-15 09:17:19 +02:00
|
|
|
instance.Video = await instance.$get('Video', { transaction: options.transaction })
|
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
|
|
|
|
}
|
|
|
|
|
2021-06-09 16:22:01 +02:00
|
|
|
static loadByVideoIdAndLanguage (videoId: string | number, language: string, transaction?: Transaction): Promise<MVideoCaptionVideo> {
|
2018-07-12 19:02:00 +02:00
|
|
|
const videoInclude = {
|
|
|
|
model: VideoModel.unscoped(),
|
|
|
|
attributes: [ 'id', 'remote', 'uuid' ],
|
2021-06-15 09:17:19 +02:00
|
|
|
where: buildWhereIdOrUUID(videoId)
|
2018-07-12 19:02:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
language
|
|
|
|
},
|
|
|
|
include: [
|
|
|
|
videoInclude
|
2021-06-15 09:17:19 +02:00
|
|
|
],
|
|
|
|
transaction
|
2018-07-12 19:02:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2021-06-09 16:22:01 +02:00
|
|
|
const existing = await VideoCaptionModel.loadByVideoIdAndLanguage(caption.videoId, caption.language, transaction)
|
|
|
|
|
2021-02-15 14:08:16 +01:00
|
|
|
// Delete existing file
|
|
|
|
if (existing) await existing.destroy({ transaction })
|
|
|
|
|
|
|
|
return caption.save({ transaction })
|
2018-07-12 19:02:00 +02:00
|
|
|
}
|
|
|
|
|
2021-06-09 17:06:56 +02:00
|
|
|
static listVideoCaptions (videoId: number, transaction?: Transaction): 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
|
2021-06-09 16:22:01 +02:00
|
|
|
},
|
|
|
|
transaction
|
2018-07-12 19:02:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2021-06-28 17:30:59 +02:00
|
|
|
return `${buildUUID()}-${language}.vtt`
|
2021-02-15 14:08:16 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2020-01-30 11:53:38 +01:00
|
|
|
|
2021-02-18 11:28:00 +01:00
|
|
|
getFileUrl (video: MVideo) {
|
2020-01-30 11:53:38 +01:00
|
|
|
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
|
2020-01-30 11:53:38 +01:00
|
|
|
}
|
2021-06-09 16:22:01 +02:00
|
|
|
|
|
|
|
isEqual (this: MVideoCaption, other: MVideoCaption) {
|
|
|
|
if (this.fileUrl) return this.fileUrl === other.fileUrl
|
|
|
|
|
|
|
|
return this.filename === other.filename
|
|
|
|
}
|
2018-07-12 19:02:00 +02:00
|
|
|
}
|