From 6302d599cdf98b5a5363a2a1dcdc266447950191 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 15 Feb 2021 14:08:16 +0100 Subject: Generate a name for caption files --- server/models/video/video-caption.ts | 58 +++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 18 deletions(-) (limited to 'server/models/video/video-caption.ts') diff --git a/server/models/video/video-caption.ts b/server/models/video/video-caption.ts index e8e883dd0..a1553ea15 100644 --- a/server/models/video/video-caption.ts +++ b/server/models/video/video-caption.ts @@ -16,7 +16,7 @@ import { UpdatedAt } from 'sequelize-typescript' import { buildRemoteVideoBaseUrl } from '@server/helpers/activitypub' -import { MVideoAccountLight, MVideoCaptionFormattable, MVideoCaptionVideo } from '@server/types/models' +import { MVideoAccountLight, MVideoCaption, MVideoCaptionFormattable, MVideoCaptionVideo } from '@server/types/models' import { VideoCaption } from '../../../shared/models/videos/caption/video-caption.model' import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions' import { logger } from '../../helpers/logger' @@ -24,6 +24,7 @@ import { CONFIG } from '../../initializers/config' import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, VIDEO_LANGUAGES, WEBSERVER } from '../../initializers/constants' import { buildWhereIdOrUUID, throwIfNotValid } from '../utils' import { VideoModel } from './video' +import { v4 as uuidv4 } from 'uuid' export enum ScopeNames { WITH_VIDEO_UUID_AND_REMOTE = 'WITH_VIDEO_UUID_AND_REMOTE' @@ -44,6 +45,10 @@ export enum ScopeNames { @Table({ tableName: 'videoCaption', indexes: [ + { + fields: [ 'filename' ], + unique: true + }, { fields: [ 'videoId' ] }, @@ -65,6 +70,10 @@ export class VideoCaptionModel extends Model { @Column language: string + @AllowNull(false) + @Column + filename: string + @AllowNull(true) @Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max)) fileUrl: string @@ -88,12 +97,12 @@ export class VideoCaptionModel extends Model { } if (instance.isOwned()) { - logger.info('Removing captions %s of video %s.', instance.Video.uuid, instance.language) + logger.info('Removing caption %s.', instance.filename) try { await instance.removeCaptionFile() } catch (err) { - logger.error('Cannot remove caption file of video %s.', instance.Video.uuid) + logger.error('Cannot remove caption file %s.', instance.filename) } } @@ -119,15 +128,28 @@ export class VideoCaptionModel extends Model { return VideoCaptionModel.findOne(query) } - static insertOrReplaceLanguage (videoId: number, language: string, fileUrl: string, transaction: Transaction) { - const values = { - videoId, - language, - fileUrl + static loadWithVideoByFilename (filename: string): Promise { + const query = { + where: { + filename + }, + include: [ + { + model: VideoModel.unscoped(), + attributes: [ 'id', 'remote', 'uuid' ] + } + ] } - return VideoCaptionModel.upsert(values, { transaction, returning: true }) - .then(([ caption ]) => caption) + 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 }) } static listVideoCaptions (videoId: number): Promise { @@ -156,6 +178,10 @@ export class VideoCaptionModel extends Model { return VideoCaptionModel.destroy(query) } + static generateCaptionName (language: string) { + return `${uuidv4()}-${language}.vtt` + } + isOwned () { return this.Video.remote === false } @@ -170,16 +196,12 @@ export class VideoCaptionModel extends Model { } } - getCaptionStaticPath (this: MVideoCaptionFormattable) { - return join(LAZY_STATIC_PATHS.VIDEO_CAPTIONS, this.getCaptionName()) - } - - getCaptionName (this: MVideoCaptionFormattable) { - return `${this.Video.uuid}-${this.language}.vtt` + getCaptionStaticPath (this: MVideoCaption) { + return join(LAZY_STATIC_PATHS.VIDEO_CAPTIONS, this.filename) } - removeCaptionFile (this: MVideoCaptionFormattable) { - return remove(CONFIG.STORAGE.CAPTIONS_DIR + this.getCaptionName()) + removeCaptionFile (this: MVideoCaption) { + return remove(CONFIG.STORAGE.CAPTIONS_DIR + this.filename) } getFileUrl (video: MVideoAccountLight) { -- cgit v1.2.3