15 } from 'sequelize-typescript'
16 import { ActorModel } from '../activitypub/actor'
17 import { getVideoSort, throwIfNotValid } from '../utils'
18 import { isActivityPubUrlValid, isUrlValid } from '../../helpers/custom-validators/activitypub/misc'
19 import { CONFIG, CONSTRAINTS_FIELDS, VIDEO_EXT_MIMETYPE } from '../../initializers'
20 import { VideoFileModel } from '../video/video-file'
21 import { getServerActor } from '../../helpers/utils'
22 import { VideoModel } from '../video/video'
23 import { VideoRedundancyStrategy } from '../../../shared/models/redundancy'
24 import { logger } from '../../helpers/logger'
25 import { CacheFileObject } from '../../../shared'
26 import { VideoChannelModel } from '../video/video-channel'
27 import { ServerModel } from '../server/server'
28 import { sample } from 'lodash'
29 import { isTestInstance } from '../../helpers/core-utils'
31 export enum ScopeNames {
32 WITH_VIDEO = 'WITH_VIDEO'
36 [ ScopeNames.WITH_VIDEO ]: {
39 model: () => VideoFileModel,
43 model: () => VideoModel,
53 tableName: 'videoRedundancy',
56 fields: [ 'videoFileId' ]
67 export class VideoRedundancyModel extends Model<VideoRedundancyModel> {
80 @Is('VideoRedundancyFileUrl', value => throwIfNotValid(value, isUrlValid, 'fileUrl'))
81 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS_REDUNDANCY.URL.max))
85 @Is('VideoRedundancyUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
86 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS_REDUNDANCY.URL.max))
91 strategy: string // Only used by us
93 @ForeignKey(() => VideoFileModel)
97 @BelongsTo(() => VideoFileModel, {
103 VideoFile: VideoFileModel
105 @ForeignKey(() => ActorModel)
109 @BelongsTo(() => ActorModel, {
118 static removeFilesAndSendDelete (instance: VideoRedundancyModel) {
120 if (!instance.strategy) return
122 logger.info('Removing video file %s-.', instance.VideoFile.Video.uuid, instance.VideoFile.resolution)
124 return instance.VideoFile.Video.removeFile(instance.VideoFile)
127 static loadByFileId (videoFileId: number) {
134 return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query)
137 static loadByUrl (url: string) {
144 return VideoRedundancyModel.findOne(query)
147 static getVideoSample (rows: { id: number }[]) {
148 const ids = rows.map(r => r.id)
149 const id = sample(ids)
151 return VideoModel.loadWithFile(id, undefined, !isTestInstance())
154 static async findMostViewToDuplicate (randomizedFactor: number) {
157 attributes: [ 'id', 'views' ],
158 logging: !isTestInstance(),
159 limit: randomizedFactor,
160 order: getVideoSort('-views'),
162 await VideoRedundancyModel.buildVideoFileForDuplication(),
163 VideoRedundancyModel.buildServerRedundancyInclude()
167 const rows = await VideoModel.unscoped().findAll(query)
169 return VideoRedundancyModel.getVideoSample(rows as { id: number }[])
172 static async findTrendingToDuplicate (randomizedFactor: number) {
175 attributes: [ 'id', 'views' ],
177 logging: !isTestInstance(),
178 group: 'VideoModel.id',
179 limit: randomizedFactor,
180 order: getVideoSort('-trending'),
182 await VideoRedundancyModel.buildVideoFileForDuplication(),
183 VideoRedundancyModel.buildServerRedundancyInclude(),
185 VideoModel.buildTrendingQuery(CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS)
189 const rows = await VideoModel.unscoped().findAll(query)
191 return VideoRedundancyModel.getVideoSample(rows as { id: number }[])
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 // Don't include video files we already duplicated
241 private static async buildVideoFileForDuplication () {
242 const actor = await getServerActor()
244 const notIn = Sequelize.literal(
246 `SELECT "videoFileId" FROM "videoRedundancy" WHERE "actorId" = ${actor.id} AND "expiresOn" >= NOW()` +
252 model: VideoFileModel.unscoped(),
256 [ Sequelize.Op.notIn ]: notIn
262 private static buildServerRedundancyInclude () {
265 model: VideoChannelModel.unscoped(),
270 model: ActorModel.unscoped(),
275 model: ServerModel.unscoped(),
278 redundancyAllowed: true