From 93e1258c7cbc0d1235ca6d2a1f7c1875985328b8 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 25 Aug 2017 11:36:23 +0200 Subject: Move video file metadata in their own table Will be used for user video quotas and multiple video resolutions --- server/models/video/video-file.ts | 89 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 server/models/video/video-file.ts (limited to 'server/models/video/video-file.ts') diff --git a/server/models/video/video-file.ts b/server/models/video/video-file.ts new file mode 100644 index 000000000..09a30d7e0 --- /dev/null +++ b/server/models/video/video-file.ts @@ -0,0 +1,89 @@ +import * as Sequelize from 'sequelize' +import { values } from 'lodash' + +import { CONSTRAINTS_FIELDS } from '../../initializers' +import { + isVideoFileResolutionValid, + isVideoFileSizeValid, + isVideoFileInfoHashValid +} from '../../helpers' + +import { addMethodsToModel } from '../utils' +import { + VideoFileInstance, + VideoFileAttributes +} from './video-file-interface' + +let VideoFile: Sequelize.Model + +export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { + VideoFile = sequelize.define('VideoFile', + { + resolution: { + type: DataTypes.INTEGER, + allowNull: false, + validate: { + resolutionValid: value => { + const res = isVideoFileResolutionValid(value) + if (res === false) throw new Error('Video file resolution is not valid.') + } + } + }, + size: { + type: DataTypes.INTEGER, + allowNull: false, + validate: { + sizeValid: value => { + const res = isVideoFileSizeValid(value) + if (res === false) throw new Error('Video file size is not valid.') + } + } + }, + extname: { + type: DataTypes.ENUM(values(CONSTRAINTS_FIELDS.VIDEOS.EXTNAME)), + allowNull: false + }, + infoHash: { + type: DataTypes.STRING, + allowNull: false, + validate: { + infoHashValid: value => { + const res = isVideoFileInfoHashValid(value) + if (res === false) throw new Error('Video file info hash is not valid.') + } + } + } + }, + { + indexes: [ + { + fields: [ 'videoId' ] + }, + { + fields: [ 'infoHash' ] + } + ] + } + ) + + const classMethods = [ + associate + ] + addMethodsToModel(VideoFile, classMethods) + + return VideoFile +} + +// ------------------------------ STATICS ------------------------------ + +function associate (models) { + VideoFile.belongsTo(models.Video, { + foreignKey: { + name: 'videoId', + allowNull: false + }, + onDelete: 'CASCADE' + }) +} + +// ------------------------------ METHODS ------------------------------ -- cgit v1.2.3