2021-02-12 16:23:19 +01:00
|
|
|
import { remove } from 'fs-extra'
|
2019-04-17 10:07:00 +02:00
|
|
|
import { join } from 'path'
|
2019-12-17 09:55:33 +01:00
|
|
|
import {
|
|
|
|
AfterDestroy,
|
|
|
|
AllowNull,
|
2021-02-16 08:50:40 +01:00
|
|
|
BeforeCreate,
|
|
|
|
BeforeUpdate,
|
2019-12-17 09:55:33 +01:00
|
|
|
BelongsTo,
|
|
|
|
Column,
|
|
|
|
CreatedAt,
|
|
|
|
DataType,
|
|
|
|
Default,
|
|
|
|
ForeignKey,
|
|
|
|
Model,
|
|
|
|
Table,
|
|
|
|
UpdatedAt
|
|
|
|
} from 'sequelize-typescript'
|
2021-02-16 08:50:40 +01:00
|
|
|
import { afterCommitIfTransaction } from '@server/helpers/database-utils'
|
2021-02-18 11:28:00 +01:00
|
|
|
import { MThumbnail, MThumbnailVideo, MVideo } from '@server/types/models'
|
2021-12-16 18:04:16 +01:00
|
|
|
import { AttributesOnly } from '@shared/typescript-utils'
|
2021-02-12 16:23:19 +01:00
|
|
|
import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type'
|
2019-04-17 10:07:00 +02:00
|
|
|
import { logger } from '../../helpers/logger'
|
|
|
|
import { CONFIG } from '../../initializers/config'
|
2021-02-12 16:23:19 +01:00
|
|
|
import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, STATIC_PATHS, WEBSERVER } from '../../initializers/constants'
|
2019-04-17 10:07:00 +02:00
|
|
|
import { VideoModel } from './video'
|
|
|
|
import { VideoPlaylistModel } from './video-playlist'
|
|
|
|
|
|
|
|
@Table({
|
|
|
|
tableName: 'thumbnail',
|
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
fields: [ 'videoId' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'videoPlaylistId' ],
|
|
|
|
unique: true
|
2021-02-12 16:23:19 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'filename', 'type' ],
|
|
|
|
unique: true
|
2019-04-17 10:07:00 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2021-05-12 14:09:04 +02:00
|
|
|
export class ThumbnailModel extends Model<Partial<AttributesOnly<ThumbnailModel>>> {
|
2019-04-17 10:07:00 +02:00
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Column
|
|
|
|
filename: string
|
|
|
|
|
|
|
|
@AllowNull(true)
|
|
|
|
@Default(null)
|
|
|
|
@Column
|
|
|
|
height: number
|
|
|
|
|
|
|
|
@AllowNull(true)
|
|
|
|
@Default(null)
|
|
|
|
@Column
|
|
|
|
width: number
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Column
|
|
|
|
type: ThumbnailType
|
|
|
|
|
|
|
|
@AllowNull(true)
|
2019-12-17 09:55:33 +01:00
|
|
|
@Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
|
2019-04-24 10:28:57 +02:00
|
|
|
fileUrl: string
|
2019-04-17 10:07:00 +02:00
|
|
|
|
2019-08-01 16:54:24 +02:00
|
|
|
@AllowNull(true)
|
|
|
|
@Column
|
|
|
|
automaticallyGenerated: boolean
|
|
|
|
|
2019-04-17 10:07:00 +02:00
|
|
|
@ForeignKey(() => VideoModel)
|
|
|
|
@Column
|
|
|
|
videoId: number
|
|
|
|
|
|
|
|
@BelongsTo(() => VideoModel, {
|
|
|
|
foreignKey: {
|
|
|
|
allowNull: true
|
|
|
|
},
|
|
|
|
onDelete: 'CASCADE'
|
|
|
|
})
|
|
|
|
Video: VideoModel
|
|
|
|
|
|
|
|
@ForeignKey(() => VideoPlaylistModel)
|
|
|
|
@Column
|
|
|
|
videoPlaylistId: number
|
|
|
|
|
|
|
|
@BelongsTo(() => VideoPlaylistModel, {
|
|
|
|
foreignKey: {
|
|
|
|
allowNull: true
|
|
|
|
},
|
|
|
|
onDelete: 'CASCADE'
|
|
|
|
})
|
|
|
|
VideoPlaylist: VideoPlaylistModel
|
|
|
|
|
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
|
|
|
|
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date
|
|
|
|
|
2021-02-16 08:50:40 +01:00
|
|
|
// If this thumbnail replaced existing one, track the old name
|
|
|
|
previousThumbnailFilename: string
|
|
|
|
|
2020-01-31 16:56:52 +01:00
|
|
|
private static readonly types: { [ id in ThumbnailType ]: { label: string, directory: string, staticPath: string } } = {
|
2019-04-23 09:50:57 +02:00
|
|
|
[ThumbnailType.MINIATURE]: {
|
|
|
|
label: 'miniature',
|
2019-04-17 10:07:00 +02:00
|
|
|
directory: CONFIG.STORAGE.THUMBNAILS_DIR,
|
|
|
|
staticPath: STATIC_PATHS.THUMBNAILS
|
|
|
|
},
|
|
|
|
[ThumbnailType.PREVIEW]: {
|
|
|
|
label: 'preview',
|
|
|
|
directory: CONFIG.STORAGE.PREVIEWS_DIR,
|
2019-08-09 11:32:40 +02:00
|
|
|
staticPath: LAZY_STATIC_PATHS.PREVIEWS
|
2019-04-17 10:07:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 08:50:40 +01:00
|
|
|
@BeforeCreate
|
|
|
|
@BeforeUpdate
|
|
|
|
static removeOldFile (instance: ThumbnailModel, options) {
|
|
|
|
return afterCommitIfTransaction(options.transaction, () => instance.removePreviousFilenameIfNeeded())
|
|
|
|
}
|
|
|
|
|
2019-04-17 10:07:00 +02:00
|
|
|
@AfterDestroy
|
2019-08-01 16:54:24 +02:00
|
|
|
static removeFiles (instance: ThumbnailModel) {
|
2019-04-17 10:07:00 +02:00
|
|
|
logger.info('Removing %s file %s.', ThumbnailModel.types[instance.type].label, instance.filename)
|
|
|
|
|
|
|
|
// Don't block the transaction
|
|
|
|
instance.removeThumbnail()
|
|
|
|
.catch(err => logger.error('Cannot remove thumbnail file %s.', instance.filename, err))
|
|
|
|
}
|
|
|
|
|
2021-02-16 08:50:40 +01:00
|
|
|
static loadByFilename (filename: string, thumbnailType: ThumbnailType): Promise<MThumbnail> {
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
filename,
|
|
|
|
type: thumbnailType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ThumbnailModel.findOne(query)
|
|
|
|
}
|
|
|
|
|
|
|
|
static loadWithVideoByFilename (filename: string, thumbnailType: ThumbnailType): Promise<MThumbnailVideo> {
|
2019-08-09 15:04:36 +02:00
|
|
|
const query = {
|
|
|
|
where: {
|
2021-02-12 16:23:19 +01:00
|
|
|
filename,
|
|
|
|
type: thumbnailType
|
|
|
|
},
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: VideoModel.unscoped(),
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
]
|
2019-08-09 15:04:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ThumbnailModel.findOne(query)
|
|
|
|
}
|
|
|
|
|
2021-02-16 09:42:22 +01:00
|
|
|
static buildPath (type: ThumbnailType, filename: string) {
|
|
|
|
const directory = ThumbnailModel.types[type].directory
|
|
|
|
|
|
|
|
return join(directory, filename)
|
|
|
|
}
|
|
|
|
|
2021-02-18 11:28:00 +01:00
|
|
|
getFileUrl (video: MVideo) {
|
2020-01-30 11:53:38 +01:00
|
|
|
const staticPath = ThumbnailModel.types[this.type].staticPath + this.filename
|
2019-04-17 10:07:00 +02:00
|
|
|
|
2020-01-30 11:53:38 +01:00
|
|
|
if (video.isOwned()) return WEBSERVER.URL + staticPath
|
|
|
|
|
2021-02-18 10:15:11 +01:00
|
|
|
return this.fileUrl
|
2019-04-17 10:07:00 +02:00
|
|
|
}
|
|
|
|
|
2019-05-16 16:55:34 +02:00
|
|
|
getPath () {
|
2021-02-16 09:42:22 +01:00
|
|
|
return ThumbnailModel.buildPath(this.type, this.filename)
|
2019-05-16 16:55:34 +02:00
|
|
|
}
|
2019-04-17 10:07:00 +02:00
|
|
|
|
2021-02-16 08:50:40 +01:00
|
|
|
getPreviousPath () {
|
2021-02-16 09:42:22 +01:00
|
|
|
return ThumbnailModel.buildPath(this.type, this.previousThumbnailFilename)
|
2021-02-16 08:50:40 +01:00
|
|
|
}
|
|
|
|
|
2019-05-16 16:55:34 +02:00
|
|
|
removeThumbnail () {
|
|
|
|
return remove(this.getPath())
|
2019-04-17 10:07:00 +02:00
|
|
|
}
|
2021-02-16 08:50:40 +01:00
|
|
|
|
|
|
|
removePreviousFilenameIfNeeded () {
|
|
|
|
if (!this.previousThumbnailFilename) return
|
|
|
|
|
|
|
|
const previousPath = this.getPreviousPath()
|
|
|
|
remove(previousPath)
|
|
|
|
.catch(err => logger.error('Cannot remove previous thumbnail file %s.', previousPath, { err }))
|
|
|
|
|
|
|
|
this.previousThumbnailFilename = undefined
|
|
|
|
}
|
2019-04-17 10:07:00 +02:00
|
|
|
}
|