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 { AttributesOnly } from '@shared/typescript-utils'
21 import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type'
22 import { logger } from '../../helpers/logger'
23 import { CONFIG } from '../../initializers/config'
24 import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, STATIC_PATHS, WEBSERVER } from '../../initializers/constants'
25 import { VideoModel } from './video'
26 import { VideoPlaylistModel } from './video-playlist'
29 tableName: 'thumbnail',
35 fields: [ 'videoPlaylistId' ],
39 fields: [ 'filename', 'type' ],
44 export class ThumbnailModel extends Model<Partial<AttributesOnly<ThumbnailModel>>> {
65 @Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
70 automaticallyGenerated: boolean
72 @ForeignKey(() => VideoModel)
76 @BelongsTo(() => VideoModel, {
84 @ForeignKey(() => VideoPlaylistModel)
86 videoPlaylistId: number
88 @BelongsTo(() => VideoPlaylistModel, {
94 VideoPlaylist: VideoPlaylistModel
102 // If this thumbnail replaced existing one, track the old name
103 previousThumbnailFilename: string
105 private static readonly types: { [ id in ThumbnailType ]: { label: string, directory: string, staticPath: string } } = {
106 [ThumbnailType.MINIATURE]: {
108 directory: CONFIG.STORAGE.THUMBNAILS_DIR,
109 staticPath: STATIC_PATHS.THUMBNAILS
111 [ThumbnailType.PREVIEW]: {
113 directory: CONFIG.STORAGE.PREVIEWS_DIR,
114 staticPath: LAZY_STATIC_PATHS.PREVIEWS
120 static removeOldFile (instance: ThumbnailModel, options) {
121 return afterCommitIfTransaction(options.transaction, () => instance.removePreviousFilenameIfNeeded())
125 static removeFiles (instance: ThumbnailModel) {
126 logger.info('Removing %s file %s.', ThumbnailModel.types[instance.type].label, instance.filename)
128 // Don't block the transaction
129 instance.removeThumbnail()
130 .catch(err => logger.error('Cannot remove thumbnail file %s.', instance.filename, err))
133 static loadByFilename (filename: string, thumbnailType: ThumbnailType): Promise<MThumbnail> {
141 return ThumbnailModel.findOne(query)
144 static loadWithVideoByFilename (filename: string, thumbnailType: ThumbnailType): Promise<MThumbnailVideo> {
152 model: VideoModel.unscoped(),
158 return ThumbnailModel.findOne(query)
161 static buildPath (type: ThumbnailType, filename: string) {
162 const directory = ThumbnailModel.types[type].directory
164 return join(directory, filename)
167 getFileUrl (video: MVideo) {
168 const staticPath = ThumbnailModel.types[this.type].staticPath + this.filename
170 if (video.isOwned()) return WEBSERVER.URL + staticPath
176 return ThumbnailModel.buildPath(this.type, this.filename)
180 return ThumbnailModel.buildPath(this.type, this.previousThumbnailFilename)
184 return remove(this.getPath())
187 removePreviousFilenameIfNeeded () {
188 if (!this.previousThumbnailFilename) return
190 const previousPath = this.getPreviousPath()
192 .catch(err => logger.error('Cannot remove previous thumbnail file %s.', previousPath, { err }))
194 this.previousThumbnailFilename = undefined