1 import { join } from 'path'
2 import { AfterDestroy, AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
3 import { LAZY_STATIC_PATHS, STATIC_PATHS, WEBSERVER } from '../../initializers/constants'
4 import { logger } from '../../helpers/logger'
5 import { remove } from 'fs-extra'
6 import { CONFIG } from '../../initializers/config'
7 import { VideoModel } from './video'
8 import { VideoPlaylistModel } from './video-playlist'
9 import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type'
12 tableName: 'thumbnail',
18 fields: [ 'videoPlaylistId' ],
23 export class ThumbnailModel extends Model<ThumbnailModel> {
49 automaticallyGenerated: boolean
51 @ForeignKey(() => VideoModel)
55 @BelongsTo(() => VideoModel, {
63 @ForeignKey(() => VideoPlaylistModel)
65 videoPlaylistId: number
67 @BelongsTo(() => VideoPlaylistModel, {
73 VideoPlaylist: VideoPlaylistModel
81 private static types: { [ id in ThumbnailType ]: { label: string, directory: string, staticPath: string } } = {
82 [ThumbnailType.MINIATURE]: {
84 directory: CONFIG.STORAGE.THUMBNAILS_DIR,
85 staticPath: STATIC_PATHS.THUMBNAILS
87 [ThumbnailType.PREVIEW]: {
89 directory: CONFIG.STORAGE.PREVIEWS_DIR,
90 staticPath: LAZY_STATIC_PATHS.PREVIEWS
95 static removeFiles (instance: ThumbnailModel) {
96 logger.info('Removing %s file %s.', ThumbnailModel.types[instance.type].label, instance.filename)
98 // Don't block the transaction
99 instance.removeThumbnail()
100 .catch(err => logger.error('Cannot remove thumbnail file %s.', instance.filename, err))
103 static loadByName (filename: string) {
110 return ThumbnailModel.findOne(query)
113 static generateDefaultPreviewName (videoUUID: string) {
114 return videoUUID + '.jpg'
118 if (this.fileUrl) return this.fileUrl
120 const staticPath = ThumbnailModel.types[this.type].staticPath
121 return WEBSERVER.URL + staticPath + this.filename
125 const directory = ThumbnailModel.types[this.type].directory
126 return join(directory, this.filename)
130 return remove(this.getPath())