]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/avatar/avatar.ts
Add ability to disable video comments
[github/Chocobozzz/PeerTube.git] / server / models / avatar / avatar.ts
index 2e7a8ae2c1e878c6210d3574c59789fb83e496d0..e1d4c20bccd1dd554200ad0c0e1a674e213cb0ac 100644 (file)
@@ -1,4 +1,8 @@
-import { AllowNull, Column, CreatedAt, Model, Table, UpdatedAt } from 'sequelize-typescript'
+import { join } from 'path'
+import { AfterDestroy, AllowNull, Column, CreatedAt, Model, Table, UpdatedAt } from 'sequelize-typescript'
+import { Avatar } from '../../../shared/models/avatars/avatar.model'
+import { unlinkPromise } from '../../helpers/core-utils'
+import { CONFIG, STATIC_PATHS } from '../../initializers'
 
 @Table({
   tableName: 'avatar'
@@ -14,4 +18,26 @@ export class AvatarModel extends Model<AvatarModel> {
 
   @UpdatedAt
   updatedAt: Date
+
+  @AfterDestroy
+  static removeFilesAndSendDelete (instance: AvatarModel) {
+    return instance.removeAvatar()
+  }
+
+  toFormattedJSON (): Avatar {
+    return {
+      path: this.getWebserverPath(),
+      createdAt: this.createdAt,
+      updatedAt: this.updatedAt
+    }
+  }
+
+  getWebserverPath () {
+    return join(STATIC_PATHS.AVATARS, this.filename)
+  }
+
+  removeAvatar () {
+    const avatarPath = join(CONFIG.STORAGE.AVATARS_DIR, this.filename)
+    return unlinkPromise(avatarPath)
+  }
 }