]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/thumbnail.ts
Fix ownership change
[github/Chocobozzz/PeerTube.git] / server / models / video / thumbnail.ts
1 import { join } from 'path'
2 import { AfterDestroy, AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
3 import { LAZY_STATIC_PATHS, STATIC_PATHS, WEBSERVER } from '../../initializers/constants'
4 import { logger } from '../../helpers/logger'
5 import { remove } from 'fs-extra'
6 import { CONFIG } from '../../initializers/config'
7 import { VideoModel } from './video'
8 import { VideoPlaylistModel } from './video-playlist'
9 import { 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 })
23 export 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
45 fileUrl: string
46
47 @AllowNull(true)
48 @Column
49 automaticallyGenerated: boolean
50
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 } } = {
82 [ThumbnailType.MINIATURE]: {
83 label: 'miniature',
84 directory: CONFIG.STORAGE.THUMBNAILS_DIR,
85 staticPath: STATIC_PATHS.THUMBNAILS
86 },
87 [ThumbnailType.PREVIEW]: {
88 label: 'preview',
89 directory: CONFIG.STORAGE.PREVIEWS_DIR,
90 staticPath: LAZY_STATIC_PATHS.PREVIEWS
91 }
92 }
93
94 @AfterDestroy
95 static removeFiles (instance: ThumbnailModel) {
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
103 static loadByName (filename: string) {
104 const query = {
105 where: {
106 filename
107 }
108 }
109
110 return ThumbnailModel.findOne(query)
111 }
112
113 static generateDefaultPreviewName (videoUUID: string) {
114 return videoUUID + '.jpg'
115 }
116
117 getFileUrl () {
118 if (this.fileUrl) return this.fileUrl
119
120 const staticPath = ThumbnailModel.types[this.type].staticPath
121 return WEBSERVER.URL + staticPath + this.filename
122 }
123
124 getPath () {
125 const directory = ThumbnailModel.types[this.type].directory
126 return join(directory, this.filename)
127 }
128
129 removeThumbnail () {
130 return remove(this.getPath())
131 }
132 }