1 import { values } from 'lodash'
15 } from 'sequelize-typescript'
17 isVideoFileInfoHashValid,
18 isVideoFileResolutionValid,
20 isVideoFPSResolutionValid
21 } from '../../helpers/custom-validators/videos'
22 import { CONSTRAINTS_FIELDS } from '../../initializers'
23 import { throwIfNotValid } from '../utils'
24 import { VideoModel } from './video'
25 import * as Sequelize from 'sequelize'
26 import { VideoRedundancyModel } from '../redundancy/video-redundancy'
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 @Column(DataType.ENUM(values(CONSTRAINTS_FIELDS.VIDEOS.EXTNAME)))
65 @Is('VideoFileSize', 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 isInfohashExists (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 hasSameUniqueKeysThan (other: VideoFileModel) {
124 return this.fps === other.fps &&
125 this.resolution === other.resolution &&
126 this.videoId === other.videoId