import { values } from 'lodash'
-import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
-import { isVideoFileInfoHashValid, isVideoFileResolutionValid, isVideoFileSizeValid } from '../../helpers/custom-validators/videos'
+import {
+ AllowNull,
+ BelongsTo,
+ Column,
+ CreatedAt,
+ DataType,
+ Default,
+ ForeignKey,
+ HasMany,
+ Is,
+ Model,
+ Table,
+ UpdatedAt
+} from 'sequelize-typescript'
+import {
+ isVideoFileInfoHashValid,
+ isVideoFileResolutionValid,
+ isVideoFileSizeValid,
+ isVideoFPSResolutionValid
+} from '../../helpers/custom-validators/videos'
import { CONSTRAINTS_FIELDS } from '../../initializers'
import { throwIfNotValid } from '../utils'
import { VideoModel } from './video'
+import * as Sequelize from 'sequelize'
+import { VideoRedundancyModel } from '../redundancy/video-redundancy'
@Table({
tableName: 'videoFile',
},
{
fields: [ 'infoHash' ]
+ },
+ {
+ fields: [ 'videoId', 'resolution', 'fps' ],
+ unique: true
}
]
})
@Column
infoHash: string
+ @AllowNull(true)
+ @Default(null)
+ @Is('VideoFileFPS', value => throwIfNotValid(value, isVideoFPSResolutionValid, 'fps'))
+ @Column
+ fps: number
+
@ForeignKey(() => VideoModel)
@Column
videoId: number
onDelete: 'CASCADE'
})
Video: VideoModel
+
+ @HasMany(() => VideoRedundancyModel, {
+ foreignKey: {
+ allowNull: false
+ },
+ onDelete: 'CASCADE',
+ hooks: true
+ })
+ RedundancyVideos: VideoRedundancyModel[]
+
+ static isInfohashExists (infoHash: string) {
+ const query = 'SELECT 1 FROM "videoFile" WHERE "infoHash" = $infoHash LIMIT 1'
+ const options = {
+ type: Sequelize.QueryTypes.SELECT,
+ bind: { infoHash },
+ raw: true
+ }
+
+ return VideoModel.sequelize.query(query, options)
+ .then(results => {
+ return results.length === 1
+ })
+ }
}