]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/thumbnail.ts
Don't guess remote tracker URL
[github/Chocobozzz/PeerTube.git] / server / models / video / thumbnail.ts
1 import { remove } from 'fs-extra'
2 import { join } from 'path'
3 import {
4 AfterDestroy,
5 AllowNull,
6 BeforeCreate,
7 BeforeUpdate,
8 BelongsTo,
9 Column,
10 CreatedAt,
11 DataType,
12 Default,
13 ForeignKey,
14 Model,
15 Table,
16 UpdatedAt
17 } from 'sequelize-typescript'
18 import { afterCommitIfTransaction } from '@server/helpers/database-utils'
19 import { MThumbnail, MThumbnailVideo, MVideoWithHost } from '@server/types/models'
20 import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type'
21 import { logger } from '../../helpers/logger'
22 import { CONFIG } from '../../initializers/config'
23 import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, STATIC_PATHS, WEBSERVER } from '../../initializers/constants'
24 import { VideoModel } from './video'
25 import { VideoPlaylistModel } from './video-playlist'
26
27 @Table({
28 tableName: 'thumbnail',
29 indexes: [
30 {
31 fields: [ 'videoId' ]
32 },
33 {
34 fields: [ 'videoPlaylistId' ],
35 unique: true
36 },
37 {
38 fields: [ 'filename', 'type' ],
39 unique: true
40 }
41 ]
42 })
43 export class ThumbnailModel extends Model {
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)
64 @Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
65 fileUrl: string
66
67 @AllowNull(true)
68 @Column
69 automaticallyGenerated: boolean
70
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
101 // If this thumbnail replaced existing one, track the old name
102 previousThumbnailFilename: string
103
104 private static readonly types: { [ id in ThumbnailType ]: { label: string, directory: string, staticPath: string } } = {
105 [ThumbnailType.MINIATURE]: {
106 label: 'miniature',
107 directory: CONFIG.STORAGE.THUMBNAILS_DIR,
108 staticPath: STATIC_PATHS.THUMBNAILS
109 },
110 [ThumbnailType.PREVIEW]: {
111 label: 'preview',
112 directory: CONFIG.STORAGE.PREVIEWS_DIR,
113 staticPath: LAZY_STATIC_PATHS.PREVIEWS
114 }
115 }
116
117 @BeforeCreate
118 @BeforeUpdate
119 static removeOldFile (instance: ThumbnailModel, options) {
120 return afterCommitIfTransaction(options.transaction, () => instance.removePreviousFilenameIfNeeded())
121 }
122
123 @AfterDestroy
124 static removeFiles (instance: ThumbnailModel) {
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
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> {
144 const query = {
145 where: {
146 filename,
147 type: thumbnailType
148 },
149 include: [
150 {
151 model: VideoModel.unscoped(),
152 required: true
153 }
154 ]
155 }
156
157 return ThumbnailModel.findOne(query)
158 }
159
160 static buildPath (type: ThumbnailType, filename: string) {
161 const directory = ThumbnailModel.types[type].directory
162
163 return join(directory, filename)
164 }
165
166 getFileUrl (video: MVideoWithHost) {
167 const staticPath = ThumbnailModel.types[this.type].staticPath + this.filename
168
169 if (video.isOwned()) return WEBSERVER.URL + staticPath
170
171 return this.fileUrl
172 }
173
174 getPath () {
175 return ThumbnailModel.buildPath(this.type, this.filename)
176 }
177
178 getPreviousPath () {
179 return ThumbnailModel.buildPath(this.type, this.previousThumbnailFilename)
180 }
181
182 removeThumbnail () {
183 return remove(this.getPath())
184 }
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 }
195 }