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