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