1 import { join } from 'path'
14 } from 'sequelize-typescript'
15 import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, STATIC_PATHS, WEBSERVER } from '../../initializers/constants'
16 import { logger } from '../../helpers/logger'
17 import { remove } from 'fs-extra'
18 import { CONFIG } from '../../initializers/config'
19 import { VideoModel } from './video'
20 import { VideoPlaylistModel } from './video-playlist'
21 import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type'
24 tableName: 'thumbnail',
30 fields: [ 'videoPlaylistId' ],
35 export class ThumbnailModel extends Model<ThumbnailModel> {
56 @Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
61 automaticallyGenerated: boolean
63 @ForeignKey(() => VideoModel)
67 @BelongsTo(() => VideoModel, {
75 @ForeignKey(() => VideoPlaylistModel)
77 videoPlaylistId: number
79 @BelongsTo(() => VideoPlaylistModel, {
85 VideoPlaylist: VideoPlaylistModel
93 private static types: { [ id in ThumbnailType ]: { label: string, directory: string, staticPath: string } } = {
94 [ThumbnailType.MINIATURE]: {
96 directory: CONFIG.STORAGE.THUMBNAILS_DIR,
97 staticPath: STATIC_PATHS.THUMBNAILS
99 [ThumbnailType.PREVIEW]: {
101 directory: CONFIG.STORAGE.PREVIEWS_DIR,
102 staticPath: LAZY_STATIC_PATHS.PREVIEWS
107 static removeFiles (instance: ThumbnailModel) {
108 logger.info('Removing %s file %s.', ThumbnailModel.types[instance.type].label, instance.filename)
110 // Don't block the transaction
111 instance.removeThumbnail()
112 .catch(err => logger.error('Cannot remove thumbnail file %s.', instance.filename, err))
115 static loadByName (filename: string) {
122 return ThumbnailModel.findOne(query)
125 static generateDefaultPreviewName (videoUUID: string) {
126 return videoUUID + '.jpg'
130 if (this.fileUrl) return this.fileUrl
132 const staticPath = ThumbnailModel.types[this.type].staticPath
133 return WEBSERVER.URL + staticPath + this.filename
137 const directory = ThumbnailModel.types[this.type].directory
138 return join(directory, this.filename)
142 return remove(this.getPath())