]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-file.ts
Fix videos list user NSFW policy
[github/Chocobozzz/PeerTube.git] / server / models / video / video-file.ts
CommitLineData
93e1258c 1import { values } from 'lodash'
c48e82b5
C
2import {
3 AllowNull,
4 BelongsTo,
5 Column,
6 CreatedAt,
7 DataType,
8 Default,
9 ForeignKey,
10 HasMany,
11 Is,
12 Model,
13 Table,
14 UpdatedAt
15} from 'sequelize-typescript'
3a6f351b
C
16import {
17 isVideoFileInfoHashValid,
18 isVideoFileResolutionValid,
19 isVideoFileSizeValid,
20 isVideoFPSResolutionValid
21} from '../../helpers/custom-validators/videos'
3fd3ab2d
C
22import { CONSTRAINTS_FIELDS } from '../../initializers'
23import { throwIfNotValid } from '../utils'
24import { VideoModel } from './video'
cc43831a 25import * as Sequelize from 'sequelize'
c48e82b5 26import { VideoRedundancyModel } from '../redundancy/video-redundancy'
93e1258c 27
3fd3ab2d
C
28@Table({
29 tableName: 'videoFile',
30 indexes: [
93e1258c 31 {
3fd3ab2d 32 fields: [ 'videoId' ]
93e1258c
C
33 },
34 {
3fd3ab2d 35 fields: [ 'infoHash' ]
8cd72bd3
C
36 },
37 {
38 fields: [ 'videoId', 'resolution', 'fps' ],
39 unique: true
93e1258c 40 }
93e1258c 41 ]
3fd3ab2d
C
42})
43export class VideoFileModel extends Model<VideoFileModel> {
44 @CreatedAt
45 createdAt: Date
46
47 @UpdatedAt
48 updatedAt: Date
49
50 @AllowNull(false)
51 @Is('VideoFileResolution', value => throwIfNotValid(value, isVideoFileResolutionValid, 'resolution'))
52 @Column
53 resolution: number
54
55 @AllowNull(false)
56 @Is('VideoFileSize', value => throwIfNotValid(value, isVideoFileSizeValid, 'size'))
57 @Column(DataType.BIGINT)
58 size: number
59
60 @AllowNull(false)
61 @Column(DataType.ENUM(values(CONSTRAINTS_FIELDS.VIDEOS.EXTNAME)))
62 extname: string
63
64 @AllowNull(false)
65 @Is('VideoFileSize', value => throwIfNotValid(value, isVideoFileInfoHashValid, 'info hash'))
66 @Column
67 infoHash: string
68
3a6f351b
C
69 @AllowNull(true)
70 @Default(null)
71 @Is('VideoFileFPS', value => throwIfNotValid(value, isVideoFPSResolutionValid, 'fps'))
72 @Column
73 fps: number
74
3fd3ab2d
C
75 @ForeignKey(() => VideoModel)
76 @Column
77 videoId: number
78
79 @BelongsTo(() => VideoModel, {
93e1258c 80 foreignKey: {
93e1258c
C
81 allowNull: false
82 },
83 onDelete: 'CASCADE'
84 })
3fd3ab2d 85 Video: VideoModel
cc43831a 86
c48e82b5
C
87 @HasMany(() => VideoRedundancyModel, {
88 foreignKey: {
89 allowNull: false
90 },
91 onDelete: 'CASCADE',
92 hooks: true
93 })
94 RedundancyVideos: VideoRedundancyModel[]
95
cc43831a
C
96 static isInfohashExists (infoHash: string) {
97 const query = 'SELECT 1 FROM "videoFile" WHERE "infoHash" = $infoHash LIMIT 1'
98 const options = {
99 type: Sequelize.QueryTypes.SELECT,
100 bind: { infoHash },
101 raw: true
102 }
103
104 return VideoModel.sequelize.query(query, options)
105 .then(results => {
106 return results.length === 1
107 })
108 }
93e1258c 109}