aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/video-caption.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/video/video-caption.ts')
-rw-r--r--server/models/video/video-caption.ts30
1 files changed, 20 insertions, 10 deletions
diff --git a/server/models/video/video-caption.ts b/server/models/video/video-caption.ts
index bfdec73e9..d24be56c3 100644
--- a/server/models/video/video-caption.ts
+++ b/server/models/video/video-caption.ts
@@ -15,8 +15,9 @@ import {
15 Table, 15 Table,
16 UpdatedAt 16 UpdatedAt
17} from 'sequelize-typescript' 17} from 'sequelize-typescript'
18import { v4 as uuidv4 } from 'uuid' 18import { buildUUID } from '@server/helpers/uuid'
19import { MVideo, MVideoCaption, MVideoCaptionFormattable, MVideoCaptionVideo } from '@server/types/models' 19import { MVideo, MVideoCaption, MVideoCaptionFormattable, MVideoCaptionVideo } from '@server/types/models'
20import { AttributesOnly } from '@shared/core-utils'
20import { VideoCaption } from '../../../shared/models/videos/caption/video-caption.model' 21import { VideoCaption } from '../../../shared/models/videos/caption/video-caption.model'
21import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions' 22import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions'
22import { logger } from '../../helpers/logger' 23import { logger } from '../../helpers/logger'
@@ -57,7 +58,7 @@ export enum ScopeNames {
57 } 58 }
58 ] 59 ]
59}) 60})
60export class VideoCaptionModel extends Model { 61export class VideoCaptionModel extends Model<Partial<AttributesOnly<VideoCaptionModel>>> {
61 @CreatedAt 62 @CreatedAt
62 createdAt: Date 63 createdAt: Date
63 64
@@ -90,9 +91,9 @@ export class VideoCaptionModel extends Model {
90 Video: VideoModel 91 Video: VideoModel
91 92
92 @BeforeDestroy 93 @BeforeDestroy
93 static async removeFiles (instance: VideoCaptionModel) { 94 static async removeFiles (instance: VideoCaptionModel, options) {
94 if (!instance.Video) { 95 if (!instance.Video) {
95 instance.Video = await instance.$get('Video') 96 instance.Video = await instance.$get('Video', { transaction: options.transaction })
96 } 97 }
97 98
98 if (instance.isOwned()) { 99 if (instance.isOwned()) {
@@ -108,7 +109,7 @@ export class VideoCaptionModel extends Model {
108 return undefined 109 return undefined
109 } 110 }
110 111
111 static loadByVideoIdAndLanguage (videoId: string | number, language: string): Promise<MVideoCaptionVideo> { 112 static loadByVideoIdAndLanguage (videoId: string | number, language: string, transaction?: Transaction): Promise<MVideoCaptionVideo> {
112 const videoInclude = { 113 const videoInclude = {
113 model: VideoModel.unscoped(), 114 model: VideoModel.unscoped(),
114 attributes: [ 'id', 'remote', 'uuid' ], 115 attributes: [ 'id', 'remote', 'uuid' ],
@@ -121,7 +122,8 @@ export class VideoCaptionModel extends Model {
121 }, 122 },
122 include: [ 123 include: [
123 videoInclude 124 videoInclude
124 ] 125 ],
126 transaction
125 } 127 }
126 128
127 return VideoCaptionModel.findOne(query) 129 return VideoCaptionModel.findOne(query)
@@ -144,19 +146,21 @@ export class VideoCaptionModel extends Model {
144 } 146 }
145 147
146 static async insertOrReplaceLanguage (caption: MVideoCaption, transaction: Transaction) { 148 static async insertOrReplaceLanguage (caption: MVideoCaption, transaction: Transaction) {
147 const existing = await VideoCaptionModel.loadByVideoIdAndLanguage(caption.videoId, caption.language) 149 const existing = await VideoCaptionModel.loadByVideoIdAndLanguage(caption.videoId, caption.language, transaction)
150
148 // Delete existing file 151 // Delete existing file
149 if (existing) await existing.destroy({ transaction }) 152 if (existing) await existing.destroy({ transaction })
150 153
151 return caption.save({ transaction }) 154 return caption.save({ transaction })
152 } 155 }
153 156
154 static listVideoCaptions (videoId: number): Promise<MVideoCaptionVideo[]> { 157 static listVideoCaptions (videoId: number, transaction?: Transaction): Promise<MVideoCaptionVideo[]> {
155 const query = { 158 const query = {
156 order: [ [ 'language', 'ASC' ] ] as OrderItem[], 159 order: [ [ 'language', 'ASC' ] ] as OrderItem[],
157 where: { 160 where: {
158 videoId 161 videoId
159 } 162 },
163 transaction
160 } 164 }
161 165
162 return VideoCaptionModel.scope(ScopeNames.WITH_VIDEO_UUID_AND_REMOTE).findAll(query) 166 return VideoCaptionModel.scope(ScopeNames.WITH_VIDEO_UUID_AND_REMOTE).findAll(query)
@@ -178,7 +182,7 @@ export class VideoCaptionModel extends Model {
178 } 182 }
179 183
180 static generateCaptionName (language: string) { 184 static generateCaptionName (language: string) {
181 return `${uuidv4()}-${language}.vtt` 185 return `${buildUUID()}-${language}.vtt`
182 } 186 }
183 187
184 isOwned () { 188 isOwned () {
@@ -210,4 +214,10 @@ export class VideoCaptionModel extends Model {
210 214
211 return this.fileUrl 215 return this.fileUrl
212 } 216 }
217
218 isEqual (this: MVideoCaption, other: MVideoCaption) {
219 if (this.fileUrl) return this.fileUrl === other.fileUrl
220
221 return this.filename === other.filename
222 }
213} 223}