]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/avatar/avatar.ts
Update server dependencies
[github/Chocobozzz/PeerTube.git] / server / models / avatar / avatar.ts
index 3d329d8885c89fc27f7994ae49e4efe4420bd8cd..950e4b181464626a68b8058e8b4f28a9d7db94ee 100644 (file)
@@ -1,24 +1,81 @@
-import * as Sequelize from 'sequelize'
-import { addMethodsToModel } from '../utils'
-import { AvatarAttributes, AvatarInstance, AvatarMethods } from './avatar-interface'
+import { join } from 'path'
+import { AfterDestroy, AllowNull, Column, CreatedAt, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
+import { Avatar } from '../../../shared/models/avatars/avatar.model'
+import { LAZY_STATIC_PATHS } from '../../initializers/constants'
+import { logger } from '../../helpers/logger'
+import { remove } from 'fs-extra'
+import { CONFIG } from '../../initializers/config'
+import { throwIfNotValid } from '../utils'
+import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
+import { MAvatarFormattable } from '@server/typings/models'
 
-let Avatar: Sequelize.Model<AvatarInstance, AvatarAttributes>
-
-export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
-  Avatar = sequelize.define<AvatarInstance, AvatarAttributes>('Avatar',
+@Table({
+  tableName: 'avatar',
+  indexes: [
     {
-      filename: {
-        type: DataTypes.STRING,
-        allowNull: false
+      fields: [ 'filename' ],
+      unique: true
+    }
+  ]
+})
+export class AvatarModel extends Model<AvatarModel> {
+
+  @AllowNull(false)
+  @Column
+  filename: string
+
+  @AllowNull(true)
+  @Is('AvatarFileUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'fileUrl', true))
+  @Column
+  fileUrl: string
+
+  @AllowNull(false)
+  @Column
+  onDisk: boolean
+
+  @CreatedAt
+  createdAt: Date
+
+  @UpdatedAt
+  updatedAt: Date
+
+  @AfterDestroy
+  static removeFilesAndSendDelete (instance: AvatarModel) {
+    logger.info('Removing avatar file %s.', instance.filename)
+
+    // Don't block the transaction
+    instance.removeAvatar()
+      .catch(err => logger.error('Cannot remove avatar file %s.', instance.filename, err))
+  }
+
+  static loadByName (filename: string) {
+    const query = {
+      where: {
+        filename
       }
-    },
-    {}
-  )
+    }
 
-  const classMethods = []
-  addMethodsToModel(Avatar, classMethods)
+    return AvatarModel.findOne(query)
+  }
 
-  return Avatar
-}
+  toFormattedJSON (this: MAvatarFormattable): Avatar {
+    return {
+      path: this.getStaticPath(),
+      createdAt: this.createdAt,
+      updatedAt: this.updatedAt
+    }
+  }
 
-// ------------------------------ Statics ------------------------------
+  getStaticPath () {
+    return join(LAZY_STATIC_PATHS.AVATARS, this.filename)
+  }
+
+  getPath () {
+    return join(CONFIG.STORAGE.AVATARS_DIR, this.filename)
+  }
+
+  removeAvatar () {
+    const avatarPath = join(CONFIG.STORAGE.AVATARS_DIR, this.filename)
+    return remove(avatarPath)
+  }
+}