From a35a22797c99f17924347da9a226068c3dbe4787 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 16 Feb 2021 08:50:40 +0100 Subject: Remove previous thumbnail if needed --- server/models/video/thumbnail.ts | 42 +++++++++++++++++++++++++++++++++-- server/models/video/video-playlist.ts | 1 + 2 files changed, 41 insertions(+), 2 deletions(-) (limited to 'server/models') diff --git a/server/models/video/thumbnail.ts b/server/models/video/thumbnail.ts index 3cad6c668..3d885f654 100644 --- a/server/models/video/thumbnail.ts +++ b/server/models/video/thumbnail.ts @@ -3,6 +3,8 @@ import { join } from 'path' import { AfterDestroy, AllowNull, + BeforeCreate, + BeforeUpdate, BelongsTo, Column, CreatedAt, @@ -14,7 +16,8 @@ import { UpdatedAt } from 'sequelize-typescript' import { buildRemoteVideoBaseUrl } from '@server/helpers/activitypub' -import { MThumbnailVideo, MVideoAccountLight } from '@server/types/models' +import { afterCommitIfTransaction } from '@server/helpers/database-utils' +import { MThumbnail, MThumbnailVideo, MVideoAccountLight } from '@server/types/models' import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type' import { logger } from '../../helpers/logger' import { CONFIG } from '../../initializers/config' @@ -96,6 +99,9 @@ export class ThumbnailModel extends Model { @UpdatedAt updatedAt: Date + // If this thumbnail replaced existing one, track the old name + previousThumbnailFilename: string + private static readonly types: { [ id in ThumbnailType ]: { label: string, directory: string, staticPath: string } } = { [ThumbnailType.MINIATURE]: { label: 'miniature', @@ -109,6 +115,12 @@ export class ThumbnailModel extends Model { } } + @BeforeCreate + @BeforeUpdate + static removeOldFile (instance: ThumbnailModel, options) { + return afterCommitIfTransaction(options.transaction, () => instance.removePreviousFilenameIfNeeded()) + } + @AfterDestroy static removeFiles (instance: ThumbnailModel) { logger.info('Removing %s file %s.', ThumbnailModel.types[instance.type].label, instance.filename) @@ -118,7 +130,18 @@ export class ThumbnailModel extends Model { .catch(err => logger.error('Cannot remove thumbnail file %s.', instance.filename, err)) } - static loadWithVideoByName (filename: string, thumbnailType: ThumbnailType): Promise { + static loadByFilename (filename: string, thumbnailType: ThumbnailType): Promise { + const query = { + where: { + filename, + type: thumbnailType + } + } + + return ThumbnailModel.findOne(query) + } + + static loadWithVideoByFilename (filename: string, thumbnailType: ThumbnailType): Promise { const query = { where: { filename, @@ -150,7 +173,22 @@ export class ThumbnailModel extends Model { return join(directory, this.filename) } + getPreviousPath () { + const directory = ThumbnailModel.types[this.type].directory + return join(directory, this.previousThumbnailFilename) + } + removeThumbnail () { return remove(this.getPath()) } + + removePreviousFilenameIfNeeded () { + if (!this.previousThumbnailFilename) return + + const previousPath = this.getPreviousPath() + remove(previousPath) + .catch(err => logger.error('Cannot remove previous thumbnail file %s.', previousPath, { err })) + + this.previousThumbnailFilename = undefined + } } diff --git a/server/models/video/video-playlist.ts b/server/models/video/video-playlist.ts index 9e6ff1f81..49a406608 100644 --- a/server/models/video/video-playlist.ts +++ b/server/models/video/video-playlist.ts @@ -17,6 +17,7 @@ import { Table, UpdatedAt } from 'sequelize-typescript' +import { v4 as uuidv4 } from 'uuid' import { MAccountId, MChannelId } from '@server/types/models' import { ActivityIconObject } from '../../../shared/models/activitypub/objects' import { PlaylistObject } from '../../../shared/models/activitypub/objects/playlist-object' -- cgit v1.2.3