2021-04-06 05:35:56 -04:00
|
|
|
import { remove } from 'fs-extra'
|
2017-12-29 13:10:13 -05:00
|
|
|
import { join } from 'path'
|
2022-02-28 02:34:43 -05:00
|
|
|
import {
|
|
|
|
AfterDestroy,
|
|
|
|
AllowNull,
|
|
|
|
BelongsTo,
|
|
|
|
Column,
|
|
|
|
CreatedAt,
|
|
|
|
Default,
|
|
|
|
ForeignKey,
|
|
|
|
Is,
|
|
|
|
Model,
|
|
|
|
Table,
|
|
|
|
UpdatedAt
|
|
|
|
} from 'sequelize-typescript'
|
|
|
|
import { MActorImage, MActorImageFormattable } from '@server/types/models'
|
|
|
|
import { getLowercaseExtension } from '@shared/core-utils'
|
|
|
|
import { ActivityIconObject, ActorImageType } from '@shared/models'
|
2021-12-16 12:04:16 -05:00
|
|
|
import { AttributesOnly } from '@shared/typescript-utils'
|
2021-04-06 05:35:56 -04:00
|
|
|
import { ActorImage } from '../../../shared/models/actors/actor-image.model'
|
|
|
|
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
|
2018-07-30 11:02:40 -04:00
|
|
|
import { logger } from '../../helpers/logger'
|
2019-04-11 05:33:44 -04:00
|
|
|
import { CONFIG } from '../../initializers/config'
|
2022-02-28 02:34:43 -05:00
|
|
|
import { LAZY_STATIC_PATHS, MIMETYPES, WEBSERVER } from '../../initializers/constants'
|
2023-01-10 05:09:30 -05:00
|
|
|
import { buildSQLAttributes, throwIfNotValid } from '../shared'
|
2022-02-28 02:34:43 -05:00
|
|
|
import { ActorModel } from './actor'
|
2017-12-04 04:34:40 -05:00
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
@Table({
|
2021-04-06 05:35:56 -04:00
|
|
|
tableName: 'actorImage',
|
2019-08-09 05:32:40 -04:00
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
fields: [ 'filename' ],
|
|
|
|
unique: true
|
2022-02-28 02:34:43 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'actorId', 'type', 'width' ],
|
|
|
|
unique: true
|
2019-08-09 05:32:40 -04:00
|
|
|
}
|
|
|
|
]
|
2017-12-12 11:53:50 -05:00
|
|
|
})
|
2021-05-12 08:09:04 -04:00
|
|
|
export class ActorImageModel extends Model<Partial<AttributesOnly<ActorImageModel>>> {
|
2017-12-04 04:34:40 -05:00
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
@AllowNull(false)
|
|
|
|
@Column
|
|
|
|
filename: string
|
2017-12-04 04:34:40 -05:00
|
|
|
|
2021-04-08 05:23:45 -04:00
|
|
|
@AllowNull(true)
|
|
|
|
@Default(null)
|
|
|
|
@Column
|
|
|
|
height: number
|
|
|
|
|
|
|
|
@AllowNull(true)
|
|
|
|
@Default(null)
|
|
|
|
@Column
|
|
|
|
width: number
|
|
|
|
|
2019-08-09 05:32:40 -04:00
|
|
|
@AllowNull(true)
|
2021-04-06 05:35:56 -04:00
|
|
|
@Is('ActorImageFileUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'fileUrl', true))
|
2019-08-09 05:32:40 -04:00
|
|
|
@Column
|
|
|
|
fileUrl: string
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Column
|
|
|
|
onDisk: boolean
|
|
|
|
|
2021-04-06 05:35:56 -04:00
|
|
|
@AllowNull(false)
|
|
|
|
@Column
|
|
|
|
type: ActorImageType
|
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
2017-12-04 04:34:40 -05:00
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date
|
2017-12-29 13:10:13 -05:00
|
|
|
|
2022-02-28 02:34:43 -05:00
|
|
|
@ForeignKey(() => ActorModel)
|
|
|
|
@Column
|
|
|
|
actorId: number
|
|
|
|
|
|
|
|
@BelongsTo(() => ActorModel, {
|
|
|
|
foreignKey: {
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'CASCADE'
|
|
|
|
})
|
|
|
|
Actor: ActorModel
|
|
|
|
|
2017-12-29 13:10:13 -05:00
|
|
|
@AfterDestroy
|
2021-04-06 05:35:56 -04:00
|
|
|
static removeFilesAndSendDelete (instance: ActorImageModel) {
|
|
|
|
logger.info('Removing actor image file %s.', instance.filename)
|
2018-10-08 04:37:08 -04:00
|
|
|
|
|
|
|
// Don't block the transaction
|
2021-04-06 05:35:56 -04:00
|
|
|
instance.removeImage()
|
2022-07-18 05:55:13 -04:00
|
|
|
.catch(err => logger.error('Cannot remove actor image file %s.', instance.filename, { err }))
|
2017-12-29 13:10:13 -05:00
|
|
|
}
|
|
|
|
|
2023-01-09 04:29:23 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static getSQLAttributes (tableName: string, aliasPrefix = '') {
|
|
|
|
return buildSQLAttributes({
|
|
|
|
model: this,
|
|
|
|
tableName,
|
|
|
|
aliasPrefix
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2019-08-09 05:32:40 -04:00
|
|
|
static loadByName (filename: string) {
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
filename
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 05:35:56 -04:00
|
|
|
return ActorImageModel.findOne(query)
|
2019-08-09 05:32:40 -04:00
|
|
|
}
|
|
|
|
|
2022-02-28 02:34:43 -05:00
|
|
|
static getImageUrl (image: MActorImage) {
|
|
|
|
if (!image) return undefined
|
|
|
|
|
|
|
|
return WEBSERVER.URL + image.getStaticPath()
|
|
|
|
}
|
|
|
|
|
2021-04-06 05:35:56 -04:00
|
|
|
toFormattedJSON (this: MActorImageFormattable): ActorImage {
|
2017-12-29 13:10:13 -05:00
|
|
|
return {
|
2022-02-28 02:34:43 -05:00
|
|
|
width: this.width,
|
2019-08-09 05:32:40 -04:00
|
|
|
path: this.getStaticPath(),
|
2017-12-29 13:10:13 -05:00
|
|
|
createdAt: this.createdAt,
|
|
|
|
updatedAt: this.updatedAt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 02:34:43 -05:00
|
|
|
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 11:01:29 -04:00
|
|
|
}
|
2022-02-28 02:34:43 -05:00
|
|
|
}
|
2021-04-07 11:01:29 -04:00
|
|
|
|
2022-02-28 02:34:43 -05: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 03:10:20 -04:00
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Error('Unknown actor image type: ' + this.type)
|
2022-02-28 02:34:43 -05:00
|
|
|
}
|
2019-08-09 05:32:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
getPath () {
|
2021-04-06 05:35:56 -04:00
|
|
|
return join(CONFIG.STORAGE.ACTOR_IMAGES, this.filename)
|
2017-12-29 13:10:13 -05:00
|
|
|
}
|
|
|
|
|
2021-04-06 05:35:56 -04:00
|
|
|
removeImage () {
|
|
|
|
const imagePath = join(CONFIG.STORAGE.ACTOR_IMAGES, this.filename)
|
|
|
|
return remove(imagePath)
|
2017-12-29 13:10:13 -05:00
|
|
|
}
|
2021-06-14 10:14:45 -04:00
|
|
|
|
|
|
|
isOwned () {
|
|
|
|
return !this.fileUrl
|
|
|
|
}
|
2017-12-04 04:34:40 -05:00
|
|
|
}
|