PeerTube/server/core/models/actor/actor-image.ts

179 lines
4.1 KiB
TypeScript
Raw Normal View History

import { ActivityIconObject, ActorImage, ActorImageType, type ActorImageType_Type } from '@peertube/peertube-models'
import { getLowercaseExtension } from '@peertube/peertube-node-utils'
import { MActorId, MActorImage, MActorImageFormattable } from '@server/types/models/index.js'
import { remove } from 'fs-extra/esm'
2017-12-29 19:10:13 +01:00
import { join } from 'path'
import {
AfterDestroy,
AllowNull,
BelongsTo,
Column,
CreatedAt,
Default,
ForeignKey,
2024-02-22 10:12:04 +01:00
Is, Table,
UpdatedAt
} from 'sequelize-typescript'
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc.js'
import { logger } from '../../helpers/logger.js'
import { CONFIG } from '../../initializers/config.js'
import { LAZY_STATIC_PATHS, MIMETYPES, WEBSERVER } from '../../initializers/constants.js'
2024-02-22 10:12:04 +01:00
import { SequelizeModel, buildSQLAttributes, throwIfNotValid } from '../shared/index.js'
import { ActorModel } from './actor.js'
2017-12-04 10:34:40 +01:00
2017-12-12 17:53:50 +01:00
@Table({
2021-04-06 11:35:56 +02:00
tableName: 'actorImage',
2019-08-09 11:32:40 +02:00
indexes: [
{
fields: [ 'filename' ],
unique: true
},
{
fields: [ 'actorId', 'type', 'width' ],
unique: true
2019-08-09 11:32:40 +02:00
}
]
2017-12-12 17:53:50 +01:00
})
2024-02-22 10:12:04 +01:00
export class ActorImageModel extends SequelizeModel<ActorImageModel> {
2017-12-04 10:34:40 +01:00
2017-12-12 17:53:50 +01:00
@AllowNull(false)
@Column
filename: string
2017-12-04 10:34:40 +01:00
2021-04-08 11:23:45 +02:00
@AllowNull(true)
@Default(null)
@Column
height: number
@AllowNull(true)
@Default(null)
@Column
width: number
2019-08-09 11:32:40 +02:00
@AllowNull(true)
2021-04-06 11:35:56 +02:00
@Is('ActorImageFileUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'fileUrl', true))
2019-08-09 11:32:40 +02:00
@Column
fileUrl: string
@AllowNull(false)
@Column
onDisk: boolean
2021-04-06 11:35:56 +02:00
@AllowNull(false)
@Column
type: ActorImageType_Type
2021-04-06 11:35:56 +02:00
2017-12-12 17:53:50 +01:00
@CreatedAt
createdAt: Date
2017-12-04 10:34:40 +01:00
2017-12-12 17:53:50 +01:00
@UpdatedAt
updatedAt: Date
2017-12-29 19:10:13 +01:00
@ForeignKey(() => ActorModel)
@Column
actorId: number
@BelongsTo(() => ActorModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
Actor: Awaited<ActorModel> // Remove awaited: https://github.com/sequelize/sequelize-typescript/issues/825
2017-12-29 19:10:13 +01:00
@AfterDestroy
2021-04-06 11:35:56 +02:00
static removeFilesAndSendDelete (instance: ActorImageModel) {
logger.info('Removing actor image file %s.', instance.filename)
2018-10-08 10:37:08 +02:00
// Don't block the transaction
2021-04-06 11:35:56 +02:00
instance.removeImage()
2022-07-18 11:55:13 +02:00
.catch(err => logger.error('Cannot remove actor image file %s.', instance.filename, { err }))
2017-12-29 19:10:13 +01:00
}
2023-01-09 10:29:23 +01:00
// ---------------------------------------------------------------------------
static getSQLAttributes (tableName: string, aliasPrefix = '') {
return buildSQLAttributes({
model: this,
tableName,
aliasPrefix
})
}
// ---------------------------------------------------------------------------
2019-08-09 11:32:40 +02:00
static loadByName (filename: string) {
const query = {
where: {
filename
}
}
2021-04-06 11:35:56 +02:00
return ActorImageModel.findOne(query)
2019-08-09 11:32:40 +02:00
}
static listByActor (actor: MActorId, type: ActorImageType_Type) {
const query = {
where: {
actorId: actor.id,
type
}
}
return ActorImageModel.findAll(query)
}
static getImageUrl (image: MActorImage) {
if (!image) return undefined
return WEBSERVER.URL + image.getStaticPath()
}
2021-04-06 11:35:56 +02:00
toFormattedJSON (this: MActorImageFormattable): ActorImage {
2017-12-29 19:10:13 +01:00
return {
width: this.width,
2019-08-09 11:32:40 +02:00
path: this.getStaticPath(),
2017-12-29 19:10:13 +01:00
createdAt: this.createdAt,
updatedAt: this.updatedAt
}
}
toActivityPubObject (): ActivityIconObject {
const extension = getLowercaseExtension(this.filename)
return {
type: 'Image',
mediaType: MIMETYPES.IMAGE.EXT_MIMETYPE[extension],
height: this.height,
width: this.width,
url: ActorImageModel.getImageUrl(this)
2021-04-07 17:01:29 +02:00
}
}
2021-04-07 17:01:29 +02:00
getStaticPath () {
switch (this.type) {
case ActorImageType.AVATAR:
return join(LAZY_STATIC_PATHS.AVATARS, this.filename)
case ActorImageType.BANNER:
return join(LAZY_STATIC_PATHS.BANNERS, this.filename)
2022-05-25 09:10:20 +02:00
default:
throw new Error('Unknown actor image type: ' + this.type)
}
2019-08-09 11:32:40 +02:00
}
getPath () {
2023-07-11 11:23:51 +02:00
return join(CONFIG.STORAGE.ACTOR_IMAGES_DIR, this.filename)
2017-12-29 19:10:13 +01:00
}
2021-04-06 11:35:56 +02:00
removeImage () {
2023-07-11 11:23:51 +02:00
const imagePath = join(CONFIG.STORAGE.ACTOR_IMAGES_DIR, this.filename)
2021-04-06 11:35:56 +02:00
return remove(imagePath)
2017-12-29 19:10:13 +01:00
}
2021-06-14 16:14:45 +02:00
isOwned () {
return !this.fileUrl
}
2017-12-04 10:34:40 +01:00
}