]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-file.ts
Fix video import CLI script
[github/Chocobozzz/PeerTube.git] / server / models / video / video-file.ts
CommitLineData
c48e82b5
C
1import {
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 15import {
14e2014a 16 isVideoFileExtnameValid,
3a6f351b
C
17 isVideoFileInfoHashValid,
18 isVideoFileResolutionValid,
19 isVideoFileSizeValid,
20 isVideoFPSResolutionValid
21} from '../../helpers/custom-validators/videos'
3acc5084 22import { parseAggregateResult, throwIfNotValid } from '../utils'
3fd3ab2d 23import { VideoModel } from './video'
c48e82b5 24import { VideoRedundancyModel } from '../redundancy/video-redundancy'
ae9bbed4 25import { VideoStreamingPlaylistModel } from './video-streaming-playlist'
3acc5084 26import { FindOptions, QueryTypes, Transaction } from 'sequelize'
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)
14e2014a
C
61 @Is('VideoFileExtname', value => throwIfNotValid(value, isVideoFileExtnameValid, 'extname'))
62 @Column
3fd3ab2d
C
63 extname: string
64
65 @AllowNull(false)
09209296 66 @Is('VideoFileInfohash', value => throwIfNotValid(value, isVideoFileInfoHashValid, 'info hash'))
3fd3ab2d
C
67 @Column
68 infoHash: string
69
2e7cf5ae
C
70 @AllowNull(false)
71 @Default(-1)
3a6f351b
C
72 @Is('VideoFileFPS', value => throwIfNotValid(value, isVideoFPSResolutionValid, 'fps'))
73 @Column
74 fps: number
75
3fd3ab2d
C
76 @ForeignKey(() => VideoModel)
77 @Column
78 videoId: number
79
80 @BelongsTo(() => VideoModel, {
93e1258c 81 foreignKey: {
93e1258c
C
82 allowNull: false
83 },
84 onDelete: 'CASCADE'
85 })
3fd3ab2d 86 Video: VideoModel
cc43831a 87
c48e82b5
C
88 @HasMany(() => VideoRedundancyModel, {
89 foreignKey: {
09209296 90 allowNull: true
c48e82b5
C
91 },
92 onDelete: 'CASCADE',
93 hooks: true
94 })
95 RedundancyVideos: VideoRedundancyModel[]
96
09209296 97 static doesInfohashExist (infoHash: string) {
cc43831a
C
98 const query = 'SELECT 1 FROM "videoFile" WHERE "infoHash" = $infoHash LIMIT 1'
99 const options = {
3acc5084 100 type: QueryTypes.SELECT,
cc43831a
C
101 bind: { infoHash },
102 raw: true
103 }
104
105 return VideoModel.sequelize.query(query, options)
3acc5084 106 .then(results => results.length === 1)
cc43831a 107 }
e5565833 108
25378bc8
C
109 static loadWithVideo (id: number) {
110 const options = {
111 include: [
112 {
113 model: VideoModel.unscoped(),
114 required: true
115 }
116 ]
117 }
118
9b39106d 119 return VideoFileModel.findByPk(id, options)
25378bc8
C
120 }
121
3acc5084 122 static listByStreamingPlaylist (streamingPlaylistId: number, transaction: Transaction) {
ae9bbed4
C
123 const query = {
124 include: [
125 {
126 model: VideoModel.unscoped(),
127 required: true,
128 include: [
129 {
130 model: VideoStreamingPlaylistModel.unscoped(),
131 required: true,
132 where: {
133 id: streamingPlaylistId
134 }
135 }
136 ]
137 }
138 ],
139 transaction
140 }
141
142 return VideoFileModel.findAll(query)
143 }
144
3acc5084
C
145 static getStats () {
146 const query: FindOptions = {
44b9c0ba
C
147 include: [
148 {
149 attributes: [],
150 model: VideoModel.unscoped(),
151 where: {
152 remote: false
153 }
154 }
155 ]
44b9c0ba 156 }
3acc5084
C
157
158 return VideoFileModel.aggregate('size', 'SUM', query)
159 .then(result => ({
160 totalLocalVideoFilesSize: parseAggregateResult(result)
161 }))
44b9c0ba
C
162 }
163
e5565833
C
164 hasSameUniqueKeysThan (other: VideoFileModel) {
165 return this.fps === other.fps &&
166 this.resolution === other.resolution &&
167 this.videoId === other.videoId
168 }
93e1258c 169}