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'
29 tableName: 'videoFile',
35 fields: [ 'infoHash' ]
38 fields: [ 'videoId', 'resolution', 'fps' ],
43 export class VideoFileModel extends Model<VideoFileModel> {
51 @Is('VideoFileResolution', value => throwIfNotValid(value, isVideoFileResolutionValid, 'resolution'))
56 @Is('VideoFileSize', value => throwIfNotValid(value, isVideoFileSizeValid, 'size'))
57 @Column(DataType.BIGINT)
61 @Is('VideoFileExtname', value => throwIfNotValid(value, isVideoFileExtnameValid, 'extname'))
66 @Is('VideoFileInfohash', value => throwIfNotValid(value, isVideoFileInfoHashValid, 'info hash'))
72 @Is('VideoFileFPS', value => throwIfNotValid(value, isVideoFPSResolutionValid, 'fps'))
76 @ForeignKey(() => VideoModel)
80 @BelongsTo(() => VideoModel, {
88 @HasMany(() => VideoRedundancyModel, {
95 RedundancyVideos: VideoRedundancyModel[]
97 static doesInfohashExist (infoHash: string) {
98 const query = 'SELECT 1 FROM "videoFile" WHERE "infoHash" = $infoHash LIMIT 1'
100 type: QueryTypes.SELECT,
105 return VideoModel.sequelize.query(query, options)
106 .then(results => results.length === 1)
109 static loadWithVideo (id: number) {
113 model: VideoModel.unscoped(),
119 return VideoFileModel.findByPk(id, options)
122 static listByStreamingPlaylist (streamingPlaylistId: number, transaction: Transaction) {
126 model: VideoModel.unscoped(),
130 model: VideoStreamingPlaylistModel.unscoped(),
133 id: streamingPlaylistId
142 return VideoFileModel.findAll(query)
146 const query: FindOptions = {
150 model: VideoModel.unscoped(),
158 return VideoFileModel.aggregate('size', 'SUM', query)
160 totalLocalVideoFilesSize: parseAggregateResult(result)
164 hasSameUniqueKeysThan (other: VideoFileModel) {
165 return this.fps === other.fps &&
166 this.resolution === other.resolution &&
167 this.videoId === other.videoId