X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Factor%2Factor-image.ts;h=9c34a0101acf46c778f548933f7ed924d756d5a0;hb=5e47f6ab984a7d00782e4c7030afffa1ba480add;hp=98a7f6fbaa575d7bc7d6c68d334eb5c497897220;hpb=79db409a41bd28fd2773626c9a93b5d326a38bc0;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/actor/actor-image.ts b/server/models/actor/actor-image.ts index 98a7f6fba..9c34a0101 100644 --- a/server/models/actor/actor-image.ts +++ b/server/models/actor/actor-image.ts @@ -1,15 +1,29 @@ import { remove } from 'fs-extra' import { join } from 'path' -import { AfterDestroy, AllowNull, Column, CreatedAt, Default, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' -import { MActorImageFormattable } from '@server/types/models' -import { AttributesOnly } from '@shared/core-utils' -import { ActorImageType } from '@shared/models' +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 } from '../../initializers/constants' -import { throwIfNotValid } from '../utils' +import { LAZY_STATIC_PATHS, MIMETYPES, WEBSERVER } from '../../initializers/constants' +import { buildSQLAttributes, throwIfNotValid } from '../shared' +import { ActorModel } from './actor' @Table({ tableName: 'actorImage', @@ -17,6 +31,10 @@ import { throwIfNotValid } from '../utils' { fields: [ 'filename' ], unique: true + }, + { + fields: [ 'actorId', 'type', 'width' ], + unique: true } ] }) @@ -55,15 +73,39 @@ export class ActorImageModel extends Model 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)) + .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: { @@ -74,20 +116,44 @@ export class ActorImageModel extends Model