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