aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-02-16 08:50:40 +0100
committerChocobozzz <chocobozzz@cpy.re>2021-02-16 10:36:44 +0100
commita35a22797c99f17924347da9a226068c3dbe4787 (patch)
treeaffb713929145f90f6bda8828ded3ac2f4f73b19 /server/models/video
parent6302d599cdf98b5a5363a2a1dcdc266447950191 (diff)
downloadPeerTube-a35a22797c99f17924347da9a226068c3dbe4787.tar.gz
PeerTube-a35a22797c99f17924347da9a226068c3dbe4787.tar.zst
PeerTube-a35a22797c99f17924347da9a226068c3dbe4787.zip
Remove previous thumbnail if needed
Diffstat (limited to 'server/models/video')
-rw-r--r--server/models/video/thumbnail.ts42
-rw-r--r--server/models/video/video-playlist.ts1
2 files changed, 41 insertions, 2 deletions
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'
3import { 3import {
4 AfterDestroy, 4 AfterDestroy,
5 AllowNull, 5 AllowNull,
6 BeforeCreate,
7 BeforeUpdate,
6 BelongsTo, 8 BelongsTo,
7 Column, 9 Column,
8 CreatedAt, 10 CreatedAt,
@@ -14,7 +16,8 @@ import {
14 UpdatedAt 16 UpdatedAt
15} from 'sequelize-typescript' 17} from 'sequelize-typescript'
16import { buildRemoteVideoBaseUrl } from '@server/helpers/activitypub' 18import { buildRemoteVideoBaseUrl } from '@server/helpers/activitypub'
17import { MThumbnailVideo, MVideoAccountLight } from '@server/types/models' 19import { afterCommitIfTransaction } from '@server/helpers/database-utils'
20import { MThumbnail, MThumbnailVideo, MVideoAccountLight } from '@server/types/models'
18import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type' 21import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type'
19import { logger } from '../../helpers/logger' 22import { logger } from '../../helpers/logger'
20import { CONFIG } from '../../initializers/config' 23import { CONFIG } from '../../initializers/config'
@@ -96,6 +99,9 @@ export class ThumbnailModel extends Model {
96 @UpdatedAt 99 @UpdatedAt
97 updatedAt: Date 100 updatedAt: Date
98 101
102 // If this thumbnail replaced existing one, track the old name
103 previousThumbnailFilename: string
104
99 private static readonly types: { [ id in ThumbnailType ]: { label: string, directory: string, staticPath: string } } = { 105 private static readonly types: { [ id in ThumbnailType ]: { label: string, directory: string, staticPath: string } } = {
100 [ThumbnailType.MINIATURE]: { 106 [ThumbnailType.MINIATURE]: {
101 label: 'miniature', 107 label: 'miniature',
@@ -109,6 +115,12 @@ export class ThumbnailModel extends Model {
109 } 115 }
110 } 116 }
111 117
118 @BeforeCreate
119 @BeforeUpdate
120 static removeOldFile (instance: ThumbnailModel, options) {
121 return afterCommitIfTransaction(options.transaction, () => instance.removePreviousFilenameIfNeeded())
122 }
123
112 @AfterDestroy 124 @AfterDestroy
113 static removeFiles (instance: ThumbnailModel) { 125 static removeFiles (instance: ThumbnailModel) {
114 logger.info('Removing %s file %s.', ThumbnailModel.types[instance.type].label, instance.filename) 126 logger.info('Removing %s file %s.', ThumbnailModel.types[instance.type].label, instance.filename)
@@ -118,7 +130,18 @@ export class ThumbnailModel extends Model {
118 .catch(err => logger.error('Cannot remove thumbnail file %s.', instance.filename, err)) 130 .catch(err => logger.error('Cannot remove thumbnail file %s.', instance.filename, err))
119 } 131 }
120 132
121 static loadWithVideoByName (filename: string, thumbnailType: ThumbnailType): Promise<MThumbnailVideo> { 133 static loadByFilename (filename: string, thumbnailType: ThumbnailType): Promise<MThumbnail> {
134 const query = {
135 where: {
136 filename,
137 type: thumbnailType
138 }
139 }
140
141 return ThumbnailModel.findOne(query)
142 }
143
144 static loadWithVideoByFilename (filename: string, thumbnailType: ThumbnailType): Promise<MThumbnailVideo> {
122 const query = { 145 const query = {
123 where: { 146 where: {
124 filename, 147 filename,
@@ -150,7 +173,22 @@ export class ThumbnailModel extends Model {
150 return join(directory, this.filename) 173 return join(directory, this.filename)
151 } 174 }
152 175
176 getPreviousPath () {
177 const directory = ThumbnailModel.types[this.type].directory
178 return join(directory, this.previousThumbnailFilename)
179 }
180
153 removeThumbnail () { 181 removeThumbnail () {
154 return remove(this.getPath()) 182 return remove(this.getPath())
155 } 183 }
184
185 removePreviousFilenameIfNeeded () {
186 if (!this.previousThumbnailFilename) return
187
188 const previousPath = this.getPreviousPath()
189 remove(previousPath)
190 .catch(err => logger.error('Cannot remove previous thumbnail file %s.', previousPath, { err }))
191
192 this.previousThumbnailFilename = undefined
193 }
156} 194}
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 {
17 Table, 17 Table,
18 UpdatedAt 18 UpdatedAt
19} from 'sequelize-typescript' 19} from 'sequelize-typescript'
20import { v4 as uuidv4 } from 'uuid'
20import { MAccountId, MChannelId } from '@server/types/models' 21import { MAccountId, MChannelId } from '@server/types/models'
21import { ActivityIconObject } from '../../../shared/models/activitypub/objects' 22import { ActivityIconObject } from '../../../shared/models/activitypub/objects'
22import { PlaylistObject } from '../../../shared/models/activitypub/objects/playlist-object' 23import { PlaylistObject } from '../../../shared/models/activitypub/objects/playlist-object'