1 import { remove } from 'fs-extra'
2 import { join } from 'path'
17 } from 'sequelize-typescript'
18 import { afterCommitIfTransaction } from '@server/helpers/database-utils'
19 import { MThumbnail, MThumbnailVideo, MVideo } from '@server/types/models'
20 import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type'
21 import { logger } from '../../helpers/logger'
22 import { CONFIG } from '../../initializers/config'
23 import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, STATIC_PATHS, WEBSERVER } from '../../initializers/constants'
24 import { VideoModel } from './video'
25 import { VideoPlaylistModel } from './video-playlist'
28 tableName: 'thumbnail',
34 fields: [ 'videoPlaylistId' ],
38 fields: [ 'filename', 'type' ],
43 export class ThumbnailModel extends Model {
64 @Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
69 automaticallyGenerated: boolean
71 @ForeignKey(() => VideoModel)
75 @BelongsTo(() => VideoModel, {
83 @ForeignKey(() => VideoPlaylistModel)
85 videoPlaylistId: number
87 @BelongsTo(() => VideoPlaylistModel, {
93 VideoPlaylist: VideoPlaylistModel
101 // If this thumbnail replaced existing one, track the old name
102 previousThumbnailFilename: string
104 private static readonly types: { [ id in ThumbnailType ]: { label: string, directory: string, staticPath: string } } = {
105 [ThumbnailType.MINIATURE]: {
107 directory: CONFIG.STORAGE.THUMBNAILS_DIR,
108 staticPath: STATIC_PATHS.THUMBNAILS
110 [ThumbnailType.PREVIEW]: {
112 directory: CONFIG.STORAGE.PREVIEWS_DIR,
113 staticPath: LAZY_STATIC_PATHS.PREVIEWS
119 static removeOldFile (instance: ThumbnailModel, options) {
120 return afterCommitIfTransaction(options.transaction, () => instance.removePreviousFilenameIfNeeded())
124 static removeFiles (instance: ThumbnailModel) {
125 logger.info('Removing %s file %s.', ThumbnailModel.types[instance.type].label, instance.filename)
127 // Don't block the transaction
128 instance.removeThumbnail()
129 .catch(err => logger.error('Cannot remove thumbnail file %s.', instance.filename, err))
132 static loadByFilename (filename: string, thumbnailType: ThumbnailType): Promise<MThumbnail> {
140 return ThumbnailModel.findOne(query)
143 static loadWithVideoByFilename (filename: string, thumbnailType: ThumbnailType): Promise<MThumbnailVideo> {
151 model: VideoModel.unscoped(),
157 return ThumbnailModel.findOne(query)
160 static buildPath (type: ThumbnailType, filename: string) {
161 const directory = ThumbnailModel.types[type].directory
163 return join(directory, filename)
166 getFileUrl (video: MVideo) {
167 const staticPath = ThumbnailModel.types[this.type].staticPath + this.filename
169 if (video.isOwned()) return WEBSERVER.URL + staticPath
175 return ThumbnailModel.buildPath(this.type, this.filename)
179 return ThumbnailModel.buildPath(this.type, this.previousThumbnailFilename)
183 return remove(this.getPath())
186 removePreviousFilenameIfNeeded () {
187 if (!this.previousThumbnailFilename) return
189 const previousPath = this.getPreviousPath()
191 .catch(err => logger.error('Cannot remove previous thumbnail file %s.', previousPath, { err }))
193 this.previousThumbnailFilename = undefined