aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/video-file.ts
blob: ead7f3e034d43315e2a662523bed24d72a068927 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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<VideoFileInstance, VideoFileAttributes>

export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
  VideoFile = sequelize.define<VideoFileInstance, VideoFileAttributes>('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.BIGINT,
        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 ------------------------------