video-file.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. @Table({
  28. tableName: 'videoFile',
  29. indexes: [
  30. {
  31. fields: [ 'videoId' ]
  32. },
  33. {
  34. fields: [ 'infoHash' ]
  35. },
  36. {
  37. fields: [ 'videoId', 'resolution', 'fps' ],
  38. unique: true
  39. }
  40. ]
  41. })
  42. export class VideoFileModel extends Model<VideoFileModel> {
  43. @CreatedAt
  44. createdAt: Date
  45. @UpdatedAt
  46. updatedAt: Date
  47. @AllowNull(false)
  48. @Is('VideoFileResolution', value => throwIfNotValid(value, isVideoFileResolutionValid, 'resolution'))
  49. @Column
  50. resolution: number
  51. @AllowNull(false)
  52. @Is('VideoFileSize', value => throwIfNotValid(value, isVideoFileSizeValid, 'size'))
  53. @Column(DataType.BIGINT)
  54. size: number
  55. @AllowNull(false)
  56. @Column(DataType.ENUM(values(CONSTRAINTS_FIELDS.VIDEOS.EXTNAME)))
  57. extname: string
  58. @AllowNull(false)
  59. @Is('VideoFileSize', value => throwIfNotValid(value, isVideoFileInfoHashValid, 'info hash'))
  60. @Column
  61. infoHash: string
  62. @AllowNull(false)
  63. @Default(-1)
  64. @Is('VideoFileFPS', value => throwIfNotValid(value, isVideoFPSResolutionValid, 'fps'))
  65. @Column
  66. fps: number
  67. @ForeignKey(() => VideoModel)
  68. @Column
  69. videoId: number
  70. @BelongsTo(() => VideoModel, {
  71. foreignKey: {
  72. allowNull: false
  73. },
  74. onDelete: 'CASCADE'
  75. })
  76. Video: VideoModel
  77. @HasMany(() => VideoRedundancyModel, {
  78. foreignKey: {
  79. allowNull: false
  80. },
  81. onDelete: 'CASCADE',
  82. hooks: true
  83. })
  84. RedundancyVideos: VideoRedundancyModel[]
  85. static isInfohashExists (infoHash: string) {
  86. const query = 'SELECT 1 FROM "videoFile" WHERE "infoHash" = $infoHash LIMIT 1'
  87. const options = {
  88. type: Sequelize.QueryTypes.SELECT,
  89. bind: { infoHash },
  90. raw: true
  91. }
  92. return VideoModel.sequelize.query(query, options)
  93. .then(results => {
  94. return results.length === 1
  95. })
  96. }
  97. static loadWithVideo (id: number) {
  98. const options = {
  99. include: [
  100. {
  101. model: VideoModel.unscoped(),
  102. required: true
  103. }
  104. ]
  105. }
  106. return VideoFileModel.findById(id, options)
  107. }
  108. hasSameUniqueKeysThan (other: VideoFileModel) {
  109. return this.fps === other.fps &&
  110. this.resolution === other.resolution &&
  111. this.videoId === other.videoId
  112. }
  113. }