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'
28 import { MVideoFile } from '@server/typings/models'
31 tableName: 'videoFile',
37 fields: [ 'infoHash' ]
40 fields: [ 'videoId', 'resolution', 'fps' ],
45 export class VideoFileModel extends Model<VideoFileModel> {
53 @Is('VideoFileResolution', value => throwIfNotValid(value, isVideoFileResolutionValid, 'resolution'))
58 @Is('VideoFileSize', value => throwIfNotValid(value, isVideoFileSizeValid, 'size'))
59 @Column(DataType.BIGINT)
63 @Is('VideoFileExtname', value => throwIfNotValid(value, isVideoFileExtnameValid, 'extname'))
68 @Is('VideoFileInfohash', value => throwIfNotValid(value, isVideoFileInfoHashValid, 'info hash'))
74 @Is('VideoFileFPS', value => throwIfNotValid(value, isVideoFPSResolutionValid, 'fps'))
78 @ForeignKey(() => VideoModel)
82 @BelongsTo(() => VideoModel, {
90 @HasMany(() => VideoRedundancyModel, {
97 RedundancyVideos: VideoRedundancyModel[]
99 static doesInfohashExist (infoHash: string) {
100 const query = 'SELECT 1 FROM "videoFile" WHERE "infoHash" = $infoHash LIMIT 1'
102 type: QueryTypes.SELECT,
107 return VideoModel.sequelize.query(query, options)
108 .then(results => results.length === 1)
111 static loadWithVideo (id: number) {
115 model: VideoModel.unscoped(),
121 return VideoFileModel.findByPk(id, options)
124 static listByStreamingPlaylist (streamingPlaylistId: number, transaction: Transaction) {
128 model: VideoModel.unscoped(),
132 model: VideoStreamingPlaylistModel.unscoped(),
135 id: streamingPlaylistId
144 return VideoFileModel.findAll(query)
148 const query: FindOptions = {
152 model: VideoModel.unscoped(),
160 return VideoFileModel.aggregate('size', 'SUM', query)
162 totalLocalVideoFilesSize: parseAggregateResult(result)
167 return !!MIMETYPES.AUDIO.EXT_MIMETYPE[this.extname]
170 hasSameUniqueKeysThan (other: MVideoFile) {
171 return this.fps === other.fps &&
172 this.resolution === other.resolution &&
173 this.videoId === other.videoId