15 } from 'sequelize-typescript'
16 import { ActorModel } from '../activitypub/actor'
17 import { throwIfNotValid } from '../utils'
18 import { isActivityPubUrlValid, isUrlValid } from '../../helpers/custom-validators/activitypub/misc'
19 import { CONSTRAINTS_FIELDS, VIDEO_EXT_MIMETYPE } from '../../initializers'
20 import { VideoFileModel } from '../video/video-file'
21 import { isDateValid } from '../../helpers/custom-validators/misc'
22 import { getServerActor } from '../../helpers/utils'
23 import { VideoModel } from '../video/video'
24 import { VideoRedundancyStrategy } from '../../../shared/models/redundancy'
25 import { logger } from '../../helpers/logger'
26 import { CacheFileObject } from '../../../shared'
27 import { VideoChannelModel } from '../video/video-channel'
28 import { ServerModel } from '../server/server'
29 import { sample } from 'lodash'
30 import { isTestInstance } from '../../helpers/core-utils'
32 export enum ScopeNames {
33 WITH_VIDEO = 'WITH_VIDEO'
37 [ ScopeNames.WITH_VIDEO ]: {
40 model: () => VideoFileModel,
44 model: () => VideoModel,
54 tableName: 'videoRedundancy',
57 fields: [ 'videoFileId' ]
68 export class VideoRedundancyModel extends Model<VideoRedundancyModel> {
81 @Is('VideoRedundancyFileUrl', value => throwIfNotValid(value, isUrlValid, 'fileUrl'))
82 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS_REDUNDANCY.URL.max))
86 @Is('VideoRedundancyUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
87 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS_REDUNDANCY.URL.max))
92 strategy: string // Only used by us
94 @ForeignKey(() => VideoFileModel)
98 @BelongsTo(() => VideoFileModel, {
104 VideoFile: VideoFileModel
106 @ForeignKey(() => ActorModel)
110 @BelongsTo(() => ActorModel, {
119 static removeFilesAndSendDelete (instance: VideoRedundancyModel) {
121 if (!instance.strategy) return
123 logger.info('Removing video file %s-.', instance.VideoFile.Video.uuid, instance.VideoFile.resolution)
125 return instance.VideoFile.Video.removeFile(instance.VideoFile)
128 static loadByFileId (videoFileId: number) {
135 return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query)
138 static loadByUrl (url: string) {
145 return VideoRedundancyModel.findOne(query)
148 static async findMostViewToDuplicate (randomizedFactor: number) {
151 logging: !isTestInstance(),
152 limit: randomizedFactor,
153 order: [ [ 'views', 'DESC' ] ],
156 model: VideoFileModel.unscoped(),
160 [ Sequelize.Op.notIn ]: await VideoRedundancyModel.buildExcludeIn()
166 model: VideoChannelModel.unscoped(),
171 model: ActorModel.unscoped(),
176 model: ServerModel.unscoped(),
179 redundancyAllowed: true
189 const rows = await VideoModel.unscoped().findAll(query)
194 static async getVideoFiles (strategy: VideoRedundancyStrategy) {
195 const actor = await getServerActor()
197 const queryVideoFiles = {
198 logging: !isTestInstance(),
205 return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO)
206 .findAll(queryVideoFiles)
209 static listAllExpired () {
211 logging: !isTestInstance(),
214 [Sequelize.Op.lt]: new Date()
219 return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO)
223 toActivityPubObject (): CacheFileObject {
226 type: 'CacheFile' as 'CacheFile',
227 object: this.VideoFile.Video.url,
228 expires: this.expiresOn.toISOString(),
231 mimeType: VIDEO_EXT_MIMETYPE[ this.VideoFile.extname ] as any,
233 height: this.VideoFile.resolution,
234 size: this.VideoFile.size,
235 fps: this.VideoFile.fps
240 private static async buildExcludeIn () {
241 const actor = await getServerActor()
243 return Sequelize.literal(
245 `SELECT "videoFileId" FROM "videoRedundancy" WHERE "actorId" = ${actor.id} AND "expiresOn" >= NOW()` +