]>
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' | |
3acc5084 | 22 | import { parseAggregateResult, throwIfNotValid } from '../utils' |
3fd3ab2d | 23 | import { VideoModel } from './video' |
c48e82b5 | 24 | import { VideoRedundancyModel } from '../redundancy/video-redundancy' |
ae9bbed4 | 25 | import { VideoStreamingPlaylistModel } from './video-streaming-playlist' |
3acc5084 | 26 | import { FindOptions, QueryTypes, Transaction } from 'sequelize' |
536598cf | 27 | import { MIMETYPES } from '../../initializers/constants' |
93e1258c | 28 | |
3fd3ab2d C |
29 | @Table({ |
30 | tableName: 'videoFile', | |
31 | indexes: [ | |
93e1258c | 32 | { |
3fd3ab2d | 33 | fields: [ 'videoId' ] |
93e1258c C |
34 | }, |
35 | { | |
3fd3ab2d | 36 | fields: [ 'infoHash' ] |
8cd72bd3 C |
37 | }, |
38 | { | |
39 | fields: [ 'videoId', 'resolution', 'fps' ], | |
40 | unique: true | |
93e1258c | 41 | } |
93e1258c | 42 | ] |
3fd3ab2d C |
43 | }) |
44 | export class VideoFileModel extends Model<VideoFileModel> { | |
45 | @CreatedAt | |
46 | createdAt: Date | |
47 | ||
48 | @UpdatedAt | |
49 | updatedAt: Date | |
50 | ||
51 | @AllowNull(false) | |
52 | @Is('VideoFileResolution', value => throwIfNotValid(value, isVideoFileResolutionValid, 'resolution')) | |
53 | @Column | |
54 | resolution: number | |
55 | ||
56 | @AllowNull(false) | |
57 | @Is('VideoFileSize', value => throwIfNotValid(value, isVideoFileSizeValid, 'size')) | |
58 | @Column(DataType.BIGINT) | |
59 | size: number | |
60 | ||
61 | @AllowNull(false) | |
14e2014a C |
62 | @Is('VideoFileExtname', value => throwIfNotValid(value, isVideoFileExtnameValid, 'extname')) |
63 | @Column | |
3fd3ab2d C |
64 | extname: string |
65 | ||
66 | @AllowNull(false) | |
09209296 | 67 | @Is('VideoFileInfohash', value => throwIfNotValid(value, isVideoFileInfoHashValid, 'info hash')) |
3fd3ab2d C |
68 | @Column |
69 | infoHash: string | |
70 | ||
2e7cf5ae C |
71 | @AllowNull(false) |
72 | @Default(-1) | |
3a6f351b C |
73 | @Is('VideoFileFPS', value => throwIfNotValid(value, isVideoFPSResolutionValid, 'fps')) |
74 | @Column | |
75 | fps: number | |
76 | ||
3fd3ab2d C |
77 | @ForeignKey(() => VideoModel) |
78 | @Column | |
79 | videoId: number | |
80 | ||
81 | @BelongsTo(() => VideoModel, { | |
93e1258c | 82 | foreignKey: { |
93e1258c C |
83 | allowNull: false |
84 | }, | |
85 | onDelete: 'CASCADE' | |
86 | }) | |
3fd3ab2d | 87 | Video: VideoModel |
cc43831a | 88 | |
c48e82b5 C |
89 | @HasMany(() => VideoRedundancyModel, { |
90 | foreignKey: { | |
09209296 | 91 | allowNull: true |
c48e82b5 C |
92 | }, |
93 | onDelete: 'CASCADE', | |
94 | hooks: true | |
95 | }) | |
96 | RedundancyVideos: VideoRedundancyModel[] | |
97 | ||
09209296 | 98 | static doesInfohashExist (infoHash: string) { |
cc43831a C |
99 | const query = 'SELECT 1 FROM "videoFile" WHERE "infoHash" = $infoHash LIMIT 1' |
100 | const options = { | |
3acc5084 | 101 | type: QueryTypes.SELECT, |
cc43831a C |
102 | bind: { infoHash }, |
103 | raw: true | |
104 | } | |
105 | ||
106 | return VideoModel.sequelize.query(query, options) | |
3acc5084 | 107 | .then(results => results.length === 1) |
cc43831a | 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 | ||
3acc5084 | 123 | static listByStreamingPlaylist (streamingPlaylistId: number, transaction: Transaction) { |
ae9bbed4 C |
124 | const query = { |
125 | include: [ | |
126 | { | |
127 | model: VideoModel.unscoped(), | |
128 | required: true, | |
129 | include: [ | |
130 | { | |
131 | model: VideoStreamingPlaylistModel.unscoped(), | |
132 | required: true, | |
133 | where: { | |
134 | id: streamingPlaylistId | |
135 | } | |
136 | } | |
137 | ] | |
138 | } | |
139 | ], | |
140 | transaction | |
141 | } | |
142 | ||
143 | return VideoFileModel.findAll(query) | |
144 | } | |
145 | ||
3acc5084 C |
146 | static getStats () { |
147 | const query: FindOptions = { | |
44b9c0ba C |
148 | include: [ |
149 | { | |
150 | attributes: [], | |
151 | model: VideoModel.unscoped(), | |
152 | where: { | |
153 | remote: false | |
154 | } | |
155 | } | |
156 | ] | |
44b9c0ba | 157 | } |
3acc5084 C |
158 | |
159 | return VideoFileModel.aggregate('size', 'SUM', query) | |
160 | .then(result => ({ | |
161 | totalLocalVideoFilesSize: parseAggregateResult(result) | |
162 | })) | |
44b9c0ba C |
163 | } |
164 | ||
536598cf C |
165 | isAudio () { |
166 | return !!MIMETYPES.AUDIO.EXT_MIMETYPE[this.extname] | |
167 | } | |
168 | ||
e5565833 C |
169 | hasSameUniqueKeysThan (other: VideoFileModel) { |
170 | return this.fps === other.fps && | |
171 | this.resolution === other.resolution && | |
172 | this.videoId === other.videoId | |
173 | } | |
93e1258c | 174 | } |