X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=server%2Fmodels%2Fredundancy%2Fvideo-redundancy.ts;h=cbfc7f7fa085c5ef75e3049cd070c8de73f37348;hb=b83b8dd5aef03084133c5983de6f312e7d1654b8;hp=3c87ec2c13470b70999a98ddf39d723d7c5a37bc;hpb=4a08f6692780fa5147d85126af3089fc7891e436;p=oweals%2Fpeertube.git diff --git a/server/models/redundancy/video-redundancy.ts b/server/models/redundancy/video-redundancy.ts index 3c87ec2c1..cbfc7f7fa 100644 --- a/server/models/redundancy/video-redundancy.ts +++ b/server/models/redundancy/video-redundancy.ts @@ -1,6 +1,6 @@ import { - AfterDestroy, AllowNull, + BeforeDestroy, BelongsTo, Column, CreatedAt, @@ -115,19 +115,28 @@ export class VideoRedundancyModel extends Model { }) Actor: ActorModel - @AfterDestroy - static removeFile (instance: VideoRedundancyModel) { + @BeforeDestroy + static async removeFile (instance: VideoRedundancyModel) { // Not us if (!instance.strategy) return - logger.info('Removing duplicated video file %s-%s.', instance.VideoFile.Video.uuid, instance.VideoFile.resolution) + const videoFile = await VideoFileModel.loadWithVideo(instance.videoFileId) - return instance.VideoFile.Video.removeFile(instance.VideoFile) + const logIdentifier = `${videoFile.Video.uuid}-${videoFile.resolution}` + logger.info('Removing duplicated video file %s.', logIdentifier) + + videoFile.Video.removeFile(videoFile) + .catch(err => logger.error('Cannot delete %s files.', logIdentifier, { err })) + + return undefined } - static loadByFileId (videoFileId: number) { + static async loadLocalByFileId (videoFileId: number) { + const actor = await getServerActor() + const query = { where: { + actorId: actor.id, videoFileId } } @@ -146,6 +155,38 @@ export class VideoRedundancyModel extends Model { return VideoRedundancyModel.findOne(query) } + static async isLocalByVideoUUIDExists (uuid: string) { + const actor = await getServerActor() + + const query = { + raw: true, + attributes: [ 'id' ], + where: { + actorId: actor.id + }, + include: [ + { + attributes: [ ], + model: VideoFileModel, + required: true, + include: [ + { + attributes: [ ], + model: VideoModel, + required: true, + where: { + uuid + } + } + ] + } + ] + } + + return VideoRedundancyModel.findOne(query) + .then(r => !!r) + } + static async getVideoSample (p: Bluebird) { const rows = await p const ids = rows.map(r => r.id) @@ -286,6 +327,47 @@ export class VideoRedundancyModel extends Model { return VideoRedundancyModel.scope([ ScopeNames.WITH_VIDEO ]).findAll(query) } + static async listLocalOfServer (serverId: number) { + const actor = await getServerActor() + + const query = { + where: { + actorId: actor.id + }, + include: [ + { + model: VideoFileModel, + required: true, + include: [ + { + model: VideoModel, + required: true, + include: [ + { + attributes: [], + model: VideoChannelModel.unscoped(), + required: true, + include: [ + { + attributes: [], + model: ActorModel.unscoped(), + required: true, + where: { + serverId + } + } + ] + } + ] + } + ] + } + ] + } + + return VideoRedundancyModel.findAll(query) + } + static async getStats (strategy: VideoRedundancyStrategy) { const actor = await getServerActor() @@ -326,6 +408,7 @@ export class VideoRedundancyModel extends Model { url: { type: 'Link', mimeType: VIDEO_EXT_MIMETYPE[ this.VideoFile.extname ] as any, + mediaType: VIDEO_EXT_MIMETYPE[ this.VideoFile.extname ] as any, href: this.fileUrl, height: this.VideoFile.resolution, size: this.VideoFile.size,