]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/thumbnail.ts
Fix video import with long thumbnail url
[github/Chocobozzz/PeerTube.git] / server / models / video / thumbnail.ts
1 import { join } from 'path'
2 import {
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'
15 import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, STATIC_PATHS, WEBSERVER } from '../../initializers/constants'
16 import { logger } from '../../helpers/logger'
17 import { remove } from 'fs-extra'
18 import { CONFIG } from '../../initializers/config'
19 import { VideoModel } from './video'
20 import { VideoPlaylistModel } from './video-playlist'
21 import { 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 })
35 export 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)
56 @Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
57 fileUrl: string
58
59 @AllowNull(true)
60 @Column
61 automaticallyGenerated: boolean
62
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 } } = {
94 [ThumbnailType.MINIATURE]: {
95 label: 'miniature',
96 directory: CONFIG.STORAGE.THUMBNAILS_DIR,
97 staticPath: STATIC_PATHS.THUMBNAILS
98 },
99 [ThumbnailType.PREVIEW]: {
100 label: 'preview',
101 directory: CONFIG.STORAGE.PREVIEWS_DIR,
102 staticPath: LAZY_STATIC_PATHS.PREVIEWS
103 }
104 }
105
106 @AfterDestroy
107 static removeFiles (instance: ThumbnailModel) {
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
115 static loadByName (filename: string) {
116 const query = {
117 where: {
118 filename
119 }
120 }
121
122 return ThumbnailModel.findOne(query)
123 }
124
125 static generateDefaultPreviewName (videoUUID: string) {
126 return videoUUID + '.jpg'
127 }
128
129 getFileUrl () {
130 if (this.fileUrl) return this.fileUrl
131
132 const staticPath = ThumbnailModel.types[this.type].staticPath
133 return WEBSERVER.URL + staticPath + this.filename
134 }
135
136 getPath () {
137 const directory = ThumbnailModel.types[this.type].directory
138 return join(directory, this.filename)
139 }
140
141 removeThumbnail () {
142 return remove(this.getPath())
143 }
144 }