X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-file.ts;h=3bc4855f38afb2bb5c86cb1adf24a8ed96caf0a6;hb=d9eaee3939bf2e93e5d775d32bce77842201faba;hp=df4067a4e59c4aa439a00af170f8c8e9fd6d41a0;hpb=3fd3ab2d34d512b160a5e6084d7609be7b4f4452;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-file.ts b/server/models/video/video-file.ts index df4067a4e..3bc4855f3 100644 --- a/server/models/video/video-file.ts +++ b/server/models/video/video-file.ts @@ -1,9 +1,15 @@ 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, 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' @Table({ tableName: 'videoFile', @@ -13,6 +19,10 @@ import { VideoModel } from './video' }, { fields: [ 'infoHash' ] + }, + { + fields: [ 'videoId', 'resolution', 'fps' ], + unique: true } ] }) @@ -42,6 +52,12 @@ export class VideoFileModel extends Model { @Column infoHash: string + @AllowNull(true) + @Default(null) + @Is('VideoFileFPS', value => throwIfNotValid(value, isVideoFPSResolutionValid, 'fps')) + @Column + fps: number + @ForeignKey(() => VideoModel) @Column videoId: number @@ -53,4 +69,18 @@ export class VideoFileModel extends Model { onDelete: 'CASCADE' }) Video: VideoModel + + 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 + }) + } }