]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/thumbnail.ts
Add infohash cache
[github/Chocobozzz/PeerTube.git] / server / models / video / thumbnail.ts
CommitLineData
e8bafea3 1import { join } from 'path'
2735a154
C
2import {
3 AfterDestroy,
4 AllowNull,
5 BelongsTo,
6 Column,
7 CreatedAt,
8 DataType,
9 Default,
10 ForeignKey,
11 Model,
12 Table,
13 UpdatedAt
14} from 'sequelize-typescript'
15import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, STATIC_PATHS, WEBSERVER } from '../../initializers/constants'
e8bafea3
C
16import { logger } from '../../helpers/logger'
17import { remove } from 'fs-extra'
18import { CONFIG } from '../../initializers/config'
19import { VideoModel } from './video'
20import { VideoPlaylistModel } from './video-playlist'
21import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type'
22
23@Table({
24 tableName: 'thumbnail',
25 indexes: [
26 {
27 fields: [ 'videoId' ]
28 },
29 {
30 fields: [ 'videoPlaylistId' ],
31 unique: true
32 }
33 ]
34})
35export class ThumbnailModel extends Model<ThumbnailModel> {
36
37 @AllowNull(false)
38 @Column
39 filename: string
40
41 @AllowNull(true)
42 @Default(null)
43 @Column
44 height: number
45
46 @AllowNull(true)
47 @Default(null)
48 @Column
49 width: number
50
51 @AllowNull(false)
52 @Column
53 type: ThumbnailType
54
55 @AllowNull(true)
2735a154 56 @Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
9cc8d43e 57 fileUrl: string
e8bafea3 58
65af03a2
C
59 @AllowNull(true)
60 @Column
61 automaticallyGenerated: boolean
62
e8bafea3
C
63 @ForeignKey(() => VideoModel)
64 @Column
65 videoId: number
66
67 @BelongsTo(() => VideoModel, {
68 foreignKey: {
69 allowNull: true
70 },
71 onDelete: 'CASCADE'
72 })
73 Video: VideoModel
74
75 @ForeignKey(() => VideoPlaylistModel)
76 @Column
77 videoPlaylistId: number
78
79 @BelongsTo(() => VideoPlaylistModel, {
80 foreignKey: {
81 allowNull: true
82 },
83 onDelete: 'CASCADE'
84 })
85 VideoPlaylist: VideoPlaylistModel
86
87 @CreatedAt
88 createdAt: Date
89
90 @UpdatedAt
91 updatedAt: Date
92
93 private static types: { [ id in ThumbnailType ]: { label: string, directory: string, staticPath: string } } = {
3acc5084
C
94 [ThumbnailType.MINIATURE]: {
95 label: 'miniature',
e8bafea3
C
96 directory: CONFIG.STORAGE.THUMBNAILS_DIR,
97 staticPath: STATIC_PATHS.THUMBNAILS
98 },
99 [ThumbnailType.PREVIEW]: {
100 label: 'preview',
101 directory: CONFIG.STORAGE.PREVIEWS_DIR,
557b13ae 102 staticPath: LAZY_STATIC_PATHS.PREVIEWS
e8bafea3
C
103 }
104 }
105
106 @AfterDestroy
65af03a2 107 static removeFiles (instance: ThumbnailModel) {
e8bafea3
C
108 logger.info('Removing %s file %s.', ThumbnailModel.types[instance.type].label, instance.filename)
109
110 // Don't block the transaction
111 instance.removeThumbnail()
112 .catch(err => logger.error('Cannot remove thumbnail file %s.', instance.filename, err))
113 }
114
e2600d8b
C
115 static loadByName (filename: string) {
116 const query = {
117 where: {
118 filename
119 }
120 }
121
122 return ThumbnailModel.findOne(query)
123 }
124
e8bafea3
C
125 static generateDefaultPreviewName (videoUUID: string) {
126 return videoUUID + '.jpg'
127 }
128
9cc8d43e
C
129 getFileUrl () {
130 if (this.fileUrl) return this.fileUrl
e8bafea3
C
131
132 const staticPath = ThumbnailModel.types[this.type].staticPath
133 return WEBSERVER.URL + staticPath + this.filename
134 }
135
536598cf 136 getPath () {
e8bafea3 137 const directory = ThumbnailModel.types[this.type].directory
536598cf
C
138 return join(directory, this.filename)
139 }
e8bafea3 140
536598cf
C
141 removeThumbnail () {
142 return remove(this.getPath())
e8bafea3
C
143 }
144}