]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/thumbnail.ts
Fix feeds
[github/Chocobozzz/PeerTube.git] / server / models / video / thumbnail.ts
CommitLineData
a8b1b404 1import { remove } from 'fs-extra'
e8bafea3 2import { join } from 'path'
2735a154
C
3import {
4 AfterDestroy,
5 AllowNull,
a35a2279
C
6 BeforeCreate,
7 BeforeUpdate,
2735a154
C
8 BelongsTo,
9 Column,
10 CreatedAt,
11 DataType,
12 Default,
13 ForeignKey,
14 Model,
15 Table,
16 UpdatedAt
17} from 'sequelize-typescript'
a35a2279 18import { afterCommitIfTransaction } from '@server/helpers/database-utils'
90a8bd30 19import { MThumbnail, MThumbnailVideo, MVideoWithHost } from '@server/types/models'
a8b1b404 20import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type'
e8bafea3 21import { logger } from '../../helpers/logger'
e8bafea3 22import { CONFIG } from '../../initializers/config'
a8b1b404 23import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, STATIC_PATHS, WEBSERVER } from '../../initializers/constants'
e8bafea3
C
24import { VideoModel } from './video'
25import { VideoPlaylistModel } from './video-playlist'
e8bafea3
C
26
27@Table({
28 tableName: 'thumbnail',
29 indexes: [
30 {
31 fields: [ 'videoId' ]
32 },
33 {
34 fields: [ 'videoPlaylistId' ],
35 unique: true
a8b1b404
C
36 },
37 {
38 fields: [ 'filename', 'type' ],
39 unique: true
e8bafea3
C
40 }
41 ]
42})
b49f22d8 43export class ThumbnailModel extends Model {
e8bafea3
C
44
45 @AllowNull(false)
46 @Column
47 filename: string
48
49 @AllowNull(true)
50 @Default(null)
51 @Column
52 height: number
53
54 @AllowNull(true)
55 @Default(null)
56 @Column
57 width: number
58
59 @AllowNull(false)
60 @Column
61 type: ThumbnailType
62
63 @AllowNull(true)
2735a154 64 @Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
9cc8d43e 65 fileUrl: string
e8bafea3 66
65af03a2
C
67 @AllowNull(true)
68 @Column
69 automaticallyGenerated: boolean
70
e8bafea3
C
71 @ForeignKey(() => VideoModel)
72 @Column
73 videoId: number
74
75 @BelongsTo(() => VideoModel, {
76 foreignKey: {
77 allowNull: true
78 },
79 onDelete: 'CASCADE'
80 })
81 Video: VideoModel
82
83 @ForeignKey(() => VideoPlaylistModel)
84 @Column
85 videoPlaylistId: number
86
87 @BelongsTo(() => VideoPlaylistModel, {
88 foreignKey: {
89 allowNull: true
90 },
91 onDelete: 'CASCADE'
92 })
93 VideoPlaylist: VideoPlaylistModel
94
95 @CreatedAt
96 createdAt: Date
97
98 @UpdatedAt
99 updatedAt: Date
100
a35a2279
C
101 // If this thumbnail replaced existing one, track the old name
102 previousThumbnailFilename: string
103
a1587156 104 private static readonly types: { [ id in ThumbnailType ]: { label: string, directory: string, staticPath: string } } = {
3acc5084
C
105 [ThumbnailType.MINIATURE]: {
106 label: 'miniature',
e8bafea3
C
107 directory: CONFIG.STORAGE.THUMBNAILS_DIR,
108 staticPath: STATIC_PATHS.THUMBNAILS
109 },
110 [ThumbnailType.PREVIEW]: {
111 label: 'preview',
112 directory: CONFIG.STORAGE.PREVIEWS_DIR,
557b13ae 113 staticPath: LAZY_STATIC_PATHS.PREVIEWS
e8bafea3
C
114 }
115 }
116
a35a2279
C
117 @BeforeCreate
118 @BeforeUpdate
119 static removeOldFile (instance: ThumbnailModel, options) {
120 return afterCommitIfTransaction(options.transaction, () => instance.removePreviousFilenameIfNeeded())
121 }
122
e8bafea3 123 @AfterDestroy
65af03a2 124 static removeFiles (instance: ThumbnailModel) {
e8bafea3
C
125 logger.info('Removing %s file %s.', ThumbnailModel.types[instance.type].label, instance.filename)
126
127 // Don't block the transaction
128 instance.removeThumbnail()
129 .catch(err => logger.error('Cannot remove thumbnail file %s.', instance.filename, err))
130 }
131
a35a2279
C
132 static loadByFilename (filename: string, thumbnailType: ThumbnailType): Promise<MThumbnail> {
133 const query = {
134 where: {
135 filename,
136 type: thumbnailType
137 }
138 }
139
140 return ThumbnailModel.findOne(query)
141 }
142
143 static loadWithVideoByFilename (filename: string, thumbnailType: ThumbnailType): Promise<MThumbnailVideo> {
e2600d8b
C
144 const query = {
145 where: {
a8b1b404
C
146 filename,
147 type: thumbnailType
148 },
149 include: [
150 {
151 model: VideoModel.unscoped(),
152 required: true
153 }
154 ]
e2600d8b
C
155 }
156
157 return ThumbnailModel.findOne(query)
158 }
159
374b725d
C
160 static buildPath (type: ThumbnailType, filename: string) {
161 const directory = ThumbnailModel.types[type].directory
162
163 return join(directory, filename)
164 }
165
90a8bd30 166 getFileUrl (video: MVideoWithHost) {
ca6d3622 167 const staticPath = ThumbnailModel.types[this.type].staticPath + this.filename
e8bafea3 168
ca6d3622 169 if (video.isOwned()) return WEBSERVER.URL + staticPath
ca6d3622 170
d9a2a031 171 return this.fileUrl
e8bafea3
C
172 }
173
536598cf 174 getPath () {
374b725d 175 return ThumbnailModel.buildPath(this.type, this.filename)
536598cf 176 }
e8bafea3 177
a35a2279 178 getPreviousPath () {
374b725d 179 return ThumbnailModel.buildPath(this.type, this.previousThumbnailFilename)
a35a2279
C
180 }
181
536598cf
C
182 removeThumbnail () {
183 return remove(this.getPath())
e8bafea3 184 }
a35a2279
C
185
186 removePreviousFilenameIfNeeded () {
187 if (!this.previousThumbnailFilename) return
188
189 const previousPath = this.getPreviousPath()
190 remove(previousPath)
191 .catch(err => logger.error('Cannot remove previous thumbnail file %s.', previousPath, { err }))
192
193 this.previousThumbnailFilename = undefined
194 }
e8bafea3 195}