]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video-file.ts
Add federation to ownership change
[github/Chocobozzz/PeerTube.git] / server / models / video / video-file.ts
index df4067a4e59c4aa439a00af170f8c8e9fd6d41a0..3bc4855f38afb2bb5c86cb1adf24a8ed96caf0a6 100644 (file)
@@ -1,9 +1,15 @@
 import { values } from 'lodash'
-import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
-import { isVideoFileInfoHashValid, isVideoFileResolutionValid, isVideoFileSizeValid } from '../../helpers/custom-validators/videos'
+import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
+import {
+  isVideoFileInfoHashValid,
+  isVideoFileResolutionValid,
+  isVideoFileSizeValid,
+  isVideoFPSResolutionValid
+} from '../../helpers/custom-validators/videos'
 import { CONSTRAINTS_FIELDS } from '../../initializers'
 import { throwIfNotValid } from '../utils'
 import { VideoModel } from './video'
+import * as Sequelize from 'sequelize'
 
 @Table({
   tableName: 'videoFile',
@@ -13,6 +19,10 @@ import { VideoModel } from './video'
     },
     {
       fields: [ 'infoHash' ]
+    },
+    {
+      fields: [ 'videoId', 'resolution', 'fps' ],
+      unique: true
     }
   ]
 })
@@ -42,6 +52,12 @@ export class VideoFileModel extends Model<VideoFileModel> {
   @Column
   infoHash: string
 
+  @AllowNull(true)
+  @Default(null)
+  @Is('VideoFileFPS', value => throwIfNotValid(value, isVideoFPSResolutionValid, 'fps'))
+  @Column
+  fps: number
+
   @ForeignKey(() => VideoModel)
   @Column
   videoId: number
@@ -53,4 +69,18 @@ export class VideoFileModel extends Model<VideoFileModel> {
     onDelete: 'CASCADE'
   })
   Video: VideoModel
+
+  static isInfohashExists (infoHash: string) {
+    const query = 'SELECT 1 FROM "videoFile" WHERE "infoHash" = $infoHash LIMIT 1'
+    const options = {
+      type: Sequelize.QueryTypes.SELECT,
+      bind: { infoHash },
+      raw: true
+    }
+
+    return VideoModel.sequelize.query(query, options)
+              .then(results => {
+                return results.length === 1
+              })
+  }
 }