X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-file.ts;h=f040803b9a0240d2156df0dc073e0609f237b3fe;hb=d382f4e9175c1520835e41c3573471a84bcf1713;hp=372d18d698efa2419409b6a763d113639f404002;hpb=3a6f351b255d21ec42578632600ba699885f350e;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-file.ts b/server/models/video/video-file.ts index 372d18d69..f040803b9 100644 --- a/server/models/video/video-file.ts +++ b/server/models/video/video-file.ts @@ -1,5 +1,18 @@ import { values } from 'lodash' -import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' +import { + AllowNull, + BelongsTo, + Column, + CreatedAt, + DataType, + Default, + ForeignKey, + HasMany, + Is, + Model, + Table, + UpdatedAt +} from 'sequelize-typescript' import { isVideoFileInfoHashValid, isVideoFileResolutionValid, @@ -9,6 +22,8 @@ import { 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', @@ -18,6 +33,10 @@ import { VideoModel } from './video' }, { fields: [ 'infoHash' ] + }, + { + fields: [ 'videoId', 'resolution', 'fps' ], + unique: true } ] }) @@ -47,8 +66,8 @@ export class VideoFileModel extends Model { @Column infoHash: string - @AllowNull(true) - @Default(null) + @AllowNull(false) + @Default(-1) @Is('VideoFileFPS', value => throwIfNotValid(value, isVideoFPSResolutionValid, 'fps')) @Column fps: number @@ -64,4 +83,33 @@ export class VideoFileModel extends Model { 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 + }) + } + + hasSameUniqueKeysThan (other: VideoFileModel) { + return this.fps === other.fps && + this.resolution === other.resolution && + this.videoId === other.videoId + } }