1 import { join } from 'path'
2 import { AfterDestroy, AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
3 import { 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> {
47 @ForeignKey(() => VideoModel)
51 @BelongsTo(() => VideoModel, {
59 @ForeignKey(() => VideoPlaylistModel)
61 videoPlaylistId: number
63 @BelongsTo(() => VideoPlaylistModel, {
69 VideoPlaylist: VideoPlaylistModel
77 private static types: { [ id in ThumbnailType ]: { label: string, directory: string, staticPath: string } } = {
78 [ThumbnailType.MINIATURE]: {
80 directory: CONFIG.STORAGE.THUMBNAILS_DIR,
81 staticPath: STATIC_PATHS.THUMBNAILS
83 [ThumbnailType.PREVIEW]: {
85 directory: CONFIG.STORAGE.PREVIEWS_DIR,
86 staticPath: STATIC_PATHS.PREVIEWS
91 static removeFilesAndSendDelete (instance: ThumbnailModel) {
92 logger.info('Removing %s file %s.', ThumbnailModel.types[instance.type].label, instance.filename)
94 // Don't block the transaction
95 instance.removeThumbnail()
96 .catch(err => logger.error('Cannot remove thumbnail file %s.', instance.filename, err))
99 static generateDefaultPreviewName (videoUUID: string) {
100 return videoUUID + '.jpg'
104 if (this.fileUrl) return this.fileUrl
106 const staticPath = ThumbnailModel.types[this.type].staticPath
107 return WEBSERVER.URL + staticPath + this.filename
111 const directory = ThumbnailModel.types[this.type].directory
112 const thumbnailPath = join(directory, this.filename)
114 return remove(thumbnailPath)