]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-file.ts
Don't quick transcode with the wrong pixel format
[github/Chocobozzz/PeerTube.git] / server / models / video / video-file.ts
CommitLineData
c48e82b5
C
1import {
2 AllowNull,
3 BelongsTo,
4 Column,
5 CreatedAt,
6 DataType,
7 Default,
8 ForeignKey,
9 HasMany,
10 Is,
11 Model,
12 Table,
13 UpdatedAt
14} from 'sequelize-typescript'
3a6f351b 15import {
14e2014a 16 isVideoFileExtnameValid,
3a6f351b
C
17 isVideoFileInfoHashValid,
18 isVideoFileResolutionValid,
19 isVideoFileSizeValid,
20 isVideoFPSResolutionValid
21} from '../../helpers/custom-validators/videos'
3acc5084 22import { parseAggregateResult, throwIfNotValid } from '../utils'
3fd3ab2d 23import { VideoModel } from './video'
c48e82b5 24import { VideoRedundancyModel } from '../redundancy/video-redundancy'
ae9bbed4 25import { VideoStreamingPlaylistModel } from './video-streaming-playlist'
3acc5084 26import { FindOptions, QueryTypes, Transaction } from 'sequelize'
536598cf 27import { MIMETYPES } from '../../initializers/constants'
453e83ea 28import { MVideoFile } from '@server/typings/models'
93e1258c 29
3fd3ab2d
C
30@Table({
31 tableName: 'videoFile',
32 indexes: [
93e1258c 33 {
3fd3ab2d 34 fields: [ 'videoId' ]
93e1258c
C
35 },
36 {
3fd3ab2d 37 fields: [ 'infoHash' ]
8cd72bd3
C
38 },
39 {
40 fields: [ 'videoId', 'resolution', 'fps' ],
41 unique: true
93e1258c 42 }
93e1258c 43 ]
3fd3ab2d
C
44})
45export class VideoFileModel extends Model<VideoFileModel> {
46 @CreatedAt
47 createdAt: Date
48
49 @UpdatedAt
50 updatedAt: Date
51
52 @AllowNull(false)
53 @Is('VideoFileResolution', value => throwIfNotValid(value, isVideoFileResolutionValid, 'resolution'))
54 @Column
55 resolution: number
56
57 @AllowNull(false)
58 @Is('VideoFileSize', value => throwIfNotValid(value, isVideoFileSizeValid, 'size'))
59 @Column(DataType.BIGINT)
60 size: number
61
62 @AllowNull(false)
14e2014a
C
63 @Is('VideoFileExtname', value => throwIfNotValid(value, isVideoFileExtnameValid, 'extname'))
64 @Column
3fd3ab2d
C
65 extname: string
66
67 @AllowNull(false)
09209296 68 @Is('VideoFileInfohash', value => throwIfNotValid(value, isVideoFileInfoHashValid, 'info hash'))
3fd3ab2d
C
69 @Column
70 infoHash: string
71
2e7cf5ae
C
72 @AllowNull(false)
73 @Default(-1)
3a6f351b
C
74 @Is('VideoFileFPS', value => throwIfNotValid(value, isVideoFPSResolutionValid, 'fps'))
75 @Column
76 fps: number
77
3fd3ab2d
C
78 @ForeignKey(() => VideoModel)
79 @Column
80 videoId: number
81
82 @BelongsTo(() => VideoModel, {
93e1258c 83 foreignKey: {
93e1258c
C
84 allowNull: false
85 },
86 onDelete: 'CASCADE'
87 })
3fd3ab2d 88 Video: VideoModel
cc43831a 89
c48e82b5
C
90 @HasMany(() => VideoRedundancyModel, {
91 foreignKey: {
09209296 92 allowNull: true
c48e82b5
C
93 },
94 onDelete: 'CASCADE',
95 hooks: true
96 })
97 RedundancyVideos: VideoRedundancyModel[]
98
09209296 99 static doesInfohashExist (infoHash: string) {
cc43831a
C
100 const query = 'SELECT 1 FROM "videoFile" WHERE "infoHash" = $infoHash LIMIT 1'
101 const options = {
3acc5084 102 type: QueryTypes.SELECT,
cc43831a
C
103 bind: { infoHash },
104 raw: true
105 }
106
107 return VideoModel.sequelize.query(query, options)
3acc5084 108 .then(results => results.length === 1)
cc43831a 109 }
e5565833 110
25378bc8
C
111 static loadWithVideo (id: number) {
112 const options = {
113 include: [
114 {
115 model: VideoModel.unscoped(),
116 required: true
117 }
118 ]
119 }
120
9b39106d 121 return VideoFileModel.findByPk(id, options)
25378bc8
C
122 }
123
3acc5084 124 static listByStreamingPlaylist (streamingPlaylistId: number, transaction: Transaction) {
ae9bbed4
C
125 const query = {
126 include: [
127 {
128 model: VideoModel.unscoped(),
129 required: true,
130 include: [
131 {
132 model: VideoStreamingPlaylistModel.unscoped(),
133 required: true,
134 where: {
135 id: streamingPlaylistId
136 }
137 }
138 ]
139 }
140 ],
141 transaction
142 }
143
144 return VideoFileModel.findAll(query)
145 }
146
3acc5084
C
147 static getStats () {
148 const query: FindOptions = {
44b9c0ba
C
149 include: [
150 {
151 attributes: [],
152 model: VideoModel.unscoped(),
153 where: {
154 remote: false
155 }
156 }
157 ]
44b9c0ba 158 }
3acc5084
C
159
160 return VideoFileModel.aggregate('size', 'SUM', query)
161 .then(result => ({
162 totalLocalVideoFilesSize: parseAggregateResult(result)
163 }))
44b9c0ba
C
164 }
165
536598cf
C
166 isAudio () {
167 return !!MIMETYPES.AUDIO.EXT_MIMETYPE[this.extname]
168 }
169
453e83ea 170 hasSameUniqueKeysThan (other: MVideoFile) {
e5565833
C
171 return this.fps === other.fps &&
172 this.resolution === other.resolution &&
173 this.videoId === other.videoId
174 }
93e1258c 175}