X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-caption.ts;h=76243bf488ffc02b4ba814f9df51f11a6dd6a060;hb=970ceac0a6bf4990b8924738591df4949491ec9b;hp=5a1becc47936c8169e8f91737448bcdc1ab9f004;hpb=f4001cf408a99049d01a356bfb20a62342de06ea;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-caption.ts b/server/models/video/video-caption.ts index 5a1becc47..76243bf48 100644 --- a/server/models/video/video-caption.ts +++ b/server/models/video/video-caption.ts @@ -1,4 +1,4 @@ -import * as Sequelize from 'sequelize' +import { OrderItem, Transaction } from 'sequelize' import { AllowNull, BeforeDestroy, @@ -12,30 +12,31 @@ import { Table, UpdatedAt } from 'sequelize-typescript' -import { throwIfNotValid } from '../utils' +import { buildWhereIdOrUUID, throwIfNotValid } from '../utils' import { VideoModel } from './video' import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions' -import { VideoCaption } from '../../../shared/models/videos/video-caption.model' -import { CONFIG, STATIC_PATHS, VIDEO_LANGUAGES } from '../../initializers' +import { VideoCaption } from '../../../shared/models/videos/caption/video-caption.model' +import { STATIC_PATHS, VIDEO_LANGUAGES } from '../../initializers/constants' import { join } from 'path' import { logger } from '../../helpers/logger' -import { unlinkPromise } from '../../helpers/core-utils' +import { remove } from 'fs-extra' +import { CONFIG } from '../../initializers/config' export enum ScopeNames { WITH_VIDEO_UUID_AND_REMOTE = 'WITH_VIDEO_UUID_AND_REMOTE' } -@Scopes({ +@Scopes(() => ({ [ScopeNames.WITH_VIDEO_UUID_AND_REMOTE]: { include: [ { attributes: [ 'uuid', 'remote' ], - model: () => VideoModel.unscoped(), + model: VideoModel.unscoped(), required: true } ] } -}) +})) @Table({ tableName: 'videoCaption', @@ -80,7 +81,7 @@ export class VideoCaptionModel extends Model { } if (instance.isOwned()) { - logger.debug('Removing captions %s of video %s.', instance.Video.uuid, instance.language) + logger.info('Removing captions %s of video %s.', instance.Video.uuid, instance.language) try { await instance.removeCaptionFile() @@ -96,12 +97,9 @@ export class VideoCaptionModel extends Model { const videoInclude = { model: VideoModel.unscoped(), attributes: [ 'id', 'remote', 'uuid' ], - where: { } + where: buildWhereIdOrUUID(videoId) } - if (typeof videoId === 'string') videoInclude.where['uuid'] = videoId - else videoInclude.where['id'] = videoId - const query = { where: { language @@ -114,18 +112,19 @@ export class VideoCaptionModel extends Model { return VideoCaptionModel.findOne(query) } - static insertOrReplaceLanguage (videoId: number, language: string, transaction: Sequelize.Transaction) { + static insertOrReplaceLanguage (videoId: number, language: string, transaction: Transaction) { const values = { videoId, language } - return VideoCaptionModel.upsert(values, { transaction }) + return (VideoCaptionModel.upsert(values, { transaction, returning: true }) as any) // FIXME: typings + .then(([ caption ]) => caption) } static listVideoCaptions (videoId: number) { const query = { - order: [ [ 'language', 'ASC' ] ], + order: [ [ 'language', 'ASC' ] ] as OrderItem[], where: { videoId } @@ -138,7 +137,7 @@ export class VideoCaptionModel extends Model { return VIDEO_LANGUAGES[language] || 'Unknown' } - static deleteAllCaptionsOfRemoteVideo (videoId: number, transaction: Sequelize.Transaction) { + static deleteAllCaptionsOfRemoteVideo (videoId: number, transaction: Transaction) { const query = { where: { videoId @@ -172,6 +171,6 @@ export class VideoCaptionModel extends Model { } removeCaptionFile () { - return unlinkPromise(CONFIG.STORAGE.CAPTIONS_DIR + this.getCaptionName()) + return remove(CONFIG.STORAGE.CAPTIONS_DIR + this.getCaptionName()) } }