]>
Commit | Line | Data |
---|---|---|
c48e82b5 C |
1 | import { |
2 | AllowNull, | |
3 | BelongsTo, | |
4 | Column, | |
5 | CreatedAt, | |
6 | DataType, | |
7 | Default, | |
8 | ForeignKey, | |
9 | HasMany, | |
10 | Is, | |
11 | Model, | |
12 | Table, | |
13 | UpdatedAt | |
14 | } from 'sequelize-typescript' | |
3a6f351b | 15 | import { |
14e2014a | 16 | isVideoFileExtnameValid, |
3a6f351b C |
17 | isVideoFileInfoHashValid, |
18 | isVideoFileResolutionValid, | |
19 | isVideoFileSizeValid, | |
20 | isVideoFPSResolutionValid | |
21 | } from '../../helpers/custom-validators/videos' | |
3fd3ab2d C |
22 | import { throwIfNotValid } from '../utils' |
23 | import { VideoModel } from './video' | |
cc43831a | 24 | import * as Sequelize from 'sequelize' |
c48e82b5 | 25 | import { VideoRedundancyModel } from '../redundancy/video-redundancy' |
93e1258c | 26 | |
3fd3ab2d C |
27 | @Table({ |
28 | tableName: 'videoFile', | |
29 | indexes: [ | |
93e1258c | 30 | { |
3fd3ab2d | 31 | fields: [ 'videoId' ] |
93e1258c C |
32 | }, |
33 | { | |
3fd3ab2d | 34 | fields: [ 'infoHash' ] |
8cd72bd3 C |
35 | }, |
36 | { | |
37 | fields: [ 'videoId', 'resolution', 'fps' ], | |
38 | unique: true | |
93e1258c | 39 | } |
93e1258c | 40 | ] |
3fd3ab2d C |
41 | }) |
42 | export class VideoFileModel extends Model<VideoFileModel> { | |
43 | @CreatedAt | |
44 | createdAt: Date | |
45 | ||
46 | @UpdatedAt | |
47 | updatedAt: Date | |
48 | ||
49 | @AllowNull(false) | |
50 | @Is('VideoFileResolution', value => throwIfNotValid(value, isVideoFileResolutionValid, 'resolution')) | |
51 | @Column | |
52 | resolution: number | |
53 | ||
54 | @AllowNull(false) | |
55 | @Is('VideoFileSize', value => throwIfNotValid(value, isVideoFileSizeValid, 'size')) | |
56 | @Column(DataType.BIGINT) | |
57 | size: number | |
58 | ||
59 | @AllowNull(false) | |
14e2014a C |
60 | @Is('VideoFileExtname', value => throwIfNotValid(value, isVideoFileExtnameValid, 'extname')) |
61 | @Column | |
3fd3ab2d C |
62 | extname: string |
63 | ||
64 | @AllowNull(false) | |
09209296 | 65 | @Is('VideoFileInfohash', value => throwIfNotValid(value, isVideoFileInfoHashValid, 'info hash')) |
3fd3ab2d C |
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: { | |
09209296 | 89 | allowNull: true |
c48e82b5 C |
90 | }, |
91 | onDelete: 'CASCADE', | |
92 | hooks: true | |
93 | }) | |
94 | RedundancyVideos: VideoRedundancyModel[] | |
95 | ||
09209296 | 96 | static doesInfohashExist (infoHash: string) { |
cc43831a C |
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 | 109 | |
25378bc8 C |
110 | static loadWithVideo (id: number) { |
111 | const options = { | |
112 | include: [ | |
113 | { | |
114 | model: VideoModel.unscoped(), | |
115 | required: true | |
116 | } | |
117 | ] | |
118 | } | |
119 | ||
9b39106d | 120 | return VideoFileModel.findByPk(id, options) |
25378bc8 C |
121 | } |
122 | ||
44b9c0ba C |
123 | static async getStats () { |
124 | let totalLocalVideoFilesSize = await VideoFileModel.sum('size', { | |
125 | include: [ | |
126 | { | |
127 | attributes: [], | |
128 | model: VideoModel.unscoped(), | |
129 | where: { | |
130 | remote: false | |
131 | } | |
132 | } | |
133 | ] | |
134 | } as any) | |
135 | // Sequelize could return null... | |
136 | if (!totalLocalVideoFilesSize) totalLocalVideoFilesSize = 0 | |
137 | ||
138 | return { | |
139 | totalLocalVideoFilesSize | |
140 | } | |
141 | } | |
142 | ||
e5565833 C |
143 | hasSameUniqueKeysThan (other: VideoFileModel) { |
144 | return this.fps === other.fps && | |
145 | this.resolution === other.resolution && | |
146 | this.videoId === other.videoId | |
147 | } | |
93e1258c | 148 | } |