]>
Commit | Line | Data |
---|---|---|
93e1258c | 1 | import { values } from 'lodash' |
c48e82b5 C |
2 | import { |
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 |
16 | import { |
17 | isVideoFileInfoHashValid, | |
18 | isVideoFileResolutionValid, | |
19 | isVideoFileSizeValid, | |
20 | isVideoFPSResolutionValid | |
21 | } from '../../helpers/custom-validators/videos' | |
3fd3ab2d C |
22 | import { CONSTRAINTS_FIELDS } from '../../initializers' |
23 | import { throwIfNotValid } from '../utils' | |
24 | import { VideoModel } from './video' | |
cc43831a | 25 | import * as Sequelize from 'sequelize' |
c48e82b5 | 26 | import { 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 | }) |
43 | export 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 | ||
2e7cf5ae C |
69 | @AllowNull(false) |
70 | @Default(-1) | |
3a6f351b C |
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 | } | |
e5565833 C |
109 | |
110 | hasSameUniqueKeysThan (other: VideoFileModel) { | |
111 | return this.fps === other.fps && | |
112 | this.resolution === other.resolution && | |
113 | this.videoId === other.videoId | |
114 | } | |
93e1258c | 115 | } |