X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fredundancy%2Fvideo-redundancy.ts;h=c2a72b71f42d554bae801ca1e106f88a741d2bd8;hb=5e47f6ab984a7d00782e4c7030afffa1ba480add;hp=53293df378c9dcb85a0e0acede4ec7699c529834;hpb=2e1e4af03bc9ccd2470a9f293d70dc27596aae24;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/redundancy/video-redundancy.ts b/server/models/redundancy/video-redundancy.ts index 53293df37..c2a72b71f 100644 --- a/server/models/redundancy/video-redundancy.ts +++ b/server/models/redundancy/video-redundancy.ts @@ -1,5 +1,5 @@ import { sample } from 'lodash' -import { FindOptions, literal, Op, QueryTypes, Transaction, WhereOptions } from 'sequelize' +import { literal, Op, QueryTypes, Transaction, WhereOptions } from 'sequelize' import { AllowNull, BeforeDestroy, @@ -16,22 +16,26 @@ import { } from 'sequelize-typescript' import { getServerActor } from '@server/models/application/application' import { MActor, MVideoForRedundancyAPI, MVideoRedundancy, MVideoRedundancyAP, MVideoRedundancyVideo } from '@server/types/models' -import { VideoRedundanciesTarget } from '@shared/models/redundancy/video-redundancies-filters.model' import { + CacheFileObject, FileRedundancyInformation, StreamingPlaylistRedundancyInformation, - VideoRedundancy -} from '@shared/models/redundancy/video-redundancy.model' -import { CacheFileObject, VideoPrivacy } from '../../../shared' -import { VideoRedundancyStrategy, VideoRedundancyStrategyWithManual } from '../../../shared/models/redundancy' + VideoPrivacy, + VideoRedundanciesTarget, + VideoRedundancy, + VideoRedundancyStrategy, + VideoRedundancyStrategyWithManual +} from '@shared/models' +import { AttributesOnly } from '@shared/typescript-utils' import { isTestInstance } from '../../helpers/core-utils' import { isActivityPubUrlValid, isUrlValid } from '../../helpers/custom-validators/activitypub/misc' import { logger } from '../../helpers/logger' import { CONFIG } from '../../initializers/config' import { CONSTRAINTS_FIELDS, MIMETYPES } from '../../initializers/constants' -import { ActorModel } from '../activitypub/actor' +import { ActorModel } from '../actor/actor' import { ServerModel } from '../server/server' -import { getSort, getVideoSort, parseAggregateResult, throwIfNotValid } from '../utils' +import { getSort, getVideoSort, parseAggregateResult, throwIfNotValid } from '../shared' +import { ScheduleVideoUpdateModel } from '../video/schedule-video-update' import { VideoModel } from '../video/video' import { VideoChannelModel } from '../video/video-channel' import { VideoFileModel } from '../video/video-file' @@ -77,13 +81,16 @@ export enum ScopeNames { { fields: [ 'actorId' ] }, + { + fields: [ 'expiresOn' ] + }, { fields: [ 'url' ], unique: true } ] }) -export class VideoRedundancyModel extends Model { +export class VideoRedundancyModel extends Model>> { @CreatedAt createdAt: Date @@ -155,8 +162,8 @@ export class VideoRedundancyModel extends Model { const logIdentifier = `${videoFile.Video.uuid}-${videoFile.resolution}` logger.info('Removing duplicated video file %s.', logIdentifier) - videoFile.Video.removeFile(videoFile, true) - .catch(err => logger.error('Cannot delete %s files.', logIdentifier, { err })) + videoFile.Video.removeWebTorrentFile(videoFile, true) + .catch(err => logger.error('Cannot delete %s files.', logIdentifier, { err })) } if (instance.videoStreamingPlaylistId) { @@ -374,7 +381,13 @@ export class VideoRedundancyModel extends Model { ...this.buildVideoIdsForDuplication(peertubeActor) }, include: [ - VideoRedundancyModel.buildServerRedundancyInclude() + VideoRedundancyModel.buildServerRedundancyInclude(), + + // Required by publishedAt sort + { + model: ScheduleVideoUpdateModel.unscoped(), + required: false + } ] } @@ -400,51 +413,7 @@ export class VideoRedundancyModel extends Model { return VideoRedundancyModel.scope([ ScopeNames.WITH_VIDEO ]).findOne(query) } - static async getTotalDuplicated (strategy: VideoRedundancyStrategy) { - const actor = await getServerActor() - const redundancyInclude = { - attributes: [], - model: VideoRedundancyModel, - required: true, - where: { - actorId: actor.id, - strategy - } - } - - const queryFiles: FindOptions = { - include: [ redundancyInclude ] - } - - const queryStreamingPlaylists: FindOptions = { - include: [ - { - attributes: [], - model: VideoModel.unscoped(), - required: true, - include: [ - { - required: true, - attributes: [], - model: VideoStreamingPlaylistModel.unscoped(), - include: [ - redundancyInclude - ] - } - ] - } - ] - } - - return Promise.all([ - VideoFileModel.aggregate('size', 'SUM', queryFiles), - VideoFileModel.aggregate('size', 'SUM', queryStreamingPlaylists) - ]).then(([ r1, r2 ]) => { - return parseAggregateResult(r1) + parseAggregateResult(r2) - }) - } - - static async listLocalExpired () { + static async listLocalExpired (): Promise { const actor = await getServerActor() const query = { @@ -503,16 +472,34 @@ export class VideoRedundancyModel extends Model { const query = { where: { - actorId: actor.id + [Op.and]: [ + { + actorId: actor.id + }, + { + [Op.or]: [ + { + '$VideoStreamingPlaylist.id$': { + [Op.ne]: null + } + }, + { + '$VideoFile.id$': { + [Op.ne]: null + } + } + ] + } + ] }, include: [ { - model: VideoFileModel, + model: VideoFileModel.unscoped(), required: false, include: [ buildVideoInclude() ] }, { - model: VideoStreamingPlaylistModel, + model: VideoStreamingPlaylistModel.unscoped(), required: false, include: [ buildVideoInclude() ] } @@ -543,7 +530,7 @@ export class VideoRedundancyModel extends Model { } if (strategy) { - Object.assign(redundancyWhere, { strategy: strategy }) + Object.assign(redundancyWhere, { strategy }) } const videoFilterWhere = { @@ -716,6 +703,13 @@ export class VideoRedundancyModel extends Model { return undefined } + getVideoUUID () { + const video = this.getVideo() + if (!video) return undefined + + return video.uuid + } + isOwned () { return !!this.strategy }