2021-04-06 04:35:56 -05:00
|
|
|
import { remove } from 'fs-extra'
|
2017-12-29 12:10:13 -06:00
|
|
|
import { join } from 'path'
|
2021-04-08 04:23:45 -05:00
|
|
|
import { AfterDestroy, AllowNull, Column, CreatedAt, Default, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
|
2021-04-06 04:35:56 -05:00
|
|
|
import { MActorImageFormattable } from '@server/types/models'
|
2021-05-12 07:09:04 -05:00
|
|
|
import { AttributesOnly } from '@shared/core-utils'
|
2021-04-06 04:35:56 -05:00
|
|
|
import { ActorImageType } from '@shared/models'
|
|
|
|
import { ActorImage } from '../../../shared/models/actors/actor-image.model'
|
|
|
|
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
|
2018-07-30 10:02:40 -05:00
|
|
|
import { logger } from '../../helpers/logger'
|
2019-04-11 04:33:44 -05:00
|
|
|
import { CONFIG } from '../../initializers/config'
|
2021-04-06 04:35:56 -05:00
|
|
|
import { LAZY_STATIC_PATHS } from '../../initializers/constants'
|
2019-08-09 04:32:40 -05:00
|
|
|
import { throwIfNotValid } from '../utils'
|
2017-12-04 03:34:40 -06:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@Table({
|
2021-04-06 04:35:56 -05:00
|
|
|
tableName: 'actorImage',
|
2019-08-09 04:32:40 -05:00
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
fields: [ 'filename' ],
|
|
|
|
unique: true
|
|
|
|
}
|
|
|
|
]
|
2017-12-12 10:53:50 -06:00
|
|
|
})
|
2021-05-12 07:09:04 -05:00
|
|
|
export class ActorImageModel extends Model<Partial<AttributesOnly<ActorImageModel>>> {
|
2017-12-04 03:34:40 -06:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@AllowNull(false)
|
|
|
|
@Column
|
|
|
|
filename: string
|
2017-12-04 03:34:40 -06:00
|
|
|
|
2021-04-08 04:23:45 -05:00
|
|
|
@AllowNull(true)
|
|
|
|
@Default(null)
|
|
|
|
@Column
|
|
|
|
height: number
|
|
|
|
|
|
|
|
@AllowNull(true)
|
|
|
|
@Default(null)
|
|
|
|
@Column
|
|
|
|
width: number
|
|
|
|
|
2019-08-09 04:32:40 -05:00
|
|
|
@AllowNull(true)
|
2021-04-06 04:35:56 -05:00
|
|
|
@Is('ActorImageFileUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'fileUrl', true))
|
2019-08-09 04:32:40 -05:00
|
|
|
@Column
|
|
|
|
fileUrl: string
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Column
|
|
|
|
onDisk: boolean
|
|
|
|
|
2021-04-06 04:35:56 -05:00
|
|
|
@AllowNull(false)
|
|
|
|
@Column
|
|
|
|
type: ActorImageType
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
2017-12-04 03:34:40 -06:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date
|
2017-12-29 12:10:13 -06:00
|
|
|
|
|
|
|
@AfterDestroy
|
2021-04-06 04:35:56 -05:00
|
|
|
static removeFilesAndSendDelete (instance: ActorImageModel) {
|
|
|
|
logger.info('Removing actor image file %s.', instance.filename)
|
2018-10-08 03:37:08 -05:00
|
|
|
|
|
|
|
// Don't block the transaction
|
2021-04-06 04:35:56 -05:00
|
|
|
instance.removeImage()
|
|
|
|
.catch(err => logger.error('Cannot remove actor image file %s.', instance.filename, err))
|
2017-12-29 12:10:13 -06:00
|
|
|
}
|
|
|
|
|
2019-08-09 04:32:40 -05:00
|
|
|
static loadByName (filename: string) {
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
filename
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 04:35:56 -05:00
|
|
|
return ActorImageModel.findOne(query)
|
2019-08-09 04:32:40 -05:00
|
|
|
}
|
|
|
|
|
2021-04-06 04:35:56 -05:00
|
|
|
toFormattedJSON (this: MActorImageFormattable): ActorImage {
|
2017-12-29 12:10:13 -06:00
|
|
|
return {
|
2019-08-09 04:32:40 -05:00
|
|
|
path: this.getStaticPath(),
|
2017-12-29 12:10:13 -06:00
|
|
|
createdAt: this.createdAt,
|
|
|
|
updatedAt: this.updatedAt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 04:32:40 -05:00
|
|
|
getStaticPath () {
|
2021-04-07 10:01:29 -05:00
|
|
|
if (this.type === ActorImageType.AVATAR) {
|
|
|
|
return join(LAZY_STATIC_PATHS.AVATARS, this.filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
return join(LAZY_STATIC_PATHS.BANNERS, this.filename)
|
2019-08-09 04:32:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
getPath () {
|
2021-04-06 04:35:56 -05:00
|
|
|
return join(CONFIG.STORAGE.ACTOR_IMAGES, this.filename)
|
2017-12-29 12:10:13 -06:00
|
|
|
}
|
|
|
|
|
2021-04-06 04:35:56 -05:00
|
|
|
removeImage () {
|
|
|
|
const imagePath = join(CONFIG.STORAGE.ACTOR_IMAGES, this.filename)
|
|
|
|
return remove(imagePath)
|
2017-12-29 12:10:13 -06:00
|
|
|
}
|
2021-06-14 09:14:45 -05:00
|
|
|
|
|
|
|
isOwned () {
|
|
|
|
return !this.fileUrl
|
|
|
|
}
|
2017-12-04 03:34:40 -06:00
|
|
|
}
|