14 } from 'sequelize-typescript'
16 isVideoFileExtnameValid,
17 isVideoFileInfoHashValid,
18 isVideoFileResolutionValid,
20 isVideoFPSResolutionValid
21 } from '../../helpers/custom-validators/videos'
22 import { throwIfNotValid } from '../utils'
23 import { VideoModel } from './video'
24 import * as Sequelize from 'sequelize'
25 import { VideoRedundancyModel } from '../redundancy/video-redundancy'
28 tableName: 'videoFile',
34 fields: [ 'infoHash' ]
37 fields: [ 'videoId', 'resolution', 'fps' ],
42 export class VideoFileModel extends Model<VideoFileModel> {
50 @Is('VideoFileResolution', value => throwIfNotValid(value, isVideoFileResolutionValid, 'resolution'))
55 @Is('VideoFileSize', value => throwIfNotValid(value, isVideoFileSizeValid, 'size'))
56 @Column(DataType.BIGINT)
60 @Is('VideoFileExtname', value => throwIfNotValid(value, isVideoFileExtnameValid, 'extname'))
65 @Is('VideoFileInfohash', value => throwIfNotValid(value, isVideoFileInfoHashValid, 'info hash'))
71 @Is('VideoFileFPS', value => throwIfNotValid(value, isVideoFPSResolutionValid, 'fps'))
75 @ForeignKey(() => VideoModel)
79 @BelongsTo(() => VideoModel, {
87 @HasMany(() => VideoRedundancyModel, {
94 RedundancyVideos: VideoRedundancyModel[]
96 static doesInfohashExist (infoHash: string) {
97 const query = 'SELECT 1 FROM "videoFile" WHERE "infoHash" = $infoHash LIMIT 1'
99 type: Sequelize.QueryTypes.SELECT,
104 return VideoModel.sequelize.query(query, options)
106 return results.length === 1
110 static loadWithVideo (id: number) {
114 model: VideoModel.unscoped(),
120 return VideoFileModel.findById(id, options)
123 static async getStats () {
124 let totalLocalVideoFilesSize = await VideoFileModel.sum('size', {
128 model: VideoModel.unscoped(),
135 // Sequelize could return null...
136 if (!totalLocalVideoFilesSize) totalLocalVideoFilesSize = 0
139 totalLocalVideoFilesSize
143 hasSameUniqueKeysThan (other: VideoFileModel) {
144 return this.fps === other.fps &&
145 this.resolution === other.resolution &&
146 this.videoId === other.videoId