14 } from 'sequelize-typescript'
16 isVideoFileExtnameValid,
17 isVideoFileInfoHashValid,
18 isVideoFileResolutionValid,
20 isVideoFPSResolutionValid
21 } from '../../helpers/custom-validators/videos'
22 import { parseAggregateResult, throwIfNotValid } from '../utils'
23 import { VideoModel } from './video'
24 import { VideoRedundancyModel } from '../redundancy/video-redundancy'
25 import { VideoStreamingPlaylistModel } from './video-streaming-playlist'
26 import { FindOptions, QueryTypes, Transaction } from 'sequelize'
27 import { MIMETYPES } from '../../initializers/constants'
30 tableName: 'videoFile',
36 fields: [ 'infoHash' ]
39 fields: [ 'videoId', 'resolution', 'fps' ],
44 export class VideoFileModel extends Model<VideoFileModel> {
52 @Is('VideoFileResolution', value => throwIfNotValid(value, isVideoFileResolutionValid, 'resolution'))
57 @Is('VideoFileSize', value => throwIfNotValid(value, isVideoFileSizeValid, 'size'))
58 @Column(DataType.BIGINT)
62 @Is('VideoFileExtname', value => throwIfNotValid(value, isVideoFileExtnameValid, 'extname'))
67 @Is('VideoFileInfohash', value => throwIfNotValid(value, isVideoFileInfoHashValid, 'info hash'))
73 @Is('VideoFileFPS', value => throwIfNotValid(value, isVideoFPSResolutionValid, 'fps'))
77 @ForeignKey(() => VideoModel)
81 @BelongsTo(() => VideoModel, {
89 @HasMany(() => VideoRedundancyModel, {
96 RedundancyVideos: VideoRedundancyModel[]
98 static doesInfohashExist (infoHash: string) {
99 const query = 'SELECT 1 FROM "videoFile" WHERE "infoHash" = $infoHash LIMIT 1'
101 type: QueryTypes.SELECT,
106 return VideoModel.sequelize.query(query, options)
107 .then(results => results.length === 1)
110 static loadWithVideo (id: number) {
114 model: VideoModel.unscoped(),
120 return VideoFileModel.findByPk(id, options)
123 static listByStreamingPlaylist (streamingPlaylistId: number, transaction: Transaction) {
127 model: VideoModel.unscoped(),
131 model: VideoStreamingPlaylistModel.unscoped(),
134 id: streamingPlaylistId
143 return VideoFileModel.findAll(query)
147 const query: FindOptions = {
151 model: VideoModel.unscoped(),
159 return VideoFileModel.aggregate('size', 'SUM', query)
161 totalLocalVideoFilesSize: parseAggregateResult(result)
166 return !!MIMETYPES.AUDIO.EXT_MIMETYPE[this.extname]
169 hasSameUniqueKeysThan (other: VideoFileModel) {
170 return this.fps === other.fps &&
171 this.resolution === other.resolution &&
172 this.videoId === other.videoId