aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/avatar/avatar.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-08-09 11:32:40 +0200
committerChocobozzz <me@florianbigard.com>2019-08-09 11:32:40 +0200
commit557b13ae24019d9ab214bbea7eaa0f892c8f4b05 (patch)
treeaa32396531acf93e3dfdb29880177813039ed77f /server/models/avatar/avatar.ts
parentc5407d7046168abb4098df1408e7aa84519cb61a (diff)
downloadPeerTube-557b13ae24019d9ab214bbea7eaa0f892c8f4b05.tar.gz
PeerTube-557b13ae24019d9ab214bbea7eaa0f892c8f4b05.tar.zst
PeerTube-557b13ae24019d9ab214bbea7eaa0f892c8f4b05.zip
Lazy load avatars
Diffstat (limited to 'server/models/avatar/avatar.ts')
-rw-r--r--server/models/avatar/avatar.ts43
1 files changed, 37 insertions, 6 deletions
diff --git a/server/models/avatar/avatar.ts b/server/models/avatar/avatar.ts
index aaf1b8bd9..7a370bcd3 100644
--- a/server/models/avatar/avatar.ts
+++ b/server/models/avatar/avatar.ts
@@ -1,13 +1,21 @@
1import { join } from 'path' 1import { join } from 'path'
2import { AfterDestroy, AllowNull, Column, CreatedAt, Model, Table, UpdatedAt } from 'sequelize-typescript' 2import { AfterDestroy, AllowNull, Column, CreatedAt, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
3import { Avatar } from '../../../shared/models/avatars/avatar.model' 3import { Avatar } from '../../../shared/models/avatars/avatar.model'
4import { STATIC_PATHS } from '../../initializers/constants' 4import { LAZY_STATIC_PATHS } from '../../initializers/constants'
5import { logger } from '../../helpers/logger' 5import { logger } from '../../helpers/logger'
6import { remove } from 'fs-extra' 6import { remove } from 'fs-extra'
7import { CONFIG } from '../../initializers/config' 7import { CONFIG } from '../../initializers/config'
8import { throwIfNotValid } from '../utils'
9import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
8 10
9@Table({ 11@Table({
10 tableName: 'avatar' 12 tableName: 'avatar',
13 indexes: [
14 {
15 fields: [ 'filename' ],
16 unique: true
17 }
18 ]
11}) 19})
12export class AvatarModel extends Model<AvatarModel> { 20export class AvatarModel extends Model<AvatarModel> {
13 21
@@ -15,6 +23,15 @@ export class AvatarModel extends Model<AvatarModel> {
15 @Column 23 @Column
16 filename: string 24 filename: string
17 25
26 @AllowNull(true)
27 @Is('AvatarFileUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'fileUrl'))
28 @Column
29 fileUrl: string
30
31 @AllowNull(false)
32 @Column
33 onDisk: boolean
34
18 @CreatedAt 35 @CreatedAt
19 createdAt: Date 36 createdAt: Date
20 37
@@ -30,16 +47,30 @@ export class AvatarModel extends Model<AvatarModel> {
30 .catch(err => logger.error('Cannot remove avatar file %s.', instance.filename, err)) 47 .catch(err => logger.error('Cannot remove avatar file %s.', instance.filename, err))
31 } 48 }
32 49
50 static loadByName (filename: string) {
51 const query = {
52 where: {
53 filename
54 }
55 }
56
57 return AvatarModel.findOne(query)
58 }
59
33 toFormattedJSON (): Avatar { 60 toFormattedJSON (): Avatar {
34 return { 61 return {
35 path: this.getWebserverPath(), 62 path: this.getStaticPath(),
36 createdAt: this.createdAt, 63 createdAt: this.createdAt,
37 updatedAt: this.updatedAt 64 updatedAt: this.updatedAt
38 } 65 }
39 } 66 }
40 67
41 getWebserverPath () { 68 getStaticPath () {
42 return join(STATIC_PATHS.AVATARS, this.filename) 69 return join(LAZY_STATIC_PATHS.AVATARS, this.filename)
70 }
71
72 getPath () {
73 return join(CONFIG.STORAGE.AVATARS_DIR, this.filename)
43 } 74 }
44 75
45 removeAvatar () { 76 removeAvatar () {