]>
Commit | Line | Data |
---|---|---|
1 | import { values } from 'lodash' | |
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' | |
16 | import { | |
17 | isVideoFileInfoHashValid, | |
18 | isVideoFileResolutionValid, | |
19 | isVideoFileSizeValid, | |
20 | isVideoFPSResolutionValid | |
21 | } from '../../helpers/custom-validators/videos' | |
22 | import { CONSTRAINTS_FIELDS } from '../../initializers' | |
23 | import { throwIfNotValid } from '../utils' | |
24 | import { VideoModel } from './video' | |
25 | import * as Sequelize from 'sequelize' | |
26 | import { VideoRedundancyModel } from '../redundancy/video-redundancy' | |
27 | ||
28 | @Table({ | |
29 | tableName: 'videoFile', | |
30 | indexes: [ | |
31 | { | |
32 | fields: [ 'videoId' ] | |
33 | }, | |
34 | { | |
35 | fields: [ 'infoHash' ] | |
36 | }, | |
37 | { | |
38 | fields: [ 'videoId', 'resolution', 'fps' ], | |
39 | unique: true | |
40 | } | |
41 | ] | |
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 | ||
69 | @AllowNull(true) | |
70 | @Default(null) | |
71 | @Is('VideoFileFPS', value => throwIfNotValid(value, isVideoFPSResolutionValid, 'fps')) | |
72 | @Column | |
73 | fps: number | |
74 | ||
75 | @ForeignKey(() => VideoModel) | |
76 | @Column | |
77 | videoId: number | |
78 | ||
79 | @BelongsTo(() => VideoModel, { | |
80 | foreignKey: { | |
81 | allowNull: false | |
82 | }, | |
83 | onDelete: 'CASCADE' | |
84 | }) | |
85 | Video: VideoModel | |
86 | ||
87 | @HasMany(() => VideoRedundancyModel, { | |
88 | foreignKey: { | |
89 | allowNull: false | |
90 | }, | |
91 | onDelete: 'CASCADE', | |
92 | hooks: true | |
93 | }) | |
94 | RedundancyVideos: VideoRedundancyModel[] | |
95 | ||
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 | } | |
109 | } |