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'
26 import { VideoStreamingPlaylistModel } from './video-streaming-playlist'
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: Sequelize.QueryTypes.SELECT,
105 return VideoModel.sequelize.query(query, options)
107 return results.length === 1
111 static loadWithVideo (id: number) {
115 model: VideoModel.unscoped(),
121 return VideoFileModel.findByPk(id, options)
124 static listByStreamingPlaylist (streamingPlaylistId: number, transaction: Sequelize.Transaction) {
128 model: VideoModel.unscoped(),
132 model: VideoStreamingPlaylistModel.unscoped(),
135 id: streamingPlaylistId
144 return VideoFileModel.findAll(query)
147 static async getStats () {
148 let totalLocalVideoFilesSize = await VideoFileModel.sum('size', {
152 model: VideoModel.unscoped(),
159 // Sequelize could return null...
160 if (!totalLocalVideoFilesSize) totalLocalVideoFilesSize = 0
163 totalLocalVideoFilesSize
167 hasSameUniqueKeysThan (other: VideoFileModel) {
168 return this.fps === other.fps &&
169 this.resolution === other.resolution &&
170 this.videoId === other.videoId