1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
import { remove } from 'fs-extra'
import { join } from 'path'
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'
import { AttributesOnly } from '@shared/typescript-utils'
import { ActorImage } from '../../../shared/models/actors/actor-image.model'
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
import { logger } from '../../helpers/logger'
import { CONFIG } from '../../initializers/config'
import { LAZY_STATIC_PATHS, MIMETYPES, WEBSERVER } from '../../initializers/constants'
import { buildSQLAttributes, throwIfNotValid } from '../shared'
import { ActorModel } from './actor'
@Table({
tableName: 'actorImage',
indexes: [
{
fields: [ 'filename' ],
unique: true
},
{
fields: [ 'actorId', 'type', 'width' ],
unique: true
}
]
})
export class ActorImageModel extends Model<Partial<AttributesOnly<ActorImageModel>>> {
@AllowNull(false)
@Column
filename: string
@AllowNull(true)
@Default(null)
@Column
height: number
@AllowNull(true)
@Default(null)
@Column
width: number
@AllowNull(true)
@Is('ActorImageFileUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'fileUrl', true))
@Column
fileUrl: string
@AllowNull(false)
@Column
onDisk: boolean
@AllowNull(false)
@Column
type: ActorImageType
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => ActorModel)
@Column
actorId: number
@BelongsTo(() => ActorModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
Actor: ActorModel
@AfterDestroy
static removeFilesAndSendDelete (instance: ActorImageModel) {
logger.info('Removing actor image file %s.', instance.filename)
// Don't block the transaction
instance.removeImage()
.catch(err => logger.error('Cannot remove actor image file %s.', instance.filename, { err }))
}
// ---------------------------------------------------------------------------
static getSQLAttributes (tableName: string, aliasPrefix = '') {
return buildSQLAttributes({
model: this,
tableName,
aliasPrefix
})
}
// ---------------------------------------------------------------------------
static loadByName (filename: string) {
const query = {
where: {
filename
}
}
return ActorImageModel.findOne(query)
}
static getImageUrl (image: MActorImage) {
if (!image) return undefined
return WEBSERVER.URL + image.getStaticPath()
}
toFormattedJSON (this: MActorImageFormattable): ActorImage {
return {
width: this.width,
path: this.getStaticPath(),
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)
}
}
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)
default:
throw new Error('Unknown actor image type: ' + this.type)
}
}
getPath () {
return join(CONFIG.STORAGE.ACTOR_IMAGES_DIR, this.filename)
}
removeImage () {
const imagePath = join(CONFIG.STORAGE.ACTOR_IMAGES_DIR, this.filename)
return remove(imagePath)
}
isOwned () {
return !this.fileUrl
}
}
|