]>
Commit | Line | Data |
---|---|---|
b49f22d8 C |
1 | import { remove } from 'fs-extra' |
2 | import { join } from 'path' | |
1735c825 | 3 | import { OrderItem, Transaction } from 'sequelize' |
40e87e9e C |
4 | import { |
5 | AllowNull, | |
6 | BeforeDestroy, | |
7 | BelongsTo, | |
8 | Column, | |
a1587156 C |
9 | CreatedAt, |
10 | DataType, | |
40e87e9e C |
11 | ForeignKey, |
12 | Is, | |
13 | Model, | |
14 | Scopes, | |
15 | Table, | |
16 | UpdatedAt | |
17 | } from 'sequelize-typescript' | |
90a8bd30 | 18 | import { v4 as uuidv4 } from 'uuid' |
8efc27bf | 19 | import { MVideo, MVideoCaption, MVideoCaptionFormattable, MVideoCaptionVideo } from '@server/types/models' |
59c76ffa | 20 | import { VideoCaption } from '../../../shared/models/videos/caption/video-caption.model' |
b49f22d8 | 21 | import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions' |
40e87e9e | 22 | import { logger } from '../../helpers/logger' |
6dd9de95 | 23 | import { CONFIG } from '../../initializers/config' |
b49f22d8 C |
24 | import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, VIDEO_LANGUAGES, WEBSERVER } from '../../initializers/constants' |
25 | import { buildWhereIdOrUUID, throwIfNotValid } from '../utils' | |
26 | import { VideoModel } from './video' | |
40e87e9e C |
27 | |
28 | export enum ScopeNames { | |
29 | WITH_VIDEO_UUID_AND_REMOTE = 'WITH_VIDEO_UUID_AND_REMOTE' | |
30 | } | |
31 | ||
3acc5084 | 32 | @Scopes(() => ({ |
40e87e9e C |
33 | [ScopeNames.WITH_VIDEO_UUID_AND_REMOTE]: { |
34 | include: [ | |
35 | { | |
453e83ea | 36 | attributes: [ 'id', 'uuid', 'remote' ], |
3acc5084 | 37 | model: VideoModel.unscoped(), |
40e87e9e C |
38 | required: true |
39 | } | |
40 | ] | |
41 | } | |
3acc5084 | 42 | })) |
40e87e9e C |
43 | |
44 | @Table({ | |
45 | tableName: 'videoCaption', | |
46 | indexes: [ | |
6302d599 C |
47 | { |
48 | fields: [ 'filename' ], | |
49 | unique: true | |
50 | }, | |
40e87e9e C |
51 | { |
52 | fields: [ 'videoId' ] | |
53 | }, | |
54 | { | |
55 | fields: [ 'videoId', 'language' ], | |
56 | unique: true | |
57 | } | |
58 | ] | |
59 | }) | |
b49f22d8 | 60 | export class VideoCaptionModel extends Model { |
40e87e9e C |
61 | @CreatedAt |
62 | createdAt: Date | |
63 | ||
64 | @UpdatedAt | |
65 | updatedAt: Date | |
66 | ||
67 | @AllowNull(false) | |
68 | @Is('VideoCaptionLanguage', value => throwIfNotValid(value, isVideoCaptionLanguageValid, 'language')) | |
69 | @Column | |
70 | language: string | |
71 | ||
6302d599 C |
72 | @AllowNull(false) |
73 | @Column | |
74 | filename: string | |
75 | ||
ca6d3622 C |
76 | @AllowNull(true) |
77 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max)) | |
78 | fileUrl: string | |
79 | ||
40e87e9e C |
80 | @ForeignKey(() => VideoModel) |
81 | @Column | |
82 | videoId: number | |
83 | ||
84 | @BelongsTo(() => VideoModel, { | |
85 | foreignKey: { | |
86 | allowNull: false | |
87 | }, | |
88 | onDelete: 'CASCADE' | |
89 | }) | |
90 | Video: VideoModel | |
91 | ||
92 | @BeforeDestroy | |
93 | static async removeFiles (instance: VideoCaptionModel) { | |
f4001cf4 | 94 | if (!instance.Video) { |
e6122097 | 95 | instance.Video = await instance.$get('Video') |
f4001cf4 | 96 | } |
40e87e9e C |
97 | |
98 | if (instance.isOwned()) { | |
6302d599 | 99 | logger.info('Removing caption %s.', instance.filename) |
f4001cf4 C |
100 | |
101 | try { | |
102 | await instance.removeCaptionFile() | |
103 | } catch (err) { | |
6302d599 | 104 | logger.error('Cannot remove caption file %s.', instance.filename) |
f4001cf4 | 105 | } |
40e87e9e C |
106 | } |
107 | ||
108 | return undefined | |
109 | } | |
110 | ||
b49f22d8 | 111 | static loadByVideoIdAndLanguage (videoId: string | number, language: string): Promise<MVideoCaptionVideo> { |
40e87e9e C |
112 | const videoInclude = { |
113 | model: VideoModel.unscoped(), | |
114 | attributes: [ 'id', 'remote', 'uuid' ], | |
3acc5084 | 115 | where: buildWhereIdOrUUID(videoId) |
40e87e9e C |
116 | } |
117 | ||
40e87e9e C |
118 | const query = { |
119 | where: { | |
120 | language | |
121 | }, | |
122 | include: [ | |
123 | videoInclude | |
124 | ] | |
125 | } | |
126 | ||
127 | return VideoCaptionModel.findOne(query) | |
128 | } | |
129 | ||
6302d599 C |
130 | static loadWithVideoByFilename (filename: string): Promise<MVideoCaptionVideo> { |
131 | const query = { | |
132 | where: { | |
133 | filename | |
134 | }, | |
135 | include: [ | |
136 | { | |
137 | model: VideoModel.unscoped(), | |
138 | attributes: [ 'id', 'remote', 'uuid' ] | |
139 | } | |
140 | ] | |
40e87e9e C |
141 | } |
142 | ||
6302d599 C |
143 | return VideoCaptionModel.findOne(query) |
144 | } | |
145 | ||
146 | static async insertOrReplaceLanguage (caption: MVideoCaption, transaction: Transaction) { | |
147 | const existing = await VideoCaptionModel.loadByVideoIdAndLanguage(caption.videoId, caption.language) | |
148 | // Delete existing file | |
149 | if (existing) await existing.destroy({ transaction }) | |
150 | ||
151 | return caption.save({ transaction }) | |
40e87e9e C |
152 | } |
153 | ||
b49f22d8 | 154 | static listVideoCaptions (videoId: number): Promise<MVideoCaptionVideo[]> { |
40e87e9e | 155 | const query = { |
1735c825 | 156 | order: [ [ 'language', 'ASC' ] ] as OrderItem[], |
40e87e9e C |
157 | where: { |
158 | videoId | |
159 | } | |
160 | } | |
161 | ||
162 | return VideoCaptionModel.scope(ScopeNames.WITH_VIDEO_UUID_AND_REMOTE).findAll(query) | |
163 | } | |
164 | ||
165 | static getLanguageLabel (language: string) { | |
166 | return VIDEO_LANGUAGES[language] || 'Unknown' | |
167 | } | |
168 | ||
1735c825 | 169 | static deleteAllCaptionsOfRemoteVideo (videoId: number, transaction: Transaction) { |
40e87e9e C |
170 | const query = { |
171 | where: { | |
172 | videoId | |
173 | }, | |
174 | transaction | |
175 | } | |
176 | ||
177 | return VideoCaptionModel.destroy(query) | |
178 | } | |
179 | ||
6302d599 C |
180 | static generateCaptionName (language: string) { |
181 | return `${uuidv4()}-${language}.vtt` | |
182 | } | |
183 | ||
40e87e9e C |
184 | isOwned () { |
185 | return this.Video.remote === false | |
186 | } | |
187 | ||
1ca9f7c3 | 188 | toFormattedJSON (this: MVideoCaptionFormattable): VideoCaption { |
40e87e9e C |
189 | return { |
190 | language: { | |
191 | id: this.language, | |
192 | label: VideoCaptionModel.getLanguageLabel(this.language) | |
193 | }, | |
194 | captionPath: this.getCaptionStaticPath() | |
195 | } | |
196 | } | |
197 | ||
6302d599 C |
198 | getCaptionStaticPath (this: MVideoCaption) { |
199 | return join(LAZY_STATIC_PATHS.VIDEO_CAPTIONS, this.filename) | |
40e87e9e C |
200 | } |
201 | ||
6302d599 C |
202 | removeCaptionFile (this: MVideoCaption) { |
203 | return remove(CONFIG.STORAGE.CAPTIONS_DIR + this.filename) | |
40e87e9e | 204 | } |
ca6d3622 | 205 | |
8efc27bf | 206 | getFileUrl (video: MVideo) { |
ca6d3622 C |
207 | if (!this.Video) this.Video = video as VideoModel |
208 | ||
209 | if (video.isOwned()) return WEBSERVER.URL + this.getCaptionStaticPath() | |
ca6d3622 | 210 | |
d9a2a031 | 211 | return this.fileUrl |
ca6d3622 | 212 | } |
40e87e9e | 213 | } |