]>
Commit | Line | Data |
---|---|---|
1735c825 | 1 | import { OrderItem, Transaction } from 'sequelize' |
40e87e9e C |
2 | import { |
3 | AllowNull, | |
4 | BeforeDestroy, | |
5 | BelongsTo, | |
6 | Column, | |
7 | CreatedAt, | |
8 | ForeignKey, | |
9 | Is, | |
10 | Model, | |
11 | Scopes, | |
12 | Table, | |
13 | UpdatedAt | |
14 | } from 'sequelize-typescript' | |
3acc5084 | 15 | import { buildWhereIdOrUUID, throwIfNotValid } from '../utils' |
40e87e9e C |
16 | import { VideoModel } from './video' |
17 | import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions' | |
59c76ffa | 18 | import { VideoCaption } from '../../../shared/models/videos/caption/video-caption.model' |
74dc3bca | 19 | import { STATIC_PATHS, VIDEO_LANGUAGES } from '../../initializers/constants' |
40e87e9e C |
20 | import { join } from 'path' |
21 | import { logger } from '../../helpers/logger' | |
62689b94 | 22 | import { remove } from 'fs-extra' |
6dd9de95 | 23 | import { CONFIG } from '../../initializers/config' |
40e87e9e C |
24 | |
25 | export enum ScopeNames { | |
26 | WITH_VIDEO_UUID_AND_REMOTE = 'WITH_VIDEO_UUID_AND_REMOTE' | |
27 | } | |
28 | ||
3acc5084 | 29 | @Scopes(() => ({ |
40e87e9e C |
30 | [ScopeNames.WITH_VIDEO_UUID_AND_REMOTE]: { |
31 | include: [ | |
32 | { | |
33 | attributes: [ 'uuid', 'remote' ], | |
3acc5084 | 34 | model: VideoModel.unscoped(), |
40e87e9e C |
35 | required: true |
36 | } | |
37 | ] | |
38 | } | |
3acc5084 | 39 | })) |
40e87e9e C |
40 | |
41 | @Table({ | |
42 | tableName: 'videoCaption', | |
43 | indexes: [ | |
44 | { | |
45 | fields: [ 'videoId' ] | |
46 | }, | |
47 | { | |
48 | fields: [ 'videoId', 'language' ], | |
49 | unique: true | |
50 | } | |
51 | ] | |
52 | }) | |
53 | export class VideoCaptionModel extends Model<VideoCaptionModel> { | |
54 | @CreatedAt | |
55 | createdAt: Date | |
56 | ||
57 | @UpdatedAt | |
58 | updatedAt: Date | |
59 | ||
60 | @AllowNull(false) | |
61 | @Is('VideoCaptionLanguage', value => throwIfNotValid(value, isVideoCaptionLanguageValid, 'language')) | |
62 | @Column | |
63 | language: string | |
64 | ||
65 | @ForeignKey(() => VideoModel) | |
66 | @Column | |
67 | videoId: number | |
68 | ||
69 | @BelongsTo(() => VideoModel, { | |
70 | foreignKey: { | |
71 | allowNull: false | |
72 | }, | |
73 | onDelete: 'CASCADE' | |
74 | }) | |
75 | Video: VideoModel | |
76 | ||
77 | @BeforeDestroy | |
78 | static async removeFiles (instance: VideoCaptionModel) { | |
f4001cf4 C |
79 | if (!instance.Video) { |
80 | instance.Video = await instance.$get('Video') as VideoModel | |
81 | } | |
40e87e9e C |
82 | |
83 | if (instance.isOwned()) { | |
8e0fd45e | 84 | logger.info('Removing captions %s of video %s.', instance.Video.uuid, instance.language) |
f4001cf4 C |
85 | |
86 | try { | |
87 | await instance.removeCaptionFile() | |
88 | } catch (err) { | |
89 | logger.error('Cannot remove caption file of video %s.', instance.Video.uuid) | |
90 | } | |
40e87e9e C |
91 | } |
92 | ||
93 | return undefined | |
94 | } | |
95 | ||
96 | static loadByVideoIdAndLanguage (videoId: string | number, language: string) { | |
97 | const videoInclude = { | |
98 | model: VideoModel.unscoped(), | |
99 | attributes: [ 'id', 'remote', 'uuid' ], | |
3acc5084 | 100 | where: buildWhereIdOrUUID(videoId) |
40e87e9e C |
101 | } |
102 | ||
40e87e9e C |
103 | const query = { |
104 | where: { | |
105 | language | |
106 | }, | |
107 | include: [ | |
108 | videoInclude | |
109 | ] | |
110 | } | |
111 | ||
112 | return VideoCaptionModel.findOne(query) | |
113 | } | |
114 | ||
1735c825 | 115 | static insertOrReplaceLanguage (videoId: number, language: string, transaction: Transaction) { |
40e87e9e C |
116 | const values = { |
117 | videoId, | |
118 | language | |
119 | } | |
120 | ||
1735c825 | 121 | return (VideoCaptionModel.upsert<VideoCaptionModel>(values, { transaction, returning: true }) as any) // FIXME: typings |
d382f4e9 | 122 | .then(([ caption ]) => caption) |
40e87e9e C |
123 | } |
124 | ||
125 | static listVideoCaptions (videoId: number) { | |
126 | const query = { | |
1735c825 | 127 | order: [ [ 'language', 'ASC' ] ] as OrderItem[], |
40e87e9e C |
128 | where: { |
129 | videoId | |
130 | } | |
131 | } | |
132 | ||
133 | return VideoCaptionModel.scope(ScopeNames.WITH_VIDEO_UUID_AND_REMOTE).findAll(query) | |
134 | } | |
135 | ||
136 | static getLanguageLabel (language: string) { | |
137 | return VIDEO_LANGUAGES[language] || 'Unknown' | |
138 | } | |
139 | ||
1735c825 | 140 | static deleteAllCaptionsOfRemoteVideo (videoId: number, transaction: Transaction) { |
40e87e9e C |
141 | const query = { |
142 | where: { | |
143 | videoId | |
144 | }, | |
145 | transaction | |
146 | } | |
147 | ||
148 | return VideoCaptionModel.destroy(query) | |
149 | } | |
150 | ||
151 | isOwned () { | |
152 | return this.Video.remote === false | |
153 | } | |
154 | ||
155 | toFormattedJSON (): VideoCaption { | |
156 | return { | |
157 | language: { | |
158 | id: this.language, | |
159 | label: VideoCaptionModel.getLanguageLabel(this.language) | |
160 | }, | |
161 | captionPath: this.getCaptionStaticPath() | |
162 | } | |
163 | } | |
164 | ||
165 | getCaptionStaticPath () { | |
166 | return join(STATIC_PATHS.VIDEO_CAPTIONS, this.getCaptionName()) | |
167 | } | |
168 | ||
169 | getCaptionName () { | |
170 | return `${this.Video.uuid}-${this.language}.vtt` | |
171 | } | |
172 | ||
173 | removeCaptionFile () { | |
62689b94 | 174 | return remove(CONFIG.STORAGE.CAPTIONS_DIR + this.getCaptionName()) |
40e87e9e C |
175 | } |
176 | } |