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